安装redis
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar -xzf redis-3.2.1.tar.gz
cd redis-3.2.1
make && make instal
在/usr/local/ 下新建一个目录redis-sentinel,然后在此目录下新建7501/ 7502/ 7503/ 7504/ 7505/ 7506/ 7507/七个目录。
mkdir /usr/local/redis-sentinel
mkdir /usr/local/redis-sentinel/{7501,7502,7503,7504,7505,7506,7507}
将redis安装目录下的reids.conf,拷贝到前4个目录下,分别命名为:redis-7501.conf redis-7502.conf redis-7503.conf redis-7504.conf(名字无所谓只是为了区分)
cp /root/redis-3.2.1/redis.conf /usr/local/redis-sentinel/7501/redis-7501.conf
cp /root/redis-3.2.1/redis.conf /usr/local/redis-sentinel/7502/redis-7502.conf
cp /root/redis-3.2.1/redis.conf /usr/local/redis-sentinel/7503/redis-7503.conf
cp /root/redis-3.2.1/redis.conf /usr/local/redis-sentinel/7504/redis-7504.conf
修改配置文件内容(以redis-7501.conf为例)
daemonize yes
pidfile /var/run/redis_7501.pid
port 7501
bind 10.10.172.191 #可选,默认就处理所有请求。
logfile "./redis-7501.log"
dir "/usr/local/redis-sentinel/7501"
#redis配置密码的话,需要以下配置
masterauth "123456"
requirepass "123456"
appendonly yes
将redis安装目录下的sentinel.conf拷贝到7505/ 7506/和7507/目录下分别命名: sentinel-7505.conf sentinel-7506.conf sentinel-7507.conf
cp /root/redis-3.2.1/sentinel.conf /usr/local/redis-sentinel/7505/sentinel-7505.conf
cp /root/redis-3.2.1/sentinel.conf /usr/local/redis-sentinel/7506/sentinel-7506.conf
cp /root/redis-3.2.1/sentinel.conf /usr/local/redis-sentinel/7507/sentinel-7507.conf
修改配置文件(以sentinel-7505.conf为例)
daemonize yes
port 7505
#指定工作目录
dir "/usr/local/redis-sentinel/7505"no
logfile "./sentinel.log"
#指定别名 主节点地址 端口 哨兵个数(有几个哨兵监控到主节点宕机执行转移)
sentinel monitor mymaster 10.10.172.191 7501 2
#如果哨兵3s内没有收到主节点的心跳,哨兵就认为主节点宕机了,默认是30秒
sentinel down-after-milliseconds mymaster 3000
#选举出新的主节点之后,可以同时连接从节点的个数
sentinel parallel-syncs mymaster 1
#如果10秒后,master仍没活过来,则启动failover,默认180s
sentinel failover-timeout mymaster 10000
#配置连接redis主节点密码
sentinel auth-pass mymaster 123456
启动redis和sentinel
分别启动4个redis实例
redis-server /usr/local/redis-sentinel/7501/redis-7501.conf
redis-server /usr/local/redis-sentinel/7502/redis-7502.conf
redis-server /usr/local/redis-sentinel/7503/redis-7503.conf
redis-server /usr/local/redis-sentinel/7504/redis-7504.conf
然后分别登陆7502 7503 7504三个实例,动态改变主从关系,成为7501的slave
redis-cli -h 192.168.111.146 -p 7502 -a 123456
192.168.111.146:7502> SLAVEOF 127.0.0.1 7501
可以事先直接修改从实例redis文件追加slaveof 192.168.111.146 7501
192.168.111.146:7501> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=10.10.172.191,port=7502,state=online,offset=127,lag=0
slave1:ip=10.10.172.191,port=7503,state=online,offset=127,lag=0
slave2:ip=10.10.172.191,port=7504,state=online,offset=127,lag=1
master_repl_offset:127
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:126
以后台启动模式启动两个sentinel(哨兵):
redis-sentinel /usr/local/redis-sentinel/7505/sentinel-7505.conf
redis-sentinel /usr/local/redis-sentinel/7506/sentinel-7506.conf
redis-sentinel /usr/local/redis-sentinel/7507/sentinel-7507.conf
测试:
登陆到 master:
# redis-cli -h 10.10.172.191 -p 7501 -a 123456
192.168.111.146:7501> info Replication #列出Master的信息
192.168.111.146:7501> set name "zhangsan"
# redis-cli -h 10.10.172.191 -p 7502 -a 123456
192.168.111.146:7502> get name
"zhangsan"
192.168.111.146:7502> set age 24
(error) READONLY You can't wri
te against a read only slave.
目前7501是master, 我们强制kill掉 7501 的进程以后,查看sentinel打出的日志信息:
ps -ef|grep redis
kill 7927
tail -fn 10 /usr/local/redis-sentinel/7505/sentinel.log
可以看到master从7501转到7504