redsi主从配置
下面是基于redis主从的环境部署哨兵模式
1、在配置好redis主从后,添加哨兵模式需要在sentinel.conf文件添加一条命令(主从节点都需要添加)
mymaster
是主节点的别名。192.168.25.129
是主节点的 IP。6379
是主节点的端口。2
是仲裁值,表示至少需要 2 个 Sentinel 同意,才认为主节点确实已经宕机。
sentinel monitor mymaster 192.168.25.129 6379 2
2、优先启动主节点的sentinel (前台启动可以看启动日志,在启动从节点的俩个sentinel )
[root@k8s-129 conf]# redis-sentinel sentinel.conf
3、在截图中可以看到我们Sentinel 检查到节点
4、登录查看当前的信息 主节点还是129
[root@k8s-129 redis]# redis-cli -p 26379
127.0.0.1:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.25.129"
5) "port"
6) "6379"
7) "runid"
8) "fbfc6ff0bf932f530177f22c786f5639080afdeb"
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) "27"
19) "last-ping-reply"
20) "27"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "3847"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "658667"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26379>
5、验证
断掉主节点redis
[root@k8s-129 redis]# ps -ef|grep redis
root 10313 1 0 14:32 ? 00:01:03 ./bin/redis-server 0.0.0.0:6379
root 44118 12647 0 16:37 pts/1 00:00:00 redis-sentinel *:26379 [sentinel]
root 44284 2327 0 16:38 pts/0 00:00:00 grep --color=auto redis
[root@k8s-129 redis]# kill -9 10313
查看其它俩个从节点的redis日志
开始是一直在刷新主节点是连接失败的,就选择新的节点作为主,在同步数据
6648:S 05 Jul 2024 16:39:13.988 * Connecting to MASTER 192.168.25.129:6379
6648:S 05 Jul 2024 16:39:13.988 * MASTER <-> REPLICA sync started
6648:S 05 Jul 2024 16:39:13.989 # Error condition on socket for SYNC: Connection refused
6648:S 05 Jul 2024 16:39:14.994 * Connecting to MASTER 192.168.25.129:6379
6648:S 05 Jul 2024 16:39:14.995 * MASTER <-> REPLICA sync started
6648:S 05 Jul 2024 16:39:14.995 # Error condition on socket for SYNC: Connection refused
6648:S 05 Jul 2024 16:39:16.002 * Connecting to MASTER 192.168.25.129:6379
6648:S 05 Jul 2024 16:39:16.002 * MASTER <-> REPLICA sync started
6648:S 05 Jul 2024 16:39:16.003 # Error condition on socket for SYNC: Connection refused
6648:S 05 Jul 2024 16:39:17.010 * Connecting to MASTER 192.168.25.129:6379
6648:S 05 Jul 2024 16:39:17.011 * MASTER <-> REPLICA sync started
6648:S 05 Jul 2024 16:39:17.011 # Error condition on socket for SYNC: Connection refused
6648:S 05 Jul 2024 16:39:17.326 * Connecting to MASTER 192.168.25.133:6379
6648:S 05 Jul 2024 16:39:17.326 * MASTER <-> REPLICA sync started
6648:S 05 Jul 2024 16:39:17.327 * REPLICAOF 192.168.25.133:6379 enabled (user request from 'id=9 addr=192.168.25.133:54988 laddr=192.168.25.130:6379 fd=12 name=sentinel-a5450c35-cmd age=1072 idle=0 flags=x db=0 sub=0 psub=0 multi=4 qbuf=345 qbuf-free=32425 argv-mem=4 obl=45 oll=0 omem=0 tot-mem=49804 events=r cmd=exec user=default redir=-1')
6648:S 05 Jul 2024 16:39:17.333 # CONFIG REWRITE executed with success.
6648:M 05 Jul 2024 16:39:47.758 * Discarding previously cached master state.
6648:M 05 Jul 2024 16:39:47.758 # Setting secondary replication ID to 347a6808b596806c52e9c1ae71426e0753cfd5bb, valid up to offset: 227540. New replication ID is b0806c5a43256b7d504988ed75cf2c7be2b94b02
6648:M 05 Jul 2024 16:39:47.758 * MASTER MODE enabled (user request from 'id=16 addr=192.168.25.129:42880 laddr=192.168.25.130:6379 fd=11 name=sentinel-bdae4dd7-cmd age=30 idle=0 flags=x db=0 sub=0 psub=0 multi=4 qbuf=216 qbuf-free=32554 argv-mem=4 obl=45 oll=0 omem=0 tot-mem=49804 events=r cmd=exec user=default redir=-1')
6648:M 05 Jul 2024 16:39:47.762 # CONFIG REWRITE executed with success.
6648:M 05 Jul 2024 16:39:58.572 * Replica 192.168.25.133:6379 asks for synchronization
6648:M 05 Jul 2024 16:39:58.572 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '4a51f82c8459fe7d8c58ca00857e4613387c44e5', my replication IDs are 'b0806c5a43256b7d504988ed75cf2c7be2b94b02' and '347a6808b596806c52e9c1ae71426e0753cfd5bb')
6648:M 05 Jul 2024 16:39:58.572 * Starting BGSAVE for SYNC with target: disk
6648:M 05 Jul 2024 16:39:58.573 * Background saving started by pid 21426
21426:C 05 Jul 2024 16:39:58.576 * DB saved on disk
21426:C 05 Jul 2024 16:39:58.577 * RDB: 0 MB of memory used by copy-on-write
6648:M 05 Jul 2024 16:39:58.605 * Background saving terminated with success
6648:M 05 Jul 2024 16:39:58.605 * Synchronization with replica 192.168.25.133:6379 succeeded
进入redsi查看当前信息,以上就完成了整个环境的故障转移
[root@k8s-130 redis]# redis-cli -p 26379
127.0.0.1:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.25.130"
5) "port"
6) "6379"
7) "runid"
8) "90d9a73b638b602bab2e917c3041bc07294f6a44"
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) "248"
19) "last-ping-reply"
20) "248"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "2532"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "404789"
29) "config-epoch"
30) "2"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26379>
6、在启动原主节点129,它并不会抢占主节点而是以从节点身份加入现有的环境