实验环境:
- Master:172.18.253.123 [node1]
- Slave: 172.18.254.57 [node2]
- Slave: 172.18.254.75 [node3]
配置主从复制环境
配置主节点
redis-sentinel
即可[root@node1 ~]# vim /etc/redis-sentinel.conf
bind 0.0.0.0 #默认只监听了本机的26379端口,需手动监听同外部的通信地址
sentinel monitor mymaster 172.18.253.123 6379 2 #定义故障转移集群名(此处为mymaster), 主从集群中的主节点地址, 2表示一共有3个拥有投票权的主机,必须≥2才算有效,以此类推(参与投票的主机数必须为奇数)
sentinel auth-pass mymaster guomai #故障转移集群的认证密码
sentinel down-after-milliseconds mymaster 30000 #主节点异常状态持续多久判定为故障状态
sentinel parallel-syncs mymaster 1 #能够被sentinel并行配置的最大从主机数量
sentinel failover-timeout mymaster 180000 #故障转移超时时长
logfile /var/log/redis/sentinel.log
[root@node1 ~]# scp /etc/redis-sentinel.conf root@172.18.254.75:/etc/
[root@node1 ~]# scp /etc/redis-sentinel.conf root@172.18.254.57:/etc/
[root@node1 ~]# systemctl start redis redis-sentinel.service
[root@node1 ~]# ss -tnlp |grep redis*
LISTEN 0 128 *:26379 *:* users:(("redis-sentinel",pid=11230,fd=4))
LISTEN 0 128 *:6379 *:* users:(("redis-server",pid=11037,fd=4))
配置从节点
root@node2 ~# systemctl restart redis-sentinel
root@node3 ~# systemctl restart redis-sentinel
检查故障转移关系
root@node2 ~# redis-cli -h 172.18.253.123 -p 26379 #远程登录Master
172.18.253.123:26379> SENTINEL MASTERS
172.18.253.123:26379> SENTINEL SLAVES mymaster
测试
root@node1 ~# systemctl stop redis #将主节点的Redis服务停止
稍等片刻再次查看
172.18.253.123:26379> SENTINEL MASTERS #查看所有集群的主节点信息
172.18.253.123:26379> SENTINEL slaves mymaster #查看特定集群的从节点信息
修复故障节点
root@node1 ~# vim /etc/redis.conf
slaveof 172.18.254.75 6379
masterauth guomai
root@node1 ~# systemctl restart redis
root@node2 ~# redis-cli -h 172.18.253.123 -p 26379