哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
1、指定外部安装源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2、安装redis
yum -y install redis
1、vi /etc/redis.conf
#修改绑定IP地址为本机IP
bind 10.3.152.78
#以守护进程在后台运行
daemonize yes
1、vi /etc/redis.conf
#分别修改绑定的对应主机IP地址
bind 10.3.151.34
#以守护进程在后台运行
daemonize yes
#指定主库IP地址与端口
slaveof 10.3.152.78 6379
1、service redis start
2、在主库上检查redis同步状态
[root@ops-site redis]# redis-cli -h 10.3.152.78
10.3.152.78:6379> info replication
# Replication
role:master # 当前主机为master角色
connected_slaves:2 #有两个从库连接过来
slave0:ip=10.3.151.86,port=6379,state=online,offset=228889,lag=1 #slave0信息
slave1:ip=10.3.151.34,port=6379,state=online,offset=228889,lag=1 #slave1信息
master_repl_offset:229163
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:229162
bind 0.0.0.0
port 26379
dir /tmp
#重点 10.3.152.78 为主库,6379为主库端口,2为当两个哨兵同时成立
sentinel monitor mymaster 10.3.152.78 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
logfile /var/log/redis/sentinel.log
service redis-sentinel start
[root@ops-site ~]# redis-cli -h 10.3.152.78 -p 26379
10.3.152.78:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.3.152.78:6379,slaves=2,sentinels=3
1、分别在主库上停止redis服务,看是否会切换到别的服务器作为master
2、启动主库的redis,看是否会自动从当前的主库中同步数据。
原文:https://blog.51cto.com/12965094/2422588