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

Design theory: On the issues of scheme, resources and communication

<br />This problem does not exist in many sm...

Detailed explanation of JS array methods

Table of contents 1. The original array will be m...

MySQL partitioning practice through Navicat

MySQL partitioning is helpful for managing very l...

Vue uses filters to format dates

This article example shares the specific code of ...

Solution to the automatic stop of MySQL service

This article mainly introduces the solution to th...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

Detailed explanation of how to install PHP7 on Linux

How to install PHP7 on Linux? 1. Install dependen...

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...

Complete steps to enable gzip compression in nginx

Table of contents Preface 1. Configure gzip compr...

How complicated is the priority of CSS styles?

Last night, I was looking at an interview question...

How to make spaces have the same width in IE and FF?

body{font-size:12px; font-family:"宋体";}...

This article will show you the basics of JavaScript: deep copy and shallow copy

Table of contents Shallow copy Deep Copy Replenis...