Docker builds redis cluster

Suqi QAQ 2022-06-23 18:54:17 阅读数:171


1. Use docker newly build 6 individual redis Container instance , Before that , You need to open the security group on the Alibaba cloud server and pagoda interfaces (redis Client connection port and cluster bus port )

redis The cluster not only needs to be opened redis Port of client connection ( Such as 6381), And the cluster bus port needs to be opened (16381).
Cluster bus port =redis Port of client connection +10000

docker run -d --name redis-node-1 --net host --privileged=true -v /docker/redis/share/redis-node-1:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6381

docker run -d --name redis-node-2 --net host --privileged=true -v /docker/redis/share/redis-node-2:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6382

docker run -d --name redis-node-3 --net host --privileged=true -v /docker/redis/share/redis-node-3:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6383

docker run -d --name redis-node-4 --net host --privileged=true -v /docker/redis/share/redis-node-4:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6384

docker run -d --name redis-node-5 --net host --privileged=true -v /docker/redis/share/redis-node-5:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6385

docker run -d --name redis-node-6 --net host --privileged=true -v /docker/redis/share/redis-node-6:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6386 

Command interpretation :

 2. Into the container redis-node-1, Build cluster relationships for six containers

        2.1  docker exec -it redis-node-1 /bin/bash

        2.2  redis-cli --cluster create --cluster-replicas 1

--cluster-replicas 1 Indicates how many slave nodes the cluster master node needs , We use the 6 platform , namely 3 Servers form a cluster , Each server is set 1 Two slave servers

 3. The entry port is 6381 Of redis Containers , And check the cluster status

        3.1 docker exec -it redis-node-1 /bin/bash

        3.2 redis-cli -p 6381

        3.3 cluster info

        3.4 cluster nodes ( This command is to see which node is the primary node , Which node is the slave node , And groove point range )

4. Test the master-slave failover migration

        4.1 The entry port is 6381 The container of , Add two key, We need to add -c, Cluster operation , Can be key-value Put it in another container

        Redis There's a built-in... In the cluster 16384 Hash slot ,redis The hash slot will be mapped to different nodes approximately equally according to the number of nodes . When need is in Redis Place one in the cluster key-value when ,redis First pair key Use crc16 The algorithm works out a result , Then get the result right 16384 Mod , So each of them key They all have a number in 0-16383 The Hashi trough between , That is, mapping to a node . The following code ,key And A 、B stay Node2, key And C Fall in the Node3 On

         therefore , You can see the first one k1 v1 The scope will include 12706 In this slot , That is to say 6383 In this container ,

 k2 v2 Will fall to include 449 In the slot points of this range , That is to say 6386 In this container

        4.2 ctrl+c, sign out redis-cli

        4.3 View cluster information redis-cli --cluster check

         4.4 The test will 6386(master) Container stop ,6381 From the opportunity to replace the host 6386 The position of , become master host

5. Master slave capacity expansion

        5.1 Create a new cluster 6387、6388 Two nodes

docker run -d --name redis-node-7 --net host --privileged=true -v /docker/redis/share/redis-node-7:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6387

docker run -d --name redis-node-8 --net host --privileged=true -v /docker/redis/share/redis-node-8:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6388

        5.2 Get into 6387 Inside of container

docker exec -it redis-node-7 /bin/bash

        5.3 Will add 6387 Node as master The node joins the original cluster ( I'm here through 6381 establish redis Clustered ,

 6381 The container is the guide in the original cluster node , amount to 6387 Bye-bye 6381 So as to find organizations to join the cluster )

redis-cli --cluster add-node

        5.4 Check whether to add this redis colony

redis-cli --cluster check

         5.5 Reassign slot numbers

redis-cli --cluster reshard

          The number of slots allocated here =16384/ Number of machines =》  Number of slots allocated =16384/4=4096

          Here, the node's id Number , Should be a new node 6387 Node number of

          Here, all nodes are allocated 4096 A slot , So for all

     5.6  View slot point assignment

redis-cli --cluster check

  Why? 6387 The slot points allocated to the container are 3 A new interval , Because reallocation costs are too high , So the first three companies set aside a portion ,

from 6381/6382/6383 Separate out the three old nodes 1364 A pit for the new node 6387

         5.7 Master node 6387 Assign slave nodes 6388

redis-cli --cluster add-node --cluster-slave --cluster-master-id f8cabc7a47b374a8838bd75ab34c6e9804781d3f

         5.8 Check the cluster I found that it has been divided

6. Master slave volume reduction

        6.1 take 6387 6388 Two nodes are offline

        6.2 Check the cluster , get 6388 The node of id

redis-cli --cluster check

         6.4 From the cluster 6388 The node to delete

redis-cli --cluster del-node da2567b5be3ac4b92b02abed931ff15ea04011e0

         6.5 take 6387 Empty the slot number , Reassign slot numbers , And give them to the host ( It must be the host , Instead of a slave )6381

redis-cli --cluster reshard

        6.6 Check the cluster

redis-cli --cluster check

  Find out 6387 The node has no slots assigned to it , Instead, all of them are distributed to other container nodes

        6.7 take 6387 Remove the cluster

redis-cli --cluster del-node f8cabc7a47b374a8838bd75ab34c6e9804781d3f

        6.8 Review the cluster , It is found that both nodes have been eliminated , And realized volume reduction


版权声明:本文为[Suqi QAQ]所创,转载请带上原文链接,感谢。