Use Docker to build a “one master and two slaves” Redis cluster (super detailed steps)

Directory

      • 1. Redis stand-alone version installation
        • 1.1 Pull Redis
        • 1.2 Create data volume directory
        • 1.3 Modify redis.conf
        • 1.4 Start the Redis container
        • 1.5 Enter container connection Redis
      • 2. Redis cluster construction with one master and two slaves
        • 2.1 Copy three copies of redis.conf
        • 2.2 Start master
        • 2.3 Start two redis slaves
        • 2.4 View the relationship between the three
        • 2.5 Data testing
      • 3. Redis high availability cluster construction
        • 3.1 Copy three copies of sentinel.conf
        • 3.2 Start sentinel
        • 3.3 Relationship View
        • 3.4 Failover testing
      • 4. Redis distributed system construction
        • 4.1 Prepare directories and configuration files
        • 4.2 Copy six copies of redis.conf
        • 4.3 Start redis
        • 4.4 Create system
        • 4.5 View node information

1. Redis stand-alone version installation

1.1 Pull Redis

First pull the Redis image from docker hub, pull version 7.0 here.

docker pull redis:7.0

1.2 Create data volume directory

First, create a directory redis in the host/root directory, which will be used to store the plug-in file redis.conf in the future.

mkdir redis

Use the rz command to upload a copy of redis.conf:

rz
1.3 Modify redis.conf

Modify configuration file:

vim redis.conf

1. Unbind IP

Comment out the bind line to unbind Redis from the visitor’s IP.

2. Turn off protected mode
Turn off protected mode, otherwise you can only access yourself locally.

3. Specify the persistence directory
Here you need to specify the persistence directory of RDB or AOF as /data, so that no matter what kind of persistence file it is, it will be saved.
to this directory. Later, the /data directory in the container will be designated as the data volume mount point directory.

1.4 Start Redis container
docker run --name myredis \
-v /root/redis/redis.conf:/etc/redis/redis.conf \
-v /root/redis/data:/data \
-dp 6379:6379 \
redis:7.0 \
redis-server /etc/redis/redis.conf

Two data volumes are specified here, one is a file and the other is a directory:

  • /root/redis/redis.conf:/etc/redis/redis.conf
  • /root/redis/data:/data
    What needs to be noted about this startup command is that the command run after it is redis-server, and the configuration file loaded is
    redis.conf in the mount point directory /etc/redis.

View the running docker image:

docker ps -a

1.5 Enter container connection Redis

After entering the Redis container through the docker exec command, you can connect to the Redis through the redis-cli client, and then execute the Redis command.

docker exec -it myredis /bin/bash
redis-cli

So far, the stand-alone version of redis is done! ! ! !

2. Redis one master and two slave cluster construction

Now we need to build a Redis cluster with “one master and two slaves”. The port numbers of these three containers remain default, but the port numbers exposed to the outside are 6381, 6382, and 6383 respectively. Among them, 6381 is the master and the other two are slaves.

2.1 Copy three copies of redis.conf

The configuration is still completed in the previous /root/redis directory. Copy redis.conf and rename it to redis1.conf, and add the following configuration at the end of the file to announce the current redis IP and port to the outside world. Note that the IP is the IP of the docker host, and the port number is the port number currently exposed by redis.

Copy redis.conf and rename to redis1.conf

cp redis.conf redis1.conf

Modify redis1.conf

vim redis1.conf

Finally, add two sentences as follows:

slave-announce-ip 192.168.162.105
slave-announce-port 6381

Similarly redis2.conf redis3.conf is as follows:

slave-announce-ip 192.168.162.105
slave-announce-port 6382
slave-announce-ip 192.168.162.105
slave-announce-port 6383

2.2 Start master

First start the master, that is, start the myredis-1 container.

docker run --name myredis-1 \
-v /root/redis/redis1.conf:/etc/redis/redis.conf \
-v /root/redis/data/6381:/data \
-dp 6381:6379 \
redis:7.0 \
redis-server /etc/redis/redis.conf

2.3 Start two redis slaves

In the command to start the slave, you need to indicate to whom it is slaveof.

Start myredis-2:

docker run --name myredis-2 \
-v /root/redis/redis2.conf:/etc/redis/redis.conf \
-v /root/redis/data/6382:/data \
-dp 6382:6379 \
redis:7.0 \
redis-server /etc/redis/redis.conf --slaveof 192.168.162.105 6381

Start myredis-3:

docker run --name myredis-3 \
-v /root/redis/redis3.conf:/etc/redis/redis.conf \
-v /root/redis/data/6383:/data \
-dp 6383:6379 \
redis:7.0 \
redis-server /etc/redis/redis.conf --slaveof 192.168.162.105 6381

2.4 View the relationship between the three

Looking at the info replication of these three container nodes, you can see that the master-slave relationship between them has been established.

docker exec -it myredis-1 redis-cli info replication

docker exec -it myredis-2 redis-cli info replication

docker exec -it myredis-3 redis-cli info replication

2.5 Data Test

Write data in the master node myredis-1.

docker exec -it myredis-1 /bin/bash

redis-cli

set name tigerhhzz11
set name tigerhhzz11

Here you can open a new connection window for testing
Data can be read from the slave nodes myredis-2 and myredis-3 nodes.

3. Redis high availability cluster construction

The problem with the master-slave cluster is that its disaster recovery method can only use cold processing solutions and cannot be used in production. Therefore, a high-availability cluster with “one master, two slaves, and three sentinels” needs to be built here to achieve a disaster recovery solution for heat treatment. For a “one master and two slaves” cluster, you can still use the previous one. Let’s directly build a cluster of three Sentinel nodes. The port numbers of these three containers remain default, but the port numbers exposed to the outside are 26381, 26382, and 26383 respectively.

3.1 Copy three copies of sentinel.conf

Copy the sentinel.conf file and rename it to sentinel1.conf. Only two changes are made:

  • Specify the master and .
  • Specify the IP and port number currently announced by sentinel. Where IP is the IP of the docker host, and the port number is
    Port number exposed to the outside world.

Modify sentinel1.conf

vim sentinel1.conf

sentinel monitor mymaster 192.168.162.105 6381 2
sentinel announce-ip 192.168.162.105
sentinel announce-port 26381

In the same way, copy and modify sentinel2.conf.

```bash
sentinel monitor mymaster 192.168.162.105 6381 2
sentinel announce-ip 192.168.162.105
sentinel announce-port 26382

Similarly, copy and modify sentinel3.conf.

```bash
```bash
sentinel monitor mymaster 192.168.162.105 6381 2
sentinel announce-ip 192.168.162.105
sentinel announce-port 26383

3.2 Start sentinel

Start three sentinel containers.

docker run --name mysentinel-1 \
-v /root/redis/sentinel1.conf:/etc/redis/sentinel.conf \
-dp 26381:26379 \
redis:7.0 \
redis-sentinel /etc/redis/sentinel.conf

docker run --name mysentinel-2 \
-v /root/redis/sentinel2.conf:/etc/redis/sentinel.conf \
-dp 26382:26379 \
redis:7.0 \
redis-sentinel /etc/redis/sentinel.conf
docker run --name mysentinel-3 \
-v /root/redis/sentinel3.conf:/etc/redis/sentinel.conf \
-dp 26383:26379 \
redis:7.0 \
redis-sentinel /etc/redis/sentinel.conf

3.3 Relationship View
docker exec -it mysentinel-1 redis-cli -h 192.168.162.105 -p 26381 info sentinel

The result of the above command shows that sentinel successfully monitors the master, indicating that the high-availability cluster is successfully established. The information viewed when connecting to any sentinel container node is the same as above.

3.4 Failover Test

In order to verify high availability, the container myredis-1 acting as the master is now stopped.

docker stop myredis-1

At this time, check the status data of the other two redis containers and find that myredis-2 has become the slave of myredis-3.
That is, myredis-3 becomes the new master.

docker exec -it myredis-2 redis-cli info replication
docker exec -it myredis-3 redis-cli info replication


At this point the myredis-1 container is started again.

docker start myredis-1

Check the status data of myredis-1 and find that it has become the slave of myredis-3.


4. Redis distributed system construction

The data saved in each node of the Redis cluster is the same. The data stored in the nodes of the Redis distributed system can be different. When a data write request reaches the distributed system, the system will use the virtual slot partitioning algorithm to write the data to the corresponding node.

Next, we will build a Redis distributed system with three masters and three slaves.

Serial number Role Container name Network mode Exposed address
1 master myredis-cluster-1 host 192.168.162.105:6381
2 master myredis-cluster-2 host 192.168.162.105:6382
3 master myredis-cluster-3 host 192.168.162.105:6383
4 slave myredis-cluster-4 host 192.168.162.105:6384
5 slave myredis-cluster-5 host 192.168.162.105:6385
6 slave myredis-cluster-6 host 192.168.162.105:6386
4.1 Prepare directories and configuration files

mkdir a directory named cluster in /root, and replace the previous configuration file /root/redis/redis.conf
Copy it here.

[root@docker ~]# mkdir cluster
[root@docker ~]# cd cluster
[root@docker cluster]# cp /root/redis/redis.conf redis.conf
[root@docker cluster]# ll

4.2 Copy six copies of redis.conf

Copy redis.conf to redis1.conf, and remove the comments before the following two configurations. Of these two configurations, one is used to enable the cluster function, that is, the distributed system function; the other is to specify the name of the configuration file it requires.

cp redis.conf redis1.conf
vim redis1.conf

Then use redis1.conf as the template to copy out 5 copies, namely redis2.conf, redis3.conf, redis4.conf, redis5.conf, and redis6.conf. The contents of these 6 configuration files are identical.

[root@docker cluster]# ll
Total usage 108
-rw-r--r--. 1 root root 106667 November 4 17:24 redis1.conf
[root@docker cluster]# cp redis1.conf redis2.conf
[root@docker cluster]# cp redis1.conf redis3.conf
[root@docker cluster]# cp redis1.conf redis4.conf
[root@docker cluster]# cp redis1.conf redis5.conf
[root@docker cluster]# cp redis1.conf redis6.conf
[root@docker cluster]# ll
Total usage 756
-rw-r--r--. 1 root root 106667 November 4 17:28 redis1.conf
-rw-r--r--. 1 root root 106667 November 4 17:28 redis2.conf
-rw-r--r--. 1 root root 106667 November 4 17:28 redis3.conf
-rw-r--r--. 1 root root 106667 November 4 17:28 redis4.conf
-rw-r--r--. 1 root root 106667 November 4 17:28 redis5.conf
-rw-r--r--. 1 root root 106667 November 4 17:28 redis6.conf
-rw-r--r--. 1 root root 106667 November 4 17:24 redis.conf
[root@docker cluster]#

4.3 Start redis

Delete all running containers before starting:

docker rm -f $(docker ps -qa)

Start 6 Redis containers.

docker run --name myredis-1 --network host -v /root/cluster/redis1.conf:/etc/redis/redis.conf -v /root/cluster/data/6381:/data -d redis :7.0 redis-server /etc/redis/redis.conf --port 6381
docker run --name myredis-2 --network host -v /root/cluster/redis2.conf:/etc/redis/redis.conf -v /root/cluster/data/6382:/data -d redis :7.0 redis-server /etc/redis/redis.conf --port 6382
docker run --name myredis-3 --network host -v /root/cluster/redis3.conf:/etc/redis/redis.conf -v /root/cluster/data/6383:/data -d redis :7.0 redis-server /etc/redis/redis.conf --port 6383
docker run --name myredis-4 --network host -v /root/cluster/redis4.conf:/etc/redis/redis.conf -v /root/cluster/data/6384:/data -d redis :7.0 redis-server /etc/redis/redis.conf --port 6384
docker run --name myredis-5 --network host -v /root/cluster/redis5.conf:/etc/redis/redis.conf -v /root/cluster/data/6385:/data -d redis :7.0 redis-server /etc/redis/redis.conf --port 6385
docker run --name myredis-6 --network host -v /root/cluster/redis6.conf:/etc/redis/redis.conf -v /root/cluster/data/6386:/data -d redis :7.0 redis-server /etc/redis/redis.conf --port 6386
4.4 Create system

After the 6 nodes are started, they are still 6 independent Redis. The 6 nodes can be created as a distributed system through the redis-cli –cluster create command. –cluster replicas 1 specifies that each master will have a slave copy.

docker exec -it myredis-1 /bin/bash

redis-cli --cluster create --cluster-replicas 1 192.168.162.105:6381 192.168.162.105:6382 192.168.162.105:6383 192.168.162.105:6384 192.168.162.105:6385 192.168.162.105:6386

After pressing Enter, you can see the following plan log:

Type yes and press Enter to complete the system creation according to the above plan.

4.5 View node information

You can view the relationship and connection status of each node in the system through the cluster nodes command. As long as you can see that each node is connected, it means that the distributed system has been successfully built.

View 6383 cluster node information:

redis-cli -c -p 6383 cluster nodes

When you see connected, it means you have successfully built a redis cluster with three masters and three slaves! ! ! (complete)