一、环境规划

#准备六台主机,地址如下
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角色存在

基于redis5的redis cluster部署的更多相关文章

  1. window下使用Redis Cluster部署Redis集群

    日常的项目很多时候都需要用到缓存.redis算是一个比较好的选择.一般情况下做一个主从就可以满足一些比较小的项目需要.在一些并发量比较大的项目可能就需要用到集群了,redis在Windows下做集群可 ...

  2. Redis Cluster部署、管理和测试

    背景: Redis 3.0之后支持了Cluster,大大增强了Redis水平扩展的能力.Redis Cluster是Redis官方的集群实现方案,在此之前已经有第三方Redis集群解决方案,如Twen ...

  3. redis cluster 部署过程

    一, 特点 高性能: 1.在多分片节点中,将16384个槽位,均匀分布到多个分片节点中 2.存数据时,将key做crc16(key),然后和16384进行取模,得出槽位值(0-16383之间) 3.根 ...

  4. 【原创】强撸基于 .NET 的 Redis Cluster 集群访问组件

    Hello 大家好,我是TANZAME,我们又见面了.今天我们来聊聊怎么手撸一个 Redis Cluster 集群客户端,纯手工有干货,您细品. 随着业务增长,线上环境的QPS暴增,自然而然将当前的单 ...

  5. 【docker】【redis】2.docker上设置redis集群---Redis Cluster部署【集群服务】【解决在docker中redis启动后,状态为Restarting,日志报错:Configured to not listen anywhere, exiting.问题】【Waiting for the cluster to join...问题】

    参考地址:https://www.cnblogs.com/zhoujinyi/p/6477133.html https://www.cnblogs.com/cxbhakim/p/9151720.htm ...

  6. 在 K8S 中快速部署 Redis Cluster & Redisinsight

    Redis Cluster 部署 使用 Bitnami helm chart 在 K8S redis 命名空间中一键部署 Redis cluster . helm repo add bitnami h ...

  7. Redis Cluster 原理相关说明

    背景 之前写的 Redis Cluster部署.管理和测试 和 Redis 5.0 redis-cli --cluster help说明 已经比较详细的介绍了如何安装和维护Cluster.但关于Clu ...

  8. 基于redis5的session共享:【redis 5.x集群应用研究】

    基于springsession构建一个session共享的模块. 这里,基于redis的集群(Redis-5.0.3版本),为了解决整个物联网平台的各个子系统之间共享session需求,且方便各个子系 ...

  9. 如何用docker部署redis cluster

    前言 由于本人是个docker控,不喜欢安装各种环境,而且安装redis-trib也有点繁琐,索性用docker来做redis cluster. 本文用的是伪集群,真正的集群放到不同的机器即可.端口是 ...

随机推荐

  1. 发布 vscode 插件 Cnblogs Client For VSCode 预览版

    为了方便大家使用 vscode 发布博文,我们做了一个小插件,今天发布预览版,欢迎大家试用并反馈问题与建议. 插件的英文名称是 Cnblogs Client For VSCode,简称是 vscode ...

  2. CSS基础-5 伪类

    一.伪类 我们以a标签为例 伪类标签分为4类     1. 设置超链接默认的样式 a:link {属性:值;.....} 或者 a { 属性: 值;}           推荐使用这种方式     2 ...

  3. MYSQL实现上一条下一条功能

    select id from(select *, (@i:=@i+1) as rownum from pre_bet_zhibo,(select @i:=0) as itwhere link_cone ...

  4. vue中使用两个window.onresize问题解决

    在vue开发中,因为引用的父组件和子组件都使用了window.onresize以至于一个window.onresize失效.找了下解决方案,可以采用下面的方式写就可以了. window.onresiz ...

  5. SYCOJ1717负二进制

    题目-负二进制 (shiyancang.cn) 进制的实质在于对于进制数的选择,选择不满足的填入当前的位置,然后除掉,继续开始选择.但是对于本题,是负数,但是进制上的数字为正数,所以就要调整,借位,因 ...

  6. EgLine V0.3—LVGL官方拖拽式UI编辑工具(可导出代码)

    ** EdgeLine ** 是LVGL官方团队退出的一款拖拽式UI编辑工具,现在还处于测试间断,目前最新版本为v0.3,已经可导出代码. 注意: 使用该软件需要注册lvgl账号,这一步可能需要代理 ...

  7. 输出2到n之间的全部素数

    本题要求输出2到n之间的全部素数,每行输出10个.素数就是只能被1和自身整除的正整数.注意:1不是素数,2是素数. 输入格式: 输入在一行中给出一个长整型范围内的整数. 输出格式: 输出素数,每个数占 ...

  8. RDA5807M开发指南 & 开源库函数

    文档标识符:RDA5807M_T-D-P16 作者:DLHC 最后修改日期:2022.1.14 最后修改内容:修改.添加内容 发布状态:已发布 本文链接:https://www.cnblogs.com ...

  9. IPOPT安装

    1.安装工具coinbrew 打开网页,找到以下网址 将网站中的内容全部复制到自己创建的coinbrew文件中,并且赋予权限 chmod u+x coinbrew 或者执行 git clone htt ...

  10. [STM32F4xx 学习] SPI与nRF24L01+的应用

    前面已经总结过STM32Fxx的特点和传输过程,下面以nRF24L01+ 2.4GHz无线收发器为例,来说明如何使用SPI. 一.nRF24L01+ 2.4GHz无线收发器的介绍 1. 主要特性 全球 ...