视频版
https://www.bilibili.com/video/BV1u84y1y7Mu/
1.安装redis
环境信息,按官方文档准备三个节点
0 1 2 3 4 5 6 7 8 9 10 11 |
redis1 192.168.2.104 ssh redis1@192.168.2.104 redis2 192.168.2.166 ssh redis2@192.168.2.166 redis3 192.168.2.139 ssh redis3@192.168.2.139 |
执行安装过程
0 1 2 3 4 5 6 |
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list sudo apt-get update sudo apt-get install redis redis-sentinel |
2.配置集群复制配置
复制的配置,假设第一个是主节点。这里测试没做安全防护,生产记得加密码和网段限制
0 1 2 3 4 5 6 7 |
sudo vim /etc/redis/redis.conf #config all nodes bind * -::1 protected-mode no #only in redis2 and redis3 replicaof 192.168.2.104 6379 |
记得重启,之后测试集群复制情况,正常后继续配置sentinel
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
redis1@redis1:~$ redis-cli -p 6379 127.0.0.1:6379> get test "2" 127.0.0.1:6379> set test 1 OK 127.0.0.1:6379> get test "1" 127.0.0.1:6379> exit redis1@redis1:~$ redis-cli -h 192.168.2.166 -p 6379 192.168.2.166:6379> get test "1" 192.168.2.166:6379> redis1@redis1:~$ ^C redis1@redis1:~$ redis-cli -h 192.168.2.139 -p 6379 192.168.2.139:6379> get test "1" 192.168.2.139:6379> |
3.配置sentinel
新建配置文件sentinel.conf 这里测试的是自己新建的,记得关掉系统的service防止干扰。实际应该修改/etc/redis下的配置
0 1 2 3 4 5 6 7 8 9 |
port 5000 sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 #with ip or port mapping(docker or nat) sentinel announce-ip <ip> sentinel announce-port <port> |
启动成功效果
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
redis1@redis1:~$ redis-sentinel sentinel.conf 3385:X 27 Nov 2022 12:11:35.380 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 3385:X 27 Nov 2022 12:11:35.380 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=3385, just started 3385:X 27 Nov 2022 12:11:35.380 # Configuration loaded 3385:X 27 Nov 2022 12:11:35.381 * Increased maximum number of open files to 10032 (it was originally set to 1024). 3385:X 27 Nov 2022 12:11:35.381 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 7.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 5000 | `-._ `._ / _.-' | PID: 3385 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 3385:X 27 Nov 2022 12:11:35.387 * Sentinel new configuration saved on disk 3385:X 27 Nov 2022 12:11:35.387 # Sentinel ID is 9a7a4e3269530706fd1002c3018a3f0c525c25d6 3385:X 27 Nov 2022 12:11:35.387 # +monitor master mymaster 192.168.2.104 6379 quorum 2 3385:X 27 Nov 2022 12:11:52.666 * +sentinel sentinel ea6cbec52f9c04c6c325414a05562a69b503129c 192.168.2.166 5000 @ mymaster 192.168.2.104 6379 3385:X 27 Nov 2022 12:11:52.670 * Sentinel new configuration saved on disk 3385:X 27 Nov 2022 12:11:59.182 * +sentinel sentinel 095fc33d13e733d7f974faf0c2b2e6aba015fc3f 192.168.2.139 5000 @ mymaster 192.168.2.104 6379 3385:X 27 Nov 2022 12:11:59.186 * Sentinel new configuration saved on disk |
观察端口监听5000
0 1 2 3 4 5 6 7 8 9 |
redis1@redis1:~$ netstat -anp|grep 5000 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 3385/redis-sentinel tcp 0 0 192.168.2.104:55560 192.168.2.139:5000 ESTABLISHED 3385/redis-sentinel tcp 0 0 192.168.2.104:44506 192.168.2.166:5000 ESTABLISHED 3385/redis-sentinel tcp 0 0 192.168.2.104:5000 192.168.2.166:36802 ESTABLISHED 3385/redis-sentinel tcp 0 0 192.168.2.104:5000 192.168.2.139:33910 ESTABLISHED 3385/redis-sentinel tcp6 0 0 :::5000 :::* LISTEN 3385/redis-sentinel |
查看集群状态
0 1 2 3 4 |
sentinel master mymaster SENTINEL get-master-addr-by-name mymaster SENTINEL replicas mymaster SENTINEL sentinels mymaster |
redis1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
redis1@redis1:~$ redis-cli -p 5000 127.0.0.1:5000> sentinel master mymaster 1) "name" 2) "mymaster" 3) "ip" 4) "192.168.2.104" 5) "port" 6) "6379" 7) "runid" 8) "4097e9a96cdb74dd4239aede54843e33104e6913" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "244" 19) "last-ping-reply" 20) "244" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "3457" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "103880" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "0" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "60000" 39) "parallel-syncs" 40) "1" 127.0.0.1:5000> SENTINEL get-master-addr-by-name mymaster 1) "192.168.2.104" 2) "6379" 127.0.0.1:5000> SENTINEL sentinels mymaster 1) 1) "name" 2) "ea6cbec52f9c04c6c325414a05562a69b503129c" 3) "ip" 4) "192.168.2.166" 5) "port" 6) "5000" 7) "runid" 8) "ea6cbec52f9c04c6c325414a05562a69b503129c" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "241" 19) "last-ping-reply" 20) "241" 21) "down-after-milliseconds" 22) "5000" 23) "last-hello-message" 24) "763" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" 2) 1) "name" 2) "095fc33d13e733d7f974faf0c2b2e6aba015fc3f" 3) "ip" 4) "192.168.2.139" 5) "port" 6) "5000" 7) "runid" 8) "095fc33d13e733d7f974faf0c2b2e6aba015fc3f" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "241" 19) "last-ping-reply" 20) "241" 21) "down-after-milliseconds" 22) "5000" 23) "last-hello-message" 24) "425" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" |
redis2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
redis1@redis1:~$ redis-cli -h 192.168.2.166 -p 5000 192.168.2.166:5000> sentinel master mymaster 1) "name" 2) "mymaster" 3) "ip" 4) "192.168.2.104" 5) "port" 6) "6379" 7) "runid" 8) "4097e9a96cdb74dd4239aede54843e33104e6913" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "979" 19) "last-ping-reply" 20) "979" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "8165" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "188616" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "0" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "60000" 39) "parallel-syncs" 40) "1" 192.168.2.166:5000> SENTINEL sentinels mymaster 1) 1) "name" 2) "095fc33d13e733d7f974faf0c2b2e6aba015fc3f" 3) "ip" 4) "192.168.2.139" 5) "port" 6) "5000" 7) "runid" 8) "095fc33d13e733d7f974faf0c2b2e6aba015fc3f" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "855" 19) "last-ping-reply" 20) "855" 21) "down-after-milliseconds" 22) "5000" 23) "last-hello-message" 24) "166" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" 2) 1) "name" 2) "9a7a4e3269530706fd1002c3018a3f0c525c25d6" 3) "ip" 4) "192.168.2.104" 5) "port" 6) "5000" 7) "runid" 8) "9a7a4e3269530706fd1002c3018a3f0c525c25d6" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "855" 19) "last-ping-reply" 20) "855" 21) "down-after-milliseconds" 22) "5000" 23) "last-hello-message" 24) "64" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" 192.168.2.166:5000> |
redis3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
redis1@redis1:~$ redis-cli -h 192.168.2.139 -p 5000 192.168.2.139:5000> sentinel master mymaster 1) "name" 2) "mymaster" 3) "ip" 4) "192.168.2.104" 5) "port" 6) "6379" 7) "runid" 8) "4097e9a96cdb74dd4239aede54843e33104e6913" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "571" 19) "last-ping-reply" 20) "571" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "8363" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "239355" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "0" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "60000" 39) "parallel-syncs" 40) "1" 192.168.2.139:5000> SENTINEL sentinels mymaster 1) 1) "name" 2) "9a7a4e3269530706fd1002c3018a3f0c525c25d6" 3) "ip" 4) "192.168.2.104" 5) "port" 6) "5000" 7) "runid" 8) "9a7a4e3269530706fd1002c3018a3f0c525c25d6" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "232" 19) "last-ping-reply" 20) "232" 21) "down-after-milliseconds" 22) "5000" 23) "last-hello-message" 24) "753" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" 2) 1) "name" 2) "ea6cbec52f9c04c6c325414a05562a69b503129c" 3) "ip" 4) "192.168.2.166" 5) "port" 6) "5000" 7) "runid" 8) "ea6cbec52f9c04c6c325414a05562a69b503129c" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "232" 19) "last-ping-reply" 20) "232" 21) "down-after-milliseconds" 22) "5000" 23) "last-hello-message" 24) "1740" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" |
3.测试故障迁移
模拟主节点挂掉30s或者直接关掉服务,可以看到自动选主,并且修改slave的指向(redis2日志)
见加粗部分
0 1 2 |
sudo service redis-server stop redis-cli -p 6379 DEBUG sleep 30 |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
redis1@redis1:~$ redis-sentinel sentinel.conf 3450:X 27 Nov 2022 12:23:13.821 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 3450:X 27 Nov 2022 12:23:13.821 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=3450, just started 3450:X 27 Nov 2022 12:23:13.821 # Configuration loaded 3450:X 27 Nov 2022 12:23:13.822 * Increased maximum number of open files to 10032 (it was originally set to 1024). 3450:X 27 Nov 2022 12:23:13.823 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 7.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 5000 | `-._ `._ / _.-' | PID: 3450 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 3450:X 27 Nov 2022 12:23:13.826 # Sentinel ID is 9a7a4e3269530706fd1002c3018a3f0c525c25d6 3450:X 27 Nov 2022 12:23:13.826 # +monitor master mymaster 192.168.2.104 6379 quorum 2 3450:X 27 Nov 2022 12:23:13.829 * +slave slave 192.168.2.139:6379 192.168.2.139 6379 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:23:13.832 * Sentinel new configuration saved on disk 3450:X 27 Nov 2022 12:23:13.832 * +slave slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:23:13.836 * Sentinel new configuration saved on disk 3450:X 27 Nov 2022 12:23:18.844 # +sdown sentinel 095fc33d13e733d7f974faf0c2b2e6aba015fc3f 192.168.2.139 5000 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:23:18.844 # +sdown sentinel ea6cbec52f9c04c6c325414a05562a69b503129c 192.168.2.166 5000 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:23:25.263 # -sdown sentinel ea6cbec52f9c04c6c325414a05562a69b503129c 192.168.2.166 5000 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:23:37.694 # -sdown sentinel 095fc33d13e733d7f974faf0c2b2e6aba015fc3f 192.168.2.139 5000 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:26:23.114 # +sdown master mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:26:23.191 # +odown master mymaster 192.168.2.104 6379 #quorum 3/2 3450:X 27 Nov 2022 12:26:23.192 # +new-epoch 4 <strong>3450:X 27 Nov 2022 12:26:23.192 # +try-failover master mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:26:23.197 * Sentinel new configuration saved on disk 3450:X 27 Nov 2022 12:26:23.197 # +vote-for-leader 9a7a4e3269530706fd1002c3018a3f0c525c25d6 4 3450:X 27 Nov 2022 12:26:23.200 # ea6cbec52f9c04c6c325414a05562a69b503129c voted for ea6cbec52f9c04c6c325414a05562a69b503129c 4 3450:X 27 Nov 2022 12:26:23.204 # 095fc33d13e733d7f974faf0c2b2e6aba015fc3f voted for ea6cbec52f9c04c6c325414a05562a69b503129c 4 3450:X 27 Nov 2022 12:26:24.346 # +config-update-from sentinel ea6cbec52f9c04c6c325414a05562a69b503129c 192.168.2.166 5000 @ mymaster 192.168.2.104 6379 3450:X 27 Nov 2022 12:26:24.346 # +switch-master mymaster 192.168.2.104 6379 192.168.2.139 6379 3450:X 27 Nov 2022 12:26:24.348 * +slave slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.139 6379 3450:X 27 Nov 2022 12:26:24.349 * +slave slave 192.168.2.104:6379 192.168.2.104 6379 @ mymaster 192.168.2.139 6379 3450:X 27 Nov 2022 12:26:24.357 * Sentinel new configuration saved on disk 3450:X 27 Nov 2022 12:26:29.371 # +sdown slave 192.168.2.104:6379 192.168.2.104 6379 @ mymaster 192.168.2.139 6379</strong> |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
redis2@redis2:~$ redis-sentinel sentinel.conf 3098:X 27 Nov 2022 12:23:24.963 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 3098:X 27 Nov 2022 12:23:24.963 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=3098, just started 3098:X 27 Nov 2022 12:23:24.963 # Configuration loaded 3098:X 27 Nov 2022 12:23:24.965 * Increased maximum number of open files to 10032 (it was originally set to 1024). 3098:X 27 Nov 2022 12:23:24.965 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 7.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 5000 | `-._ `._ / _.-' | PID: 3098 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 3098:X 27 Nov 2022 12:23:24.968 # Sentinel ID is ea6cbec52f9c04c6c325414a05562a69b503129c 3098:X 27 Nov 2022 12:23:24.968 # +monitor master mymaster 192.168.2.104 6379 quorum 2 3098:X 27 Nov 2022 12:23:24.972 * +slave slave 192.168.2.139:6379 192.168.2.139 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:23:24.975 * Sentinel new configuration saved on disk 3098:X 27 Nov 2022 12:23:24.975 * +slave slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:23:24.979 * Sentinel new configuration saved on disk 3098:X 27 Nov 2022 12:23:29.998 # +sdown sentinel 095fc33d13e733d7f974faf0c2b2e6aba015fc3f 192.168.2.139 5000 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:23:37.449 # -sdown sentinel 095fc33d13e733d7f974faf0c2b2e6aba015fc3f 192.168.2.139 5000 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.136 # +sdown master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.237 # +odown master mymaster 192.168.2.104 6379 #quorum 2/2 3098:X 27 Nov 2022 12:26:23.237 # +new-epoch 4 3098:X 27 Nov 2022 12:26:23.237 # +try-failover master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.242 * Sentinel new configuration saved on disk 3098:X 27 Nov 2022 12:26:23.242 # +vote-for-leader ea6cbec52f9c04c6c325414a05562a69b503129c 4 3098:X 27 Nov 2022 12:26:23.248 # 9a7a4e3269530706fd1002c3018a3f0c525c25d6 voted for 9a7a4e3269530706fd1002c3018a3f0c525c25d6 4 3098:X 27 Nov 2022 12:26:23.253 # 095fc33d13e733d7f974faf0c2b2e6aba015fc3f voted for ea6cbec52f9c04c6c325414a05562a69b503129c 4 3098:X 27 Nov 2022 12:26:23.333 # +elected-leader master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.333 # +failover-state-select-slave master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.417 # +selected-slave slave 192.168.2.139:6379 192.168.2.139 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.417 * +failover-state-send-slaveof-noone slave 192.168.2.139:6379 192.168.2.139 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:23.483 * +failover-state-wait-promotion slave 192.168.2.139:6379 192.168.2.139 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:24.307 * Sentinel new configuration saved on disk <strong>3098:X 27 Nov 2022 12:26:24.307 # +promoted-slave slave 192.168.2.139:6379 192.168.2.139 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:24.307 # +failover-state-reconf-slaves master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:24.394 * +slave-reconf-sent slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:25.322 * +slave-reconf-inprog slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:25.323 * +slave-reconf-done slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:25.399 # -odown master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:25.399 # +failover-end master mymaster 192.168.2.104 6379 3098:X 27 Nov 2022 12:26:25.399 # +switch-master mymaster 192.168.2.104 6379 192.168.2.139 6379 3098:X 27 Nov 2022 12:26:25.400 * +slave slave 192.168.2.166:6379 192.168.2.166 6379 @ mymaster 192.168.2.139 6379 3098:X 27 Nov 2022 12:26:25.400 * +slave slave 192.168.2.104:6379 192.168.2.104 6379 @ mymaster 192.168.2.139 6379 3098:X 27 Nov 2022 12:26:25.403 * Sentinel new configuration saved on disk 3098:X 27 Nov 2022 12:26:30.447 # +sdown slave 192.168.2.104:6379 192.168.2.104 6379 @ mymaster 192.168.2.139 6379 3098:X 27 Nov 2022 12:29:48.267 # -sdown slave 192.168.2.104:6379 192.168.2.104 6379 @ mymaster 192.168.2.139 6379 3098:X 27 Nov 2022 12:29:58.242 * +convert-to-slave slave 192.168.2.104:6379 192.168.2.104 6379 @ mymaster 192.168.2.139 6379</strong> |
这时候再看master已经变成了redis3
0 1 2 3 4 |
redis1@redis1:~$ redis-cli -p 5000 127.0.0.1:5000> SENTINEL get-master-addr-by-name mymaster 1) "192.168.2.139" 2) "6379" |
去redis3改点数据,去redis2看变不变
0 1 2 3 4 5 6 7 8 9 10 11 12 |
redis1@redis1:~$ redis-cli -h 192.168.2.139 -p 6379 192.168.2.139:6379> get test "1" 192.168.2.139:6379> set test 2 OK 192.168.2.139:6379> get test "2" 192.168.2.139:6379> redis1@redis1:~$ redis-cli -h 192.168.2.166 -p 6379 192.168.2.166:6379> get test "2" 192.168.2.166:6379> |
显然redis3改的值可以从redis2读到,说明复制正常,继续尝试恢复redis1看看能不能读到test=2
0 1 2 3 4 |
redis1@redis1:~$ redis-cli -p 6379 127.0.0.1:6379> get test "2" 127.0.0.1:6379> |
也可以读到,高可用集群搭建完成
附用到的命令
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
https://mirrors.tuna.tsinghua.edu.cn redis1 192.168.2.104 ssh redis1@192.168.2.104 redis2 192.168.2.166 ssh redis2@192.168.2.166 redis3 192.168.2.139 ssh redis3@192.168.2.139 6379 sudo vim /etc/redis/redis.conf #config all nodes bind * -::1 protected-mode no #only in redis2 and redis3 replicaof 192.168.2.104 6379 sudo service redis-server restart vim sentinel.conf port 5000 sentinel monitor mymaster 192.168.2.104 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 redis-sentinel sentinel.conf redis-cli -p 5000 redis-cli -h 192.168.2.166 -p 5000 redis-cli -h 192.168.2.139 -p 5000 SENTINEL master mymaster SENTINEL get-master-addr-by-name mymaster SENTINEL replicas mymaster SENTINEL sentinels mymaster redis-cli -p 6379 redis-cli -h 192.168.2.166 -p 6379 redis-cli -h 192.168.2.139 -p 6379 |
参考
https://redis.io/docs/
https://redis.io/docs/getting-started/installation/install-redis-on-linux/
https://redis.io/docs/management/replication/
https://redis.io/docs/management/sentinel/
0 Comments