docker run -d --name redis-node-1 --net host --privileged=true -v /data/redis/share/redis-node-1:/data redis --cluster-enabled yes --appendonly yes --port 6381 docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis --cluster-enabled yes --appendonly yes --port 6382 docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis --cluster-enabled yes --appendonly yes --port 6383 docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis --cluster-enabled yes --appendonly yes --port 6384 docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis --cluster-enabled yes --appendonly yes --port 6385 docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis --cluster-enabled yes --appendonly yes --port 63862.进入redis-node-1并为6台机器构建集群关系
[root@localhost ~]# docker exec -it redis-node-1 bash root@localhost:/data# redis-cli --cluster create 192.168.0.15:6381 192.168.0.15:6382 192.168.0.15:6383 192.168.0.15:6384 192.168.0.15:6385 192.168.0.15:6386 --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 192.168.0.15:6385 to 192.168.0.15:6381 Adding replica 192.168.0.15:6386 to 192.168.0.15:6382 Adding replica 192.168.0.15:6384 to 192.168.0.15:6383 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[0-5460] (5461 slots) master M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[5461-10922] (5462 slots) master M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[10923-16383] (5461 slots) master S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 replicates a85b8afb10190406420a0e566b9785d56aa58403 S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 replicates ffa128aa359c8faf438d783a2650c32ba000f7ec Can I set the above configuration? (type 'yes' to accept): 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 192.168.0.15:6381) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 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
--cluster-replicas 1 表示为每个master创建一个slave节点
3数据读写存储root@localhost:/data# redis-cli -p 6381 127.0.0.1:6381> keys * (empty array) 127.0.0.1:6381> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:235 cluster_stats_messages_pong_sent:248 cluster_stats_messages_sent:483 cluster_stats_messages_ping_received:243 cluster_stats_messages_pong_received:235 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:483 127.0.0.1:6381> cluster nodes a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386@16386 slave ffa128aa359c8faf438d783a2650c32ba000f7ec 0 1643533928811 3 connected c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381@16381 myself,master - 0 1643533930000 1 connected 0-5460 a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382@16382 master - 0 1643533928000 2 connected 5461-10922 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384@16384 slave c8c36e3345fcd03a0b29c21f574eeedaee6397d6 0 1643533930821 1 connected c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385@16385 slave a85b8afb10190406420a0e566b9785d56aa58403 0 1643533929000 2 connected ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383@16383 master - 0 1643533929816 3 connected 10923-16383 ##加入参数-c,优化路由 127.0.0.1:6381> quit root@localhost:/data# resis-cli -p 6381 -c 127.0.0.1:6381> set k1 v1 -> Redirected to slot [12706] located at 192.168.0.15:6383 OK 192.168.0.15:6383> set k2 v2 -> Redirected to slot [449] located at 192.168.0.15:6381 OK 192.168.0.15:6381> set k3 v3 OK 192.168.0.15:6381> set k4 v4 -> Redirected to slot [8455] located at 192.168.0.15:6382 OK 192.168.0.15:6382> get k2 -> Redirected to slot [449] located at 192.168.0.15:6381 "v2"4查看集群信息
###主机6381 的从机 6384 ,主机6382 的从机 6385,主机6383 的从机6386 root@localhost:/data# redis-cli --cluster check 192.168.0.15:6381 192.168.0.15:6381 (c8c36e33...) -> 2 keys | 5461 slots | 1 slaves. 192.168.0.15:6382 (a85b8afb...) -> 1 keys | 5462 slots | 1 slaves. 192.168.0.15:6383 (ffa128aa...) -> 1 keys | 5461 slots | 1 slaves. [OK] 4 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 192.168.0.15:6381) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 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.5容错迁移切换
#停掉node-1 [root@localhost ~]# docker stop redis-node-1 #进入6832 [root@localhost ~]# docker exec -it 181ac954c7d5 bash #查看集群信息发现6384变为主机 root@localhost:/data# redis-cli --cluster check 192.168.0.15:6382 Could not connect to Redis at 192.168.0.15:6381: Connection refused 192.168.0.15:6382 (a85b8afb...) -> 1 keys | 5462 slots | 1 slaves. 192.168.0.15:6383 (ffa128aa...) -> 1 keys | 5461 slots | 1 slaves. 192.168.0.15:6384 (3464284e...) -> 2 keys | 5461 slots | 0 slaves. [OK] 4 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 192.168.0.15:6382) M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots:[0-5460] (5461 slots) master S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. #重启node-1 [root@localhost ~]# docker start 406dd10503cb #查看集群信息发现6381变为6384的从机 root@localhost:/data# redis-cli --cluster check 192.168.0.15:6382 192.168.0.15:6382 (a85b8afb...) -> 1 keys | 5462 slots | 1 slaves. 192.168.0.15:6383 (ffa128aa...) -> 1 keys | 5461 slots | 1 slaves. 192.168.0.15:6384 (3464284e...) -> 2 keys | 5461 slots | 1 slaves. [OK] 4 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 192.168.0.15:6382) M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots: (0 slots) slave replicates 3464284ed5db12c52ed22ab37a4f9fbada837bbb S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. ##还原6381主,6384为从机,只需停掉6384,过一会再启动6384 6主从扩容
新建2台Redis实例
### 1,2,3 为主机 ### docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis --cluster-enabled yes --appendonly yes --port 6387 docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis --cluster-enabled yes --appendonly yes --port 6388
加入集群
##新增6387 作为master节点加入集群 ##redis-cli --cluster add-node 自己实际IP地址:6387 自己实际IP地址:6381 root@localhost:/data# redis-cli --cluster add-node 192.168.0.15:6387 192.168.0.15:6381 >>> Adding node 192.168.0.15:6387 to cluster 192.168.0.15:6381 >>> Performing Cluster Check (using node 192.168.0.15:6381) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 192.168.0.15:6387 to make it join the cluster. [OK] New node added correctly. ##查看几群 root@localhost:/data# redis-cli --cluster check 192.168.0.15:6387 192.168.0.15:6387 (1b1b966a...) -> 0 keys | 0 slots | 0 slaves. 192.168.0.15:6383 (ffa128aa...) -> 1 keys | 5461 slots | 1 slaves. 192.168.0.15:6381 (c8c36e33...) -> 2 keys | 5461 slots | 1 slaves. 192.168.0.15:6382 (a85b8afb...) -> 1 keys | 5462 slots | 1 slaves.
为6387 分配槽位
##命令:redis-cli --cluster reshard IP地址:端口号 root@localhost:/data# redis-cli --cluster reshard 192.168.0.15:6387 >>> Performing Cluster Check (using node 192.168.0.15:6387) M: 1b1b966aedab54998220701d02efc0f030eeefaf 192.168.0.15:6387 slots: (0 slots) master M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[6827-10922] (4096 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. ##选择槽位数量 How many slots do you want to move (from 1 to 16384)? 4096 ###选择6387的id What is the receiving node ID? 1b1b966aedab54998220701d02efc0f030eeefaf Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. ##source 值为all Source node #1: all . . . ##分配完成检查集群状态 root@localhost:/data# redis-cli --cluster check 192.168.0.15:6387 192.168.0.15:6387 (1b1b966a...) -> 1 keys | 4096 slots | 0 slaves. 192.168.0.15:6383 (ffa128aa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.0.15:6381 (c8c36e33...) -> 1 keys | 4096 slots | 1 slaves. 192.168.0.15:6382 (a85b8afb...) -> 1 keys | 4096 slots | 1 slaves. [OK] 4 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 192.168.0.15:6387) M: 1b1b966aedab54998220701d02efc0f030eeefaf 192.168.0.15:6387 ###6387槽位由3部分组成 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[6827-10922] (4096 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. ###
为6387 分配从节点6388
##redis-cli --cluster add-node 192.168.0.15:6388 192.168.0.15:6387 --cluster-slave --cluster-master-id 1b1b966aedab54998220701d02efc0f030eeefaf-------这个是6387的编号,按照自己实际情况 root@localhost:/data# redis-cli --cluster add-node 192.168.0.15:6388 192.168.0.15:6387 --cluster-slave --cluster-master-id 1b1b966aedab54998220701d02efc0f030eeefaf >>> Adding node 192.168.0.15:6388 to cluster 192.168.0.15:6387 >>> Performing Cluster Check (using node 192.168.0.15:6387) M: 1b1b966aedab54998220701d02efc0f030eeefaf 192.168.0.15:6387 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[6827-10922] (4096 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. >>> Send CLUSTER MEET to node 192.168.0.15:6388 to make it join the cluster. Waiting for the cluster to join >>> Configure node as replica of 192.168.0.15:6387. [OK] New node added correctly. ###查看集群 root@localhost:/data# redis-cli --cluster check 192.168.0.15:6387 192.168.0.15:6387 (1b1b966a...) -> 2 keys | 4096 slots | 1 slaves. 192.168.0.15:6383 (ffa128aa...) -> 2 keys | 4096 slots | 1 slaves. 192.168.0.15:6381 (c8c36e33...) -> 2 keys | 4096 slots | 1 slaves. 192.168.0.15:6382 (a85b8afb...) -> 1 keys | 4096 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 192.168.0.15:6387) M: 1b1b966aedab54998220701d02efc0f030eeefaf 192.168.0.15:6387 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master 1 additional replica(s) M: ffa128aa359c8faf438d783a2650c32ba000f7ec 192.168.0.15:6383 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: c8c36e3345fcd03a0b29c21f574eeedaee6397d6 192.168.0.15:6381 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: 51052c5ab9d44c773341a195ba53877240c2513a 192.168.0.15:6388 slots: (0 slots) slave replicates 1b1b966aedab54998220701d02efc0f030eeefaf S: a033ef6cba285b074633388a81f183074fd237ff 192.168.0.15:6386 slots: (0 slots) slave replicates ffa128aa359c8faf438d783a2650c32ba000f7ec S: c8e26baca971251a318eabd201f7a35d2fc563ad 192.168.0.15:6385 slots: (0 slots) slave replicates a85b8afb10190406420a0e566b9785d56aa58403 S: 3464284ed5db12c52ed22ab37a4f9fbada837bbb 192.168.0.15:6384 slots: (0 slots) slave replicates c8c36e3345fcd03a0b29c21f574eeedaee6397d6 M: a85b8afb10190406420a0e566b9785d56aa58403 192.168.0.15:6382 slots:[6827-10922] (4096 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.主从缩容 6387 6388 下线
6388 下线
##命令:redis-cli --cluster del-node ip:从机端口 从机6388节点ID root@localhost:/data# redis-cli --cluster del-node 192.168.0.15:6388 51052c5ab9d44c773341a195ba53877240c2513a >>> Removing node 51052c5ab9d44c773341a195ba53877240c2513a from cluster 192.168.0.15:6388 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node.
重新分配槽位
### redis-cli --cluster reshard 192.168.0.15:6381
6387下线
##命令:redis-cli --cluster del-node ip:从机端口 从机6388节点ID root@localhost:/data# redis-cli --cluster del-node 192.168.0.15:6387 1b1b966aedab54998220701d02efc0f030eeefaf >>> Removing node 1b1b966aedab54998220701d02efc0f030eeefaf from cluster 192.168.0.15:6387 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node. docker 删除6387 6388 redis实例
docker rm -f 0200d763c22d 554a783e9fe2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)