第二十一周--作业
1、简述redis集群的实现原理
redis cluster 将所有的数据划分到16384个卡槽(slot)中,每个节点负责存储一部分卡槽,且槽位的信息也存储在每个服务节点中。 当redis cluster 的客户端来连接集群时,它也会得到一份集群的槽位配置信息并将其缓存在客户端本地。这样当客户端要查找某个 key 时,可以直接定位到目标节点。同时因为槽位的信息可能会存在客户端与服务器不一致的情况,还需要纠正机制来实现槽位信息的校验调整。 |
2、基于redis5的redis cluster部署
#创建 redis cluster集群的环境准备 每个redis 节点采用相同的redis版本、相同的密码、硬件配置 所有redis服务器必须没有任何数据 准备六台主机: 10.0.0.7 Redis-6.2.4 node1 10.0.0.17 Redis-6.2.4 node2 10.0.0.27 Redis-6.2.4 node3 10.0.0.37 Redis-6.2.4 node4 10.0.0.47 Redis-6.2.4 node5 10.0.0.57 Redis-6.2.4 node6 #启用 redis cluster 配置 sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/ccluster-require-full-coverage no' /apps/redis/etc/redis.conf systemctl daemon-reload systemctl restart redis.service #创建集群 redis-cli -a 123456 --cluster create 10.0.0.7:6379 10.0.0.17:6379 10.0.0.27:6379 10.0.0.37:6379 10.0.0.47:6379 10.0.0.57:6379 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> 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.47:6379 to 10.0.0.7:6379 Adding replica 10.0.0.57:6379 to 10.0.0.17:6379 Adding replica 10.0.0.37:6379 to 10.0.0.27:6379 M: 494700d188e9462f1199b97929b58338c97d1986 10.0.0.7:6379 #带M的为master slots:[0-5460] (5461 slots) master #当前master的槽位起始和结束位 M: 4ac01ebc72ebfed8e9bca715866553f9b2d48712 10.0.0.17:6379 slots:[5461-10922] (5462 slots) master M: c60ce314b919dcaf4c21d147f3bd808295ac9219 10.0.0.27:6379 slots:[10923-16383] (5461 slots) master S: b3d69d2214f157d435de6afbc5ddc0115624e658 10.0.0.37:6379 #带S的slave replicates c60ce314b919dcaf4c21d147f3bd808295ac9219 S: 6f3bd4e7b531e977a40f1f40820df9f85cb2e6c0 10.0.0.47:6379 replicates 494700d188e9462f1199b97929b58338c97d1986 S: d0e5a0a4b49a804ad50583759c080afb42634ea9 10.0.0.57:6379 replicates 4ac01ebc72ebfed8e9bca715866553f9b2d48712 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.7:6379) M: 494700d188e9462f1199b97929b58338c97d1986 10.0.0.7:6379 slots:[0-5460] (5461 slots) master #已经分配的槽位 1 additional replica(s) #分配了一个slave S: d0e5a0a4b49a804ad50583759c080afb42634ea9 10.0.0.57:6379 slots: (0 slots) slave #slave没有分配槽位 replicates 4ac01ebc72ebfed8e9bca715866553f9b2d48712 #对应的master的10.0.0.17的ID S: 6f3bd4e7b531e977a40f1f40820df9f85cb2e6c0 10.0.0.47:6379 slots: (0 slots) slave replicates 494700d188e9462f1199b97929b58338c97d1986 #对应的master的10.0.0.7的ID S: b3d69d2214f157d435de6afbc5ddc0115624e658 10.0.0.37:6379 slots: (0 slots) slave replicates c60ce314b919dcaf4c21d147f3bd808295ac9219 #对应的master的10.0.0.27的ID M: c60ce314b919dcaf4c21d147f3bd808295ac9219 10.0.0.27:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 4ac01ebc72ebfed8e9bca715866553f9b2d48712 10.0.0.17:6379 slots:[5461-10922] (5462 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.7---slave:10.0.0.47 master:10.0.0.17---slave:10.0.0.57 master:10.0.0.27---slave:10.0.0.37 #查看集群node对应关系 redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. d0e5a0a4b49a804ad50583759c080afb42634ea9 10.0.0.57:6379@16379 slave 4ac01ebc72ebfed8e9bca715866553f9b2d48712 0 1652335386000 2 connected 494700d188e9462f1199b97929b58338c97d1986 10.0.0.7:6379@16379 myself,master - 0 1652335386000 1 connected 0-5460 6f3bd4e7b531e977a40f1f40820df9f85cb2e6c0 10.0.0.47:6379@16379 slave 494700d188e9462f1199b97929b58338c97d1986 0 1652335387000 1 connected b3d69d2214f157d435de6afbc5ddc0115624e658 10.0.0.37:6379@16379 slave c60ce314b919dcaf4c21d147f3bd808295ac9219 0 1652335387608 3 connected c60ce314b919dcaf4c21d147f3bd808295ac9219 10.0.0.27:6379@16379 master - 0 1652335386573 3 connected 10923-16383 4ac01ebc72ebfed8e9bca715866553f9b2d48712 10.0.0.17:6379@16379 master - 0 1652335388628 2 connected 5461-10922 #模拟master故障,对应的slave节点自动提升为新master redis-cli -a 123456 -c 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.47,port=6379,state=online,offset=7770,lag=0 master_failover_state:no-failover master_replid:2604b6e0d6c3ad7b448104bfebf3c22e1a69e5a1 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:7770 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:7770 #10.0.0.47自动提升为新的master [root@node5 ~]#redis-cli -a 123456 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:0 master_failover_state:no-failover master_replid:0b4c2c5ac6df32f3e6fff33cea5e60240da11bd2 master_replid2:2604b6e0d6c3ad7b448104bfebf3c22e1a69e5a1 master_repl_offset:8008 second_repl_offset:8009 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:8008 #恢复故障节点node1自动成为slave节点 systemctl start redis #查看集群状态文件,可以查看node1自动成为slave节点 [root@node1 ~]#cat /apps/redis/data/nodes-6379.conf b3d69d2214f157d435de6afbc5ddc0115624e658 10.0.0.37:6379@16379 slave c60ce314b919dcaf4c21d147f3bd808295ac9219 0 1652340965102 3 connected 494700d188e9462f1199b97929b58338c97d1986 10.0.0.7:6379@16379 myself,slave 6f3bd4e7b531e977a40f1f40820df9f85cb2e6c0 0 1652340965085 7 connected 6f3bd4e7b531e977a40f1f40820df9f85cb2e6c0 10.0.0.47:6379@16379 master - 0 1652340965105 7 connected 0-5460 4ac01ebc72ebfed8e9bca715866553f9b2d48712 10.0.0.17:6379@16379 master - 0 1652340965085 2 connected 5461-10922 d0e5a0a4b49a804ad50583759c080afb42634ea9 10.0.0.57:6379@16379 slave 4ac01ebc72ebfed8e9bca715866553f9b2d48712 0 1652340965085 2 connected c60ce314b919dcaf4c21d147f3bd808295ac9219 10.0.0.27:6379@16379 master - 0 1652340965085 3 connected 10923-16383 vars currentEpoch 7 lastVoteEpoch 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通