Building a Redis cluster on Docker

Building a Redis cluster on Docker

Environment: Docker + (Redis:5.0.5 * 3)

1. Pull the image

docker pull redis:5.0.5 

2. Create a Redis container

Create three redis containers:

redis-node1:6379

redis-node2:6380

redis-node3:6381

docker create --name redis-node1 -v /data/redis-data/node1:/data -p 6379:6379 redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-1.conf

docker create --name redis-node2 -v /data/redis-data/node2:/data -p 6380:6379 redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-2.conf

docker create --name redis-node3 -v /data/redis-data/node3:/data -p 6381:6379 redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-3.conf 

3. Start and build a cluster to start the container

First, start three Redis containers using the docker start command:

After executing the run command, check the startup of the container:

If the above situation occurs, Exited (1) 3 seconds ago , you can view it through docker logs :

As shown above, it is a permission problem. Let's try to modify the permissions:

chmod -R 777 /data

After successful startup, the following figure is shown:

Building a cluster

View the IP node information assigned by 3 Redis in Docker:

Execute "docker inspect redis-node1" to get the redis-node1 ip information: 172.17.0.4
Execute "docker inspect redis-node2" to get the redis-node2 ip information: 172.17.0.3
Execute "docker inspect redis-node3" to get the redis-node3 ip information: 172.17.0.2

After getting the IP information (each person's IP information may be different), enter a container to build a cluster:

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

# Then execute the cluster formation command (please splice according to your own IP information)
redis-cli --cluster create 172.17.0.2:6379 172.17.0.3:6379 172.17.0.4:6379 --cluster-replicas 0 

OK, now the cluster is built. Let's test it.

Test cluster

Use the redis-cli -c command to connect to the cluster node, and then set the value. After setting the value, it will automatically redirect to the 0.2 IP address, and then obtain it through get. Successful acquisition proves that the cluster is valid.

4. Existing problems

According to the above steps, although the cluster is built successfully, there are still some problems. Since ip地址in the cluster node is allocated internally by the docket, such as 172.17.0.2 , if the project using redis集群is not on the same server as the cluster, then the project cannot use the cluster because it is inaccessible.

One solution is to let Docker use the host模式network connection type. The container created by Docker in host模式does not have its own independent network namespace. It shares a network space with the physical machine, and can then share all the ports and IP addresses of the physical machine. This allows the public network to directly access the container. Although this method has security risks, no other feasible mode has been found so far.

To solve the existing problem, we re-adopt host模式and re-create the container:

1. Stop the running container

docker stop redis-node1 redis-node2 redis-node3

2. Delete the previously created container

docker rm redis-node1 redis-node2 redis-node3

# Clear the configuration file created above rm -rf /data/redis-data/node*

3. Re-create based on host mode

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

Different from the previous creation command, first, the --net network type is specified as host , and second, port mapping is not required in this case, such as -p 6379:6379 , because the container port service needs to be shared externally at this time, so only the exposed ports -p 6379 , -p 6380 , etc. need to be specified.

4. Start the container and form a cluster

# Start command docker start redis-node1 redis-node2 redis-node3

# Enter a container docker exec -it redis-node1 /bin/bash

# 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 --cluster-replicas 0 

5. View cluster information

root@CentOS7:/data# redis-cli
127.0.0.1:6379> cluster nodes
72c291c32815194b64d1f6d0fdf771f5cc04e14a 10.211.55.4:6380@16380 master - 0 1590905997358 2 connected 5461-10922
6a595b67bbff15c94e5874c2d2cd556d6a6a6c17 10.211.55.4:6381@16381 master - 0 1590905998362 3 connected 10923-16383
4e3dbdc8f835dcbc38291c88f08165ee51d53d3d 10.211.55.4:6379@16379 myself,master - 0 1590905997000 1 connected 0-5460
127.0.0.1:6379>

6. Test cluster

Use redis-cli -c to connect to the cluster, set a value, and then get the value from other nodes to see if it is successful:

root@CentOS7:/data# redis-cli -c
127.0.0.1:6379> set wxiaowei 123
-> Redirected to slot [7515] located at 10.211.55.4:6380
OK
10.211.55.4:6380> get wxiaowei
"123" 

At this point,單副本模式of the Redis cluster based on Docker has been built. The three redis in this article all use the master node. The high availability of multiple copies and master-slave architecture will be supplemented later.

The master-slave cluster you want: https://www.jb51.net/article/212285.htm

This is the end of this article about building a Redis cluster on Docker. For more information about building a Docker Redis cluster, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you 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
  • Implementation of Redis master-slave cluster based on Docker
  • How to deploy Redis 6.x cluster through Docker
  • How to quickly build a redis cluster using Docker-swarm

<<:  CSS3 implements footer fixed at the bottom (always at the bottom no matter how high the page is)

>>:  How to display a small icon in front of the browser URL

Recommend

How to manually upgrade the node version under CentOs

1. Find the corresponding nodejs package, refer t...

Detailed explanation of JavaScript's built-in objects Math and strings

Table of contents Math Objects Common properties ...

Specific use of Node.js package manager npm

Table of contents Purpose npm init and package.js...

Vue custom optional time calendar component

This article example shares the specific code of ...

MySQL index coverage example analysis

This article describes MySQL index coverage with ...

MySQL count detailed explanation and function example code

Detailed explanation of mysql count The count fun...

Share some uncommon but useful JS techniques

Preface Programming languages ​​usually contain v...

MySQL 5.6.37 (zip) download installation configuration graphic tutorial

This article shares the download, installation an...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

Solution to large line spacing (5 pixels more in IE)

Copy code The code is as follows: li {width:300px...

Nginx reverse proxy learning example tutorial

Table of contents 1. Reverse proxy preparation 1....

Solve the problem of docker pull image error

describe: Install VM under Windows 10, run Docker...