集群模式3主3从。
在三台机器上分别启动2个redis实例。步骤可参考以前的文章。
之前配置集群使用的是ruby方案。安装ruby比较麻烦,我不喜欢。其实是可以直接使用redis自己的指令完成的。虽然步骤多一点,(也不多,只要三步)。但是redis的原生方案更放心。
redis的5.x版本更方便了,只需要一个命令,见后续文章。
cluster meet
第一步将所有redis实例之间建立联系。
随便选择一个redis实例执行
# redis-cli -h 127.0.0.1 -p 6379 -a xxxxxx
127.0.0.1:6379> cluster meet 10.250.8.10 6380
OK
127.0.0.1:6379> cluster meet 10.250.8.11 6379
OK
127.0.0.1:6379> cluster meet 10.250.8.11 6380
OK
127.0.0.1:6379> cluster meet 10.250.8.12 6379
OK
127.0.0.1:6379> cluster meet 10.250.8.12 6380
OK
查看一下状态
127.0.0.1:6379> cluster nodes
23c4a4663cac558243226819ced0ae9f7474d661 10.250.8.10:6379@16379 myself,master - 0 1671593497000 3 connected
07813601b89542172c1eda310f4d175ebf4b145b 10.250.8.11:6379@16379 master - 0 1671593498000 2 connected
a05e7b4314147e86c674dc7203ddce9be142f2e3 10.250.8.12:6380@16380 master - 0 1671593497000 5 connected
0057e3eac5dca123de6a0febeaa55a4f589d5cfc 10.250.8.10:6380@16380 master - 0 1671593497117 1 connected
f54487779ead35e41d878aa876ea456576f8da60 10.250.8.11:6380@16380 master - 0 1671593499120 0 connected
a79a1d40bc9f6830f62bb98e2936209d24925593 10.250.8.12:6379@16379 master - 0 1671593498119 4 connected
主从配置
现在6个实例全部加入集群,但是主从关系还没有分配。
分配主从的时候要注意,主节点和对应的从节点应该部署在不同的服务器上,以取得高可用性。
图片分别登录3个备节点,执行replicate指令
# redis-cli -h 10.250.8.10 -p 6380 -a xxxxxxx cluster replicate a79a1d40bc9f6830f62bb98e2936209d24925593
OK
# redis-cli -h 10.250.8.11 -p 6380 -a xxxxxxx cluster replicate 23c4a4663cac558243226819ced0ae9f7474d661
OK
# redis-cli -h 10.250.8.12 -p 6380 -a xxxxxxx cluster replicate 07813601b89542172c1eda310f4d175ebf4b145b
OK
查看节点状态
# redis-cli -h 10.250.8.12 -p 6380 -a xxxxxx cluster nodes
a79a1d40bc9f6830f62bb98e2936209d24925593 10.250.8.12:6379@16379 master - 0 1671601584000 4 connected
f54487779ead35e41d878aa876ea456576f8da60 10.250.8.11:6380@16380 slave 23c4a4663cac558243226819ced0ae9f7474d661 0 1671601585000 3 connected
a05e7b4314147e86c674dc7203ddce9be142f2e3 10.250.8.12:6380@16380 myself,slave 07813601b89542172c1eda310f4d175ebf4b145b 0 1671601584000 5 connected
0057e3eac5dca123de6a0febeaa55a4f589d5cfc 10.250.8.10:6380@16380 slave a79a1d40bc9f6830f62bb98e2936209d24925593 0 1671601587266 4 connected
07813601b89542172c1eda310f4d175ebf4b145b 10.250.8.11:6379@16379 master - 0 1671601586264 2 connected
23c4a4663cac558243226819ced0ae9f7474d661 10.250.8.10:6379@16379 master - 0 1671601586000 3 connected
分配slot
redis集群共有16384个slot。我们平均分。序号是从0开始,到16383结束。必须全部分配,缺一个都不行。
分配slot只在master节点上执行。
# redis-cli -h 10.250.8.10 -p 6379 -a wW1tUY8n cluster addslots {0..5461}
OK
# redis-cli -h 10.250.8.11 -p 6379 -a wW1tUY8n cluster addslots {5462..10922}
OK
# redis-cli -h 10.250.8.12 -p 6379 -a wW1tUY8n cluster addslots {10923..16383}
OK
查看集群状态
# redis-cli -h 10.250.8.12 -p 6380 -a xxxxxx cluster info
cluster_state:ok
cluster_slots_assigned:10923
cluster_slots_ok:10923
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:2
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:8493
cluster_stats_messages_pong_sent:8077
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:16573
cluster_stats_messages_ping_received:8075
cluster_stats_messages_pong_received:8496
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:16573
完成了
网友评论