1.环境准备 三台主机实现:确保三台服务器redis的版本一致,以host1为主服务,其他为从 host1 192.168.1.9 master host2 192.168.1.106 slave1 host3 192.168.1.110 slave2 2.配置host1的哨兵配置文件sentinel.co
1.环境准备
三台主机实现:确保三台服务器redis的版本一致,以host1为主服务,其他为从host1 192.168.1.9 masterhost2 192.168.1.106 slave1host3 192.168.1.110 slave2
2.配置host1的哨兵配置文件sentinel.conf
[root@localhost ~]# vim /app/redis/etc/sentinel.confbind 192.168.1.9port 6379daemonize yeslogfile "sentinel_26379.log"dir "/app/redis/log"sentinel monitor mymaster 192.168.1.9 6379 2 #当有2个及以上的哨兵断定master宕机时会选举新的mastersentinel auth-pass mymaster 123456sentinel down-after-milliseconds mymaster 30000 #主观下线的时间sentinel parallel-syncs mymaster 1 #向新 master 同步数据的 slave 数量,数字越 小总同步时间越长sentinel failover - timeout mymaster 180000 #所有slave指向新的master所需的超时时间sentinel deny-scripts-reconfig yes
3.配置host2的哨兵配置文件sentinel.conf,只需修改绑定的ip其他与主相同
[root@localhost ~]# vim /app/redis/etc/sentinel.confbind 192.168.1.106 …………
4.配置host3的哨兵配置文件sentinel.conf,只需修改绑定的ip其他与主相同
[root@localhost ~]# vim /app/redis/etc/sentinel.confbind 192.168.1.110 …………
5.在命令行将host2和host3的主服务指向host1
host2:[root@localhost ~]# redis-cli127.0.0.1:6379> auth 12345OK127.0.0.1:6379> slaveof 192.168.1.9 6379127.0.0.1:6379> config set masterauth 123456127.0.0.1:6379> info replication#Replicationrole:masterconnected_slaves:1slave:ip=192.168.1.106,port=6379,state=online,offset=1541332,lag=1…………
host3:[root@localhost ~]# redis-cli127.0.0.1:6379> auth 12345OK127.0.0.1:6379> slaveof 192.168.1.9 6379127.0.0.1:6379> config set masterauth 123456127.0.0.1:6379> info replication#Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.106,port=6379,state=online,offset=1541332,lag=1slave1:ip=192.168.1.110,port=6379,state=online,offset=1541332,lag=1…………
6.三台服务同时开启哨兵服务
[root@localhost ~]# redis-server /app/redis/etc/redis.conf
7.将host1主服挂掉模拟宕机测试,测试是否其中的salve有一台会被自动提升为主服务
hsot1服务器:
[root@localhost ~]# ss -tnlpState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 511 192.168.1.9:26379 *:* users:(("redis-sentinel",pid=2413,fd=6))LISTEN 0 511 192.168.1.9:6379 *:* users:(("redis-server",pid=2369,fd=7))LISTEN 0 511 127.0.0.1:6379 *:* users:(("redis-server",pid=2369,fd=6))
root@localhost ~]# kill -9 2413
host2服务器:可以看到此服务被提升为主服务127.0.0.1:6379> info replication#Keyspacedb0:keys=7,expires=0,avg_ttl=0127.0.0.1:6379> info replication#Replicationrole:master #角色切换为主connected_slaves:1 #当亲从服务的连接个数slave0:ip=192.168.1.110,port=6379,state=online,offset=1728348,lag=1master_replid:7c8eb18c7c05b8f2d9f29028d016f9c40e8c2ce0master_replid2:a8efce354ba8249ff264dcba60ac21030253b829master_repl_offset:1728348second_repl_offset:63848repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:679773repl_backlog_histlen:1048576
host2服务器:此时的主指定为host2服务器127.0.0.1:6379> info replication#Replicationrole:slavemaster_host:192.168.1.106 #主服务为host2master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:1758995slave_priority:100slave_read_only:1…………
8.当host1服务恢复正常,重启,会被自动切换为从服务
1)重启之前先修改redis.conf配置文件,添加连接主服务的认证密码即可[root@localhost ~]# vim /app/redis/etc/redis.conf masterauth 1234562)在重启host1的redis服务[root@localhost ~]# redis-server /app/redis/etc/redis.conf3)查看其所处的状态,可以看到被切换的从服务127.0.0.1:6379> info replication#Replicationrole:slavemaster_host:192.168.1.106 #主服务指向host2master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:1829345slave_priority:100slave_read_only:1…………
9.在查看host2被提升为新的主后的连接状态
127.0.0.1:6379> info replication#Replicationrole:masterconnected_slaves:2slave0:ip=192.168.1.110,port=6379,state=online,offset=1854211,lag=0slave1:ip=192.168.1.9,port=6379,state=online,offset=1854072,lag=1master_replid:7c8eb18c7c05b8f2d9f29028d016f9c40e8c2ce0master_replid2:a8efce354ba8249ff264dcba60ac21030253b829master_repl_offset:1854211second_repl_offset:63848…………