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

Encoding problems and solutions when mysql associates two tables

When Mysql associates two tables, an error messag...

Sample code for implementing markdown automatic numbering with pure CSS

The origin of the problem The first time I paid a...

Tutorial on installing MySQL 5.6 using RPM in CentOS

All previous projects were deployed in the Window...

MySQL master-slave data is inconsistent, prompt: Slave_SQL_Running: No solution

This article uses an example to describe the solu...

XHTML Getting Started Tutorial: What is XHTML?

What is HTML? To put it simply: HTML is used to m...

Steps to split and compress CSS with webpack and import it with link

Let's take a look at the code file structure ...

Problems and solutions encountered when installing mininet on Ubuntu 16.04.4LTS

Mininet Mininet is a lightweight software defined...

Two ways to connect WeChat mini program to Tencent Maps

I've been writing a WeChat applet recently an...

Pure HTML and CSS to achieve JD carousel effect

The JD carousel was implemented using pure HTML a...

Comparison of mydumper and mysqldump in mysql

If you only want to back up a few tables or a sin...

How to prevent Flash from covering HTML div elements

Today when I was writing a flash advertising code,...