1.准备文件
1.解压redis-4.0.1.tar.gz的redis文件
2.新建目录 redis-cluster以及子目录 master-6379 slave-7000 slave-7001
3.复制redis目录下的redis.conf配置文件到三个目录下各一份
redis_6379.conf redis_7000.conf redis_7001.conf
2.修改配置文件:1master +2slave + 3sentinel
4.1 maste和slave的配置
1)redis_6379.conf
bind 127.0.0.1 这里也可以不配置
port 6379
pidfile /var/run/redis_6379.pid
maxmemory 1024*1024*10
2)redis_7000.conf
bind 127.0.0.1 这里也可以不配置
port 7000
pidfile /var/run/redis_7000.pid
maxmemory 1024*1024*10
3)redis_7001.conf
bind 127.0.0.1 这里也可以不配置
port 7001
pidfile /var/run/redis_7001.pid
maxmemory 1024*1024*10
4.2 sentinel的配置:
复制redis目录下的sentinel.conf配置文件到master-6379 slave-7000 slave-7001三个目录下各一份
1)sentinel_26379.conf
port 26379
sentinel myid 94a9064543f19c638d6e9a9ee2b4c06150743216 这一行如果一直一定要删掉,这个会动生成
# sentinel parallel-syncs <master-name> <numslaves>
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel parallel-syncs mymaster 1
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 3000
2)sentinel_27000conf
port 27000
sentinel myid 94a9064543f19c638d6e9a9ee2b4c06150743216 这一行如果一直一定要删掉,这个会动生成
# sentinel parallel-syncs <master-name> <numslaves>
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel parallel-syncs mymaster 1
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 3000
3)sentinel_27001conf
port 27001
sentinel myid 94a9064543f19c638d6e9a9ee2b4c06150743216 这一行如果一直一定要删掉,这个会动生成
# sentinel parallel-syncs <master-name> <numslaves>
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel parallel-syncs mymaster 1
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 3000
依次启动redis服务
启动命令:./redis-4.0.1/src/reids-server redis-cluster/redis-master/redis_6379.conf 其他的依次类推启动
查看服务器结果:
root@db:~/mysoft/soft# ./redis-4.0.1/src/redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=7000,state=online,offset=914454,lag=0
slave1:ip=127.0.0.1,port=7001,state=online,offset=914188,lag=1
master_replid:631cc91ab9e66d1555f7ed526d50219663b6bc19
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:914454
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:537727
查看sentinel
root@hecs-x-medium-2-linux-20200620124443:~/mysoft/soft# ./redis-4.0.1/src/redis-cli -h 127.0.0.1 -p 26379
127.0.0.1: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=127.0.0.1:6379,slaves=2,sentinels=3
slaves=2,sentinels=3 才表示配置成功,否则无法进行主从切换,因为前面配置了 sentinel monitor <master-name> <ip> <redis-port> <quorum> quorum=2,表示有两个sentinel认为主节点宕机就下掉主节点,重新选举进行主从切换
注意:
1.都监听mater,只要改下端口号就好,后面sentinel自动同步监听其他节点以及互相监听
2.配置文件sentinel.conf中如果有myid一定要删掉,不然会导致主从切换失败
查看sentinel启动后,配置文件的末尾处的变化:每个sentinel的配置文件末尾都会追加相同的配置
# Generated by CONFIG REWRITE sentinel leader-epoch mymaster 0 sentinel known-slave mymaster 127.0.0.1 7000 sentinel known-slave mymaster 127.0.0.1 7001 sentinel known-sentinel mymaster 127.0.0.1 27001 efd086631857c7295cfc50a37f5c0796bc3b7549 sentinel known-sentinel mymaster 127.0.0.1 27000 f0b7aeca04b7b27a74c87c7021f8a72052dc8c41 sentinel current-epoch 0
3.查看sentinel控制台日志:
26379
5445:X 12 Jan 23:33:01.599 # Sentinel ID is 674501a329a678b914a3bb0483ba223ede535c94
5445:X 12 Jan 23:33:01.599 # +monitor master mymaster 127.0.0.1 6379 quorum 2
5445:X 12 Jan 23:33:17.057 * +sentinel-invalid-addr sentinel f0b7aeca04b7b27a74c87c7021f8a72052dc8c41 127.0.0.1 27000 @ mymaster 127.0.0.1 6379
5445:X 12 Jan 23:33:17.057 * +sentinel sentinel 38dfc2e01c118fd897378b5c121912fc266923a8 127.0.0.1 27000 @ mymaster 127.0.0.1 6379
5445:X 12 Jan 23:33:25.580 * +sentinel-invalid-addr sentinel efd086631857c7295cfc50a37f5c0796bc3b7549 127.0.0.1 27001 @ mymaster 127.0.0.1 6379
5445:X 12 Jan 23:33:25.580 * +sentinel sentinel d315173e7739c37759806c5b243752b755ca7c91 127.0.0.1 27001 @ mymaster 127.0.0.1 6379
27000
5451:X 12 Jan 23:33:15.031 # Sentinel ID is 38dfc2e01c118fd897378b5c121912fc266923a8
5451:X 12 Jan 23:33:15.031 # +monitor master mymaster 127.0.0.1 6379 quorum 2
5451:X 12 Jan 23:33:15.032 * +sentinel-invalid-addr sentinel 94a9064543f19c638d6e9a9ee2b4c06150743216 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
5451:X 12 Jan 23:33:15.032 * +sentinel sentinel 674501a329a678b914a3bb0483ba223ede535c94 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
5451:X 12 Jan 23:33:25.503 * +sentinel-invalid-addr sentinel efd086631857c7295cfc50a37f5c0796bc3b7549 127.0.0.1 27001 @ mymaster 127.0.0.1 6379
5451:X 12 Jan 23:33:25.503 * +sentinel sentinel d315173e7739c37759806c5b243752b755ca7c91 127.0.0.1 27001 @ mymaster 127.0.0.1 6379
5451:X 12 Jan 23:33:45.043 # +sdown sentinel 94a9064543f19c638d6e9a9ee2b4c06150743216 127.0.0.1 0 @ mymaster 127.0.0.1 6379
5451:X 12 Jan 23:33:54.513 # +sdown sentinel efd086631857c7295cfc50a37f5c0796bc3b7549 127.0.0.1 0 @ mymaster 127.0.0.1 6379
27001
5455:X 12 Jan 23:33:23.440 # Sentinel ID is d315173e7739c37759806c5b243752b755ca7c91
5455:X 12 Jan 23:33:23.441 # +monitor master mymaster 127.0.0.1 6379 quorum 2
5455:X 12 Jan 23:33:23.461 * +sentinel-invalid-addr sentinel f0b7aeca04b7b27a74c87c7021f8a72052dc8c41 127.0.0.1 27000 @ mymaster 127.0.0.1 6379
5455:X 12 Jan 23:33:23.461 * +sentinel sentinel 38dfc2e01c118fd897378b5c121912fc266923a8 127.0.0.1 27000 @ mymaster 127.0.0.1 6379
5455:X 12 Jan 23:33:24.264 * +sentinel-invalid-addr sentinel 94a9064543f19c638d6e9a9ee2b4c06150743216 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
5455:X 12 Jan 23:33:24.264 * +sentinel sentinel 674501a329a678b914a3bb0483ba223ede535c94 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
5455:X 12 Jan 23:33:53.462 # +sdown sentinel f0b7aeca04b7b27a74c87c7021f8a72052dc8c41 127.0.0.1 0 @ mymaster 127.0.0.1 6379
5455:X 12 Jan 23:33:53.462 # +sdown sentinel 94a9064543f19c638d6e9a9ee2b4c06150743216 127.0.0.1 0 @ mymaster 127.0.0.1 6379
上面打印的日志显示sentinel处理会自动监听master和slave,还会互相监听
4.进行主从切换看日志
26379 5445:X 12 Jan 23:33:01.599 # Sentinel ID is 674501a329a678b914a3bb0483ba223ede535c94 5445:X 12 Jan 23:33:01.599 # +monitor master mymaster 127.0.0.1 6379 quorum 2 5445:X 12 Jan 23:33:17.057 * +sentinel-invalid-addr sentinel f0b7aeca04b7b27a74c87c7021f8a72052dc8c41 127.0.0.1 27000 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:33:17.057 * +sentinel sentinel 38dfc2e01c118fd897378b5c121912fc266923a8 127.0.0.1 27000 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:33:25.580 * +sentinel-invalid-addr sentinel efd086631857c7295cfc50a37f5c0796bc3b7549 127.0.0.1 27001 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:33:25.580 * +sentinel sentinel d315173e7739c37759806c5b243752b755ca7c91 127.0.0.1 27001 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:33:47.078 # +sdown sentinel f0b7aeca04b7b27a74c87c7021f8a72052dc8c41 127.0.0.1 0 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:33:55.308 # +sdown sentinel efd086631857c7295cfc50a37f5c0796bc3b7549 127.0.0.1 0 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:37:30.061 # +sdown master mymaster 127.0.0.1 6379 5445:X 12 Jan 23:37:30.188 # +new-epoch 1 5445:X 12 Jan 23:37:30.191 # +vote-for-leader d315173e7739c37759806c5b243752b755ca7c91 1 5445:X 12 Jan 23:37:31.171 # +odown master mymaster 127.0.0.1 6379 #quorum 3/2 5445:X 12 Jan 23:37:31.171 # Next failover delay: I will not start a failover before Tue Jan 12 23:37:36 2021 5445:X 12 Jan 23:37:31.316 # +config-update-from sentinel d315173e7739c37759806c5b243752b755ca7c91 127.0.0.1 27001 @ mymaster 127.0.0.1 6379 5445:X 12 Jan 23:37:31.316 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 7000 5445:X 12 Jan 23:37:31.317 * +slave slave 127.0.0.1:7001 127.0.0.1 7001 @ mymaster 127.0.0.1 7000 5445:X 12 Jan 23:37:31.317 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 7000 5445:X 12 Jan 23:38:01.373 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 7000
上面显示切换 7000端口的机器为master
slave-7000机器的日志
5302:M 12 Jan 23:37:30.400 # Setting secondary replication ID to 1be1283f0bfec0e63832668a5a0fe738359dd550, valid up to offset: 593645. New replication ID is 11c02d28f2f002abc551a2abfaa58a33863977cf 5302:M 12 Jan 23:37:30.400 * Discarding previously cached master state. 5302:M 12 Jan 23:37:30.400 * MASTER MODE enabled (user request from 'id=14 addr=127.0.0.1:54446 fd=12 name=sentinel-d315173e-cmd age=247 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=0 qbuf-free=32768
obl=36 oll=0 omem=0 events=r cmd=exec') 5302:M 12 Jan 23:37:30.401 # CONFIG REWRITE executed with success. 5302:M 12 Jan 23:37:31.928 * Slave 127.0.0.1:7001 asks for synchronization 5302:M 12 Jan 23:37:31.928 * Partial resynchronization request from 127.0.0.1:7001 accepted. Sending 422 bytes of backlog starting from offset 593645
r