Home > Database > Redis > How to build a Redis cluster based on Docker

How to build a Redis cluster based on Docker

王林
Release: 2023-05-30 12:46:07
forward
1432 people have browsed it

Environment: Docker (Redis:5.0.5 * 3)

1. Pull the image

docker  pull  redis:5.0.5
Copy after login

How to build a Redis cluster based on Docker

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
    Copy after login

How to build a Redis cluster based on Docker

3. Start and form a cluster

Start the container
First start 3 Redis containers through the command

docker start:

How to build a Redis cluster based on Docker

After executing the running command, check the startup status of the container:

How to build a Redis cluster based on Docker

If the above situation occurs,

Exited (1) 3 seconds ago, you can view it through docker logs:

How to build a Redis cluster based on Docker

The above prompt is a permission problem. Let’s try to modify the permissions:

chmod -R  777 /data
Copy after login

After successful startup, as shown below:

How to build a Redis cluster based on Docker

Build a cluster
View the ip node information assigned by 3 Redis in Docker:

执行「docker inspect redis-node1」得到 redis-node1 ip 信息为:172.17.0.4 
执行「docker inspect redis-node2」得到 redis-node2 ip 信息为:172.17.0.3 
执行「docker inspect redis-node3」得到 redis-node3 ip 信息为:172.17.0.2
Copy after login

How to build a Redis cluster based on Docker

After getting the ip information (each person’s ip (The information may be different), then enter a certain container to set up a cluster:

# 这里以进入 node1 为例
docker exec -it redis-node1 /bin/bash

# 接着执行组建集群命令(请根据自己的ip信息进行拼接)
redis-cli --cluster create 172.17.0.2:6379  172.17.0.3:6379  172.17.0.4:6379 --cluster-replicas 0
Copy after login

How to build a Redis cluster based on Docker

ok, now the cluster is set up, let’s test it next.

Test cluster
Use the

redis-cli -c command to connect to the cluster node, and then set the value. After the set value, it will automatically redirect to the 0.2 ip address, and then pass Get Get it. Successful acquisition proves that the cluster is valid.

How to build a Redis cluster based on Docker

4. Existing problems

According to the above steps, although the cluster was successfully built, there are still some problems. Due to the

ip address is assigned internally by the dock, such as: 172.17.0.2 etc. If the project using redis cluster is not on the same server as the cluster, then the project is You can't use the cluster because access is blocked.

How to build a Redis cluster based on Docker

One solution is to let

Docker use the host mode network connection type, Docker in Containers created using host mode do not have their own independent network namespace. They share a network space with the physical machine, and can then share all ports and IP of the physical machine. In this way This allows the public network to directly access the container. Although this method has security risks, no other feasible mode has been found yet.

Regarding the existing problems, we re-adopt the

host mode and re-create the container:

1. Stop the running container
docker stop redis-node1 redis-node2 redis-node3
Copy after login
2. Delete The previously created container
docker rm redis-node1 redis-node2 redis-node3

# 清空上面创建的配置文件
rm -rf /data/redis-data/node*
Copy after login
3. Re-create it based on the 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
Copy after login
is different from the previous creation command. First, the

--net network type is specified host, secondly, in this case, there is no need for port mapping, such as -p 6379:6379, because the container port service needs to be shared externally at this time, so you only need to specify the externally exposed port -p 6379, -p 6380, etc.

How to build a Redis cluster based on Docker

4. Start the container and form a cluster
# 启动命令
docker start redis-node1 redis-node2 redis-node3

# 进入某一个容器
docker exec -it redis-node1 /bin/bash

# 组建集群,10.211.55.4为当前物理机的ip地址
redis-cli --cluster create 10.211.55.4:6379  10.211.55.4:6380  10.211.55.4:6381 --cluster-replicas 0
Copy after login

How to build a Redis cluster based on Docker

5. View the 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>
Copy after login
6. Test the 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"
Copy after login

How to build a Redis cluster based on Docker

The above is the detailed content of How to build a Redis cluster based on Docker. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template