Recently, many garden friends added me to ask about the problem of redis cluster building. I think there must be something wrong with the article “Docker-based Redis cluster building” written before, so I spent a few minutes browsing the previous article and summarized the following problems:

  • The number of Redis is too small, with only 3 instances created;
  • Because there are only three instances, all of them can only be primary nodes, which cannot reflect the master-slave relationship of the cluster.
  • How do I set up a primary/secondary cluster? How are slave nodes assigned?

Based on the previous article, I would like to quickly go over these problems. This article is based on Docker + Redis 5.0.5 version, through the way of cluster to create a master and slave cluster of 6 Instances of Redis, of course, the article will point out the corresponding parameter description. This is the same even if you create a cluster of nine instances.

1. Pull the Redis image

Based on Redis: 5.0.5, execute the following commands:

Docker pull redis: 5.0.5Copy the code

2. Create 6 Redis containers

Create 6 Redis containers:

  • Redis rac-node1:6379
  • Redis – 2:6380
  • Redis – node3:6381
  • Redis – node4:6382
  • Redis – node5:6383
  • Redis – node6:6384

Run the following command:

Docker create --name redis-node1 --net host -v /data/redis-data/node1:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-1.conf --port 6379 docker create --name redis-node2 --net host -v /data/redis-data/node2:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-2.conf --port 6380 Docker create --name redis-node3 --net host -v /data/redis-data/node3:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-3.conf --port 6381 docker create --name redis-node4 --net host -v /data/redis-data/node4:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-4.conf --port 6382 Docker create --name redis-node5 --net host -v /data/redis-data/node5:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-5.conf --port 6383 docker create --name redis-node6 --net host -v /data/redis-data/node6:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-6.conf --port 6384Copy the code

Explanation of some parameters:

  • –cluster-enabled: specifies whether to start the cluster. Value: yes, no
  • –cluster-config-file Configuration file. conf: specifies node information, which is automatically generated
  • –cluster-node-timeout ms Value: specifies the timeout period for node connection
  • –appendonly: Indicates whether to enable persistence. Value: yes or no

Screenshot of the command:

3. Start the Redis container

Run the following command:

docker start redis-node1 redis-node2 redis-node3 redis-node4 redis-node5 redis-node6
Copy the code

The startup screenshot is as follows:

4. Set up Redis cluster

Enter any Redis instance:

Docker exec it redis-node1 /bin/bashCopy the code

Execute component cluster commands:

Redis-cli --cluster create 10.211.55.4:6379 10.211.55.4:6380 10.211.55.4:6381 10.211.55.4:6382 10.211.55.4:6383 10.211.55.4:6384 --cluster-replicas 1Copy the code

The following is the screenshot of the command:

After the cluster is created, run the redis-CLI command to view the cluster node information:

root@CentOS7:/data# redis-cli
127.0.0.1:6379> cluster nodes
Copy the code

The following is the screenshot of the command:

5. About Redis cluster building

Let’s go back to the cluster creation command:

Redis-cli --cluster create 10.211.55.4:6379~6384 --cluster-replicas 1Copy the code

Cluster-replicas 1 specifies the ratio between the primary and secondary nodes in the cluster. For example, the 1 in cluster-replicas is 1:1.

That is, one master node corresponds to several slave nodes. There are six instances, so the master/slave assignment is 3 master nodes and 3 slave nodes.

At least three primary nodes are required to ensure the robustness of the cluster.

What if –cluster-replicas 2?

So the master-slave ratio is 1:2, that is, 1 master node for 2 slave nodes.

That is, 3(master) + 6(slave) = 9 Redis instances.

What if there are fewer than nine instances of Redis, but the parameter is specified as 2?

The following error message is displayed:

The tip is already clear: a Redis cluster requires at least three primary nodes. So you have to have six nodes, so you end up with at least nine nodes.

Well, I’m not going to go on with the requirement of at least 3 master nodes, but I think 4 master nodes, 2 slave nodes, is that good enough?

Four primary nodes satisfy you:

Docker exec it redis-node1 /bin/bash docker exec it redis-node1 /bin/bashCopy the code

Execute commands to form a cluster:

Redis-cli --cluster create 10.211.55.4:6379 10.211.55.4:6380 10.211.55.4:6381 10.211.55.4:6382 --cluster-replicas 0Copy the code

Specify 4 primary nodes with no secondary nodes, so you have 4 primary nodes:

What about the remaining two slave nodes? Manually add.

How do I add it? Manually add!

Do you see the ids of these master nodes? Just assign them the slave nodes.

Run the following command:

Redis-cli --cluster add-node 10.211.55.4:6383 10.211.55.4:6379 --cluster-slave --cluster-master-id B0c32b1dae9e7b7f7f4b74354c59bdfcaa46f30a redis - cli - cluster add -node 10.211.55.4:6384 10.211.55.4:6379 - cluster - slave --cluster-master-id 111de8bed5772585cef5280c4b5225ecb15a582eCopy the code

Two Redis instances have been stuffed to other primary nodes:

Finally, we enter redis-CLI and check the node information through cluster Nodes:

Did you lose school when you saw that? I’m going to make a video next time.

In this paper, starting from the blog garden: www.cnblogs.com/niceyoo/p/1…