一、环境规划

#准备六台主机,地址如下
10.0.0.8 ---> master1
10.0.0.18 ---> master2
10.0.0.28 ---> master3
10.0.0.38 ---> slave1
10.0.0.48 ---> slave2
10.0.0.58 ---> slave3

二、配置redis cluster

2.1 部署redis

##安装redis
# dnf -y install redis
# redis-server --version
Redis server v=5.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=9529b692c0384fb7 ##修改配置文件
# sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' \
-e '/# masterauth/c masterauth 123456' \
-e '/# requirepass/c requirepass 123456' \
-e '/# cluster-enabled yes/c cluster-enabled yes' \
-e '/# cluster-config-file nodes-6379.conf/c cluster-config-file nodes-6379.conf' \
-e '/# cluster-require-full-coverage/c cluster-require-full-coverage no' \
/etc/redis.conf ##验证Redis服务状态
# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 0.0.0.0:16379 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*

2.2 配置reis cluster

##redis-cli --cluster-replicas 1 表示每个master对应一个slave节点
# redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.38:6379 to 10.0.0.8:6379
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: 7a4443e815ca6009d8e25e5f84d2f5feee3c007a 10.0.0.8:6379 ##带M的为master
slots:[0-5460] (5461 slots) master ##当前master的槽位起始和结束位
M: 2f41d6b0a3b8957755b67b2dccfb764dee57f123 10.0.0.18:6379
slots:[5461-10922] (5462 slots) master
M: 4e0eb42d503974e00962fa1d0214593720327d4a 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
S: 5308ffa51d731096d8c9e86d2f3976cb94fa7fa8 10.0.0.38:6379 ##带S的slave
replicates 7a4443e815ca6009d8e25e5f84d2f5feee3c007a
S: 67eb5b5226f01b6dde9145fe4063b1f4849fe921 10.0.0.48:6379
replicates 2f41d6b0a3b8957755b67b2dccfb764dee57f123
S: 128518070585eb054210b322b8a6f910cc2849f8 10.0.0.58:6379
replicates 4e0eb42d503974e00962fa1d0214593720327d4a
Can I set the above configuration? (type 'yes' to accept): yes ##输入yes自动创建集群
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
......
>>> Performing Cluster Check (using node 10.0.0.8:6379)
M: 7a4443e815ca6009d8e25e5f84d2f5feee3c007a 10.0.0.8:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 67eb5b5226f01b6dde9145fe4063b1f4849fe921 10.0.0.48:6379
slots: (0 slots) slave
replicates 2f41d6b0a3b8957755b67b2dccfb764dee57f123
M: 2f41d6b0a3b8957755b67b2dccfb764dee57f123 10.0.0.18:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 128518070585eb054210b322b8a6f910cc2849f8 10.0.0.58:6379
slots: (0 slots) slave
replicates 4e0eb42d503974e00962fa1d0214593720327d4a
S: 5308ffa51d731096d8c9e86d2f3976cb94fa7fa8 10.0.0.38:6379
slots: (0 slots) slave
replicates 7a4443e815ca6009d8e25e5f84d2f5feee3c007a
M: 4e0eb42d503974e00962fa1d0214593720327d4a 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration. ##所有节点槽位分配完成
>>> Check for open slots... ##检查打开的槽位
>>> Check slots coverage... ##检查插槽覆盖范围
[OK] All 16384 slots covered. #所有槽位(16384个)分配完成 ###观察以上结果,可以看到3组master/slave
master:10.0.0.8---slave:10.0.0.38
master:10.0.0.18---slave:10.0.0.48
master:10.0.0.28---slave:10.0.0.58

2.3 查看主从状态、集群状态

# redis-cli -a 123456 -h 10.0.0.18 --no-auth-warning cluster nodes

2.4 模拟故障

##模拟master1(IP:10.0.0.8)节点出故障
# redis-cli -a 123456 shutdown ##关闭master1
# tail -f /var/log/redis/redis.log
2300:C 23 Jan 2022 12:11:41.952 * DB saved on disk
2300:C 23 Jan 2022 12:11:41.952 * RDB: 4 MB of memory used by copy-on-write
2296:M 23 Jan 2022 12:11:42.027 * Background saving terminated with success
2296:M 23 Jan 2022 12:11:42.028 * Synchronization with replica 10.0.0.38:6379 succeeded
2296:M 23 Jan 2022 12:11:43.081 # Cluster state changed: ok
2296:M 23 Jan 2022 12:14:45.924 # User requested shutdown...
2296:M 23 Jan 2022 12:14:45.924 * Saving the final RDB snapshot before exiting.
2296:M 23 Jan 2022 12:14:45.927 * DB saved on disk
2296:M 23 Jan 2022 12:14:45.927 * Removing the pid file.
2296:M 23 Jan 2022 12:14:45.927 # Redis is now ready to exit, bye bye... ##查看角色变化
# redis-cli -a 123456 -h 10.0.0.38 --no-auth-warning cluster nodes
4e0eb42d503974e00962fa1d0214593720327d4a 10.0.0.28:6379@16379 master - 0 1636914427274 3 connected 10923-16383
2f41d6b0a3b8957755b67b2dccfb764dee57f123 10.0.0.18:6379@16379 master - 0 1636914429452 2 connected 5461-10922
128518070585eb054210b322b8a6f910cc2849f8 10.0.0.58:6379@16379 slave 4e0eb42d503974e00962fa1d0214593720327d4a 0 1636914423993 6 connected
67eb5b5226f01b6dde9145fe4063b1f4849fe921 10.0.0.48:6379@16379 slave 2f41d6b0a3b8957755b67b2dccfb764dee57f123 0 1636914430542 5 connected
7a4443e815ca6009d8e25e5f84d2f5feee3c007a 10.0.0.8:6379@16379 master,fail - 1636913491097 1636913490340 1 disconnected
5308ffa51d731096d8c9e86d2f3976cb94fa7fa8 10.0.0.38:6379@16379 myself,master - 0 0 8 connected 0-5460 ##查看slave1的日志
# tail -f /var/log/redis/redis.log
2050:S 15 Nov 2021 02:11:47.767 * FAIL message received from 2f41d6b0a3b8957755b67b2dccfb764dee57f123 about 7a4443e815ca6009d8e25e5f84d2f5feee3c007a
2050:S 15 Nov 2021 02:11:47.867 # Start of election delayed for 795 milliseconds (rank #0, offset 238).
2050:S 15 Nov 2021 02:11:48.733 * Connecting to MASTER 10.0.0.8:6379
2050:S 15 Nov 2021 02:11:48.734 * MASTER <-> REPLICA sync started
2050:S 15 Nov 2021 02:11:48.734 # Starting a failover election for epoch 8.
2050:S 15 Nov 2021 02:11:48.736 # Error condition on socket for SYNC: Connection refused
2050:S 15 Nov 2021 02:11:48.738 # Failover election won: I'm the new master. ##已经变成新的master
2050:S 15 Nov 2021 02:11:48.738 # configEpoch set to 8 after successful failover
2050:M 15 Nov 2021 02:11:48.738 # Setting secondary replication ID to 68e9f4f70b4fec8086c0717b8981d5adca478be0, valid up to offset: 239. New replication ID is 22f817fbe0ef49a3c0ef26e5a34ccd1f38743bde
2050:M 15 Nov 2021 02:11:48.738 * Discarding previously cached master state.

2.5 恢复故障

# systemctl start redis

# redis-cli  -a 123456 -h 10.0.38 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.8,port=6379,state=online,offset=644,lag=1
master_replid:22f817fbe0ef49a3c0ef26e5a34ccd1f38743bde
master_replid2:68e9f4f70b4fec8086c0717b8981d5adca478be0
master_repl_offset:658
second_repl_offset:239
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:658 # redis-cli -a 123456 -h 10.0.8 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.38
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:826
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:22f817fbe0ef49a3c0ef26e5a34ccd1f38743bde
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:826
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:239
repl_backlog_histlen:588 ##查看最终master/slave关系
# redis-cli -a 123456 -h 10.0.0.8 --no-auth-warning cluster nodes
[root@localhost ~]#redis-cli -a 123456 -h 10.0.0.8 --no-auth-warning cluster nodes
4e0eb42d503974e00962fa1d0214593720327d4a 10.0.0.28:6379@16379 master - 0 1642914346272 3 connected 10923-16383
67eb5b5226f01b6dde9145fe4063b1f4849fe921 10.0.0.48:6379@16379 slave 2f41d6b0a3b8957755b67b2dccfb764dee57f123 0 1642914339763 5 connected
2f41d6b0a3b8957755b67b2dccfb764dee57f123 10.0.0.18:6379@16379 master - 0 1642914344105 2 connected 5461-10922
7a4443e815ca6009d8e25e5f84d2f5feee3c007a 10.0.0.8:6379@16379 myself,slave 5308ffa51d731096d8c9e86d2f3976cb94fa7fa8 0 1642913710880 1 connected
128518070585eb054210b322b8a6f910cc2849f8 10.0.0.58:6379@16379 slave 4e0eb42d503974e00962fa1d0214593720327d4a 0 1642914345195 6 connected
5308ffa51d731096d8c9e86d2f3976cb94fa7fa8 10.0.0.38:6379@16379 master - 0 1642914341929 8 connected 0-5460

可以看出,master1恢复故障并没有重新获取master角色,而是以slave角色存在

最新文章

  1. 三道简单的前端HTML/CSS题目
  2. C#网络编程之---TCP协议的同步通信(二)
  3. CodeForces 716B Complete the Word
  4. C语言 指针与字符串
  5. &lt;mvc:annotation-driven /&gt;
  6. Android开源项目发现--- 效率开发工具篇(持续更新)
  7. Zipf’s Law
  8. 201521145048 《Java程序设计》第3周学习总结
  9. WOW.js – 让页面滚动更有趣
  10. lombook安装以及在eclipse和idea上配置
  11. springboot快速开发(简单web)
  12. 【leetcode】475. Heaters
  13. jmeter操作练习
  14. SVN多分支开发模式V1.0.1
  15. Python基础知识:字典
  16. RabbitMQ 优先级队列-为队列赋权
  17. NO.8:绝不在构造或者析构过程中调用virtual函数
  18. toFixed方法的bug
  19. 关于Android4.X的Alertdialog对话框
  20. C++的空指针、野指针和指针赋值NULL.md

热门文章

  1. Git 远程分支常用操作
  2. Sentry 企业级数据安全解决方案 - Relay 配置选项
  3. Go语言系列之标准库log
  4. LINUX学习-Mysql集群-多主一从备份
  5. [转]JS正则表达式基础
  6. HDU 1576 A/B (两种解法)
  7. Selenium2+python自动化65-js定位几种方法总结
  8. 【记录一个问题】ndk下使用c++11的condition_variable问题较多
  9. 集合框架-工具类-Arrays-asList方法
  10. JDBC 连接DRUID 连接池!