Deploy redis multi-master and multi-slave cluster based on docker

Deploy redis multi-master and multi-slave clusters in docker, and prepare to deploy three-to-one master-slave services, a total of 6

Get the mirror first

The version 6.0.8 is used here

docker pull redis:6.0.8

 

Start six containers

docker run -d --name redis-node1 --net host --privileged=true -v /usr/local/redis/node1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6371

docker run -d --name redis-node2 --net host --privileged=true -v /usr/local/redis/node2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6372

docker run -d --name redis-node3 --net host --privileged=true -v /usr/local/redis/node3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6373

docker run -d --name redis-node4 --net host --privileged=true -v /usr/local/redis/node4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6374

docker run -d --name redis-node5 --net host --privileged=true -v /usr/local/redis/node5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6375

docker run -d --name redis-node6 --net host --privileged=true -v /usr/local/redis/node6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6376

Command description:

--net host : use the host's IP and port, default

--privileged=true : Obtain root user privileges of the host machine

-v /usr/local/redis/node1:/data : mount container data volume

--cluster-enabled yes : enable redis cluster

--appendonly yes : enable persistence

Build a master-slave relationship

into a container

docker exec -it redis-node1 /bin/bash

Execute the following command, replace the ip in the following command with the actual ip (remember to open the ports used by the 6 containers, and the port of each port +10000, because the port used by the redis cluster communication is the main port +10000. I opened 6371 here to 6376 and 16371 to 16376 several ports)

--cluster-replicas 1 means 1 master and 1 slave allocation

redis-cli --cluster create 111.111.111.111:6371 111.111.111.111:6372 111.111.111.111:6373 111.111.111.111:6374 111.111.111.111:6375 111.111.111.111:6376 --cluster-replicas 1

After execution, the following content appears, showing the master-slave correspondence. You can see that there are 3 masters and 3 slaves. If you agree with the configuration, enter yes and press Enter 

 After yes, the following content appears to indicate success

You can also see the corresponding relationship described above by using the view cluster status command, as follows

redis-cli --cluster check 111.111.111.111:6371

 

View cluster information

Also use the docker exec command to enter container 1 

Connect to redis client

redis-cli -p 6271

view information

cluster info

 You can see that there are 6 known nodes

View node information

cluster nodes

 You can see the ip and ports of the three master nodes and three slave nodes. From the corresponding codes, the No. 4 slave is connected to the No. 1 master, the No. 5 slave is connected to the No. 2 master, and the No. 6 slave is connected to the No. 2 master. Host No. 3. This correspondence is not fixed, and may not be the case in the next deployment.

 

Test data read and write

Same docker exec into container 1

Use the cluster mode to connect to redis1 (after the ordinary stand-alone version is connected, an error may occur in the stored data, because the hash value calculated by the stored key is not within the storage range of the current redis node after taking the remainder from 16383)

Adding a -c after it means connecting in a cluster

redis-cli -p 6371 -c

Set a k1 v1, you will be prompted to redirect to the node where 6373 is located, because the 12706 slot calculated by k1 is within the range of 6373 nodes

 

Downtime Switch Migration

When a master is down, the slave should be the master

docker stop redis-node1 Stop the 6371 node, and then wait for a while, waiting for the heartbeat notification inside the cluster

Enter the 6372 container, connect to 6372, and check the cluster status

It can be seen that node 6371 has failed, and its previous slave node 6374 has become the master node

And the previously stored data can also be accessed normally

After starting the 6371 node again, check the node status and find that 6374 is still the master node, and 6371 has become a slave node

If you want to restore the original 6371 as the master node, you can stop 6374 for a while and start it again, and the two node identities will be swapped back again

 

Guess you like

Origin blog.csdn.net/qq_41890624/article/details/127893584