Implementation of Redis master-slave cluster based on Docker

Implementation of Redis master-slave cluster based on Docker

Recently, many friends have added me as a friend to ask about the problem of setting up a redis cluster. I think there must be something wrong with the article "Setting up a Redis cluster based on Docker" I wrote before, so I spent a few minutes browsing the previous article and summarized the following questions:

The number of redis is too small, and only 3 instances are created; since there are only 3 instances, all of them can only be master nodes, and the master-slave relationship of the cluster cannot be reflected; how to build a master-slave cluster? How to allocate slave nodes?

Based on the previous article, I would like to quickly go over these questions. This article is based on Docker + Redis 5.0.5 version, and creates a master-slave cluster of 6 redis instances through cluster mode. Of course, the article will point out the corresponding parameter descriptions, so that even if a cluster of 9 instances is created, the method is the same.

1. Pull the Redis image

Based on Redis: version 5.0.5, execute the following instructions:

docker pull redis:5.0.5

2. Create 6 Redis containers

Create 6 Redis containers:

  • redis-node1:6379
  • redis-node2:6380
  • redis-node3:6381
  • redis-node4:6382
  • redis-node5:6383
  • redis-node6:6384

The execution command is as follows:

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 6384

Some parameter explanations:

  • --cluster-enabled: whether to enable the cluster, select value: yes, no
  • --cluster-config-file configuration file.conf: specify node information, automatically generated
  • --cluster-node-timeout millisecond value: Configure node connection timeout
  • --appendonly: whether to enable persistence, select value: yes, no

Execute command screenshot:

3. Start the Redis container

The execution command is as follows:

docker start redis-node1 redis-node2 redis-node3 redis-node4 redis-node5 redis-node6

The startup screenshot is as follows:

4. Build a Redis cluster

Enter any Redis instance:

# Here we take the redis-node1 instance as an example docker exec -it redis-node1 /bin/bash

Execute the command of the component cluster:

# Build a cluster, 10.211.55.4 is the IP address of the current physical machine 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 1

The screenshot of the execution command is as follows:

After successful creation, check the cluster node information through redis-cli:

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

The screenshot of the execution command is as follows:

5. About Redis cluster construction

Let's go back to the command to create the cluster:

redis-cli --cluster create 10.211.55.4:6379~6384 --cluster-replicas 1

Please pay special attention to this parameter --cluster-replicas 1. The number after the parameter indicates the master-slave ratio. For example, the 1 here means that the master-slave ratio is 1:1. What does this mean?

That is, 1 master node corresponds to several slave nodes. There are 6 instances now, so the master-slave allocation is 3 master nodes and 3 slave nodes.

There must be at least 3 master nodes to ensure the robustness of the cluster.

What if --cluster-replicas 2?

Then the master-slave ratio is 1:2, that is, 1 master node corresponds to 2 slave nodes.

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

What happens if there are less than 9 Redis instances, but the parameter is specified as 2?

The error message is as follows:

The prompt is very clear, the Redis cluster requires at least 3 master nodes. Then there needs to be 6 slave nodes, so in the end: at least 9 nodes are needed.

OK, I won't continue with the requirement of at least 3 master nodes, but I think 4 master nodes and 2 slave nodes should be enough, right?

4 master nodes satisfy you:

# Enter a started redis instance. Here we take the redis-node1 instance as an example. docker exec -it redis-node1 /bin/bash

Execute the command to build 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 0

Specify 4 masters without slaves, so you have 4 masters:

What about the remaining two slave nodes? Add manually.

How to add? Add manually!

See the IDs of these master nodes? Just assign slaves to them.

Continue to execute the following commands:

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 111de8bed5772585cef5280c4b5225ecb15a582e

The two Redis instances are plugged into other master nodes:

Finally, we enter redis-cli and view the node information through cluster nodes:

This is the end of this article about the implementation of Redis master-slave cluster based on Docker. For more relevant Docker Redis master-slave cluster content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the environment construction of docker to build redis cluster
  • Example of building a redis-sentinel cluster based on docker
  • Method of building redis cluster based on docker
  • Use Docker to build a Redis master-slave replication cluster
  • Teach you how to build Redis cluster mode and sentinel mode with docker in 5 minutes
  • Building a Redis cluster on Docker
  • How to deploy Redis 6.x cluster through Docker
  • How to quickly build a redis cluster using Docker-swarm

<<:  Detailed explanation of the problem when combining CSS ellipsis and padding

>>:  Basic knowledge of HTML: a preliminary understanding of web pages

Recommend

How to store images in MySQL

1 Introduction When designing a database, it is i...

Web design experience: Make the navigation system thin

<br />When discussing with my friends, I men...

Vue uses rules to implement form field validation

There are many ways to write and validate form fi...

How to install Nginx in CentOS

Official documentation: https://nginx.org/en/linu...

Linux service monitoring and operation and maintenance

Table of contents 1. Install the psutil package S...

How to support Webdings fonts in Firefox

Firefox, Opera and other browsers do not support W...

Implementation of docker view container log command

Why should we read the log? For example, if the c...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

HTML page common style (recommended)

As shown below: XML/HTML CodeCopy content to clip...

Vue close browser logout implementation example

Table of contents 1. beforeunload event 2. Unload...

Detailed explanation of the difference between alt and title

These two attributes are often used, but their di...

Linux file systems explained: ext4 and beyond

Today I will take you through the history of ext4...

How to enable TLS and CA authentication in Docker

Table of contents 1. Generate a certificate 2. En...

How to use Docker to limit container resources

Problem Peeping In the server, assuming that the ...