docker builds redis cluster

Posted by DeeDee2010 on Thu, 13 Jan 2022 15:24:31 +0100

1docker install redis standalone

1 Download Image

[root@db01 ~]#docker pull redis

2 create mount profile

[root@db01 ~]#mkdir -p /application/redis/conf
[root@db01 ~]#touch /application/redis/conf/redis.conf

3 create an instance and start:

Map the 6379 of the virtual machine to the 6379 of docker, give the container a name called redis, hang the / data folder in the container in the / application/redis/data folder, and the configuration file in the container / etc / redis / redis Conf mount to virtual machine / application / redis / conf / redis Conf, - d redis indicates that the image redis is running in the background

-d redis redis-server /etc/redis/redis.conf means to start the redis server in the redis image with the specified configuration file

docker run -p 6379:6379 --name redis -v /application/redis/data:/data \
-v /application/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf

4. How to enter the redis client:

[root@db01 ~]#docker exec -it redis redis-cli
127.0.0.1:6379> 

5. How to restart redis:

[root@db01 ~]#docker restart redis

6. Configure data persistence (redis needs to be restarted):

[root@db01 ~]#vi /application/redis/conf/redis.conf 
[root@db01 ~]#cat /application/redis/conf/redis.conf 
appendonly yes
[root@db01 ~]#
[root@db01 ~]#docker restart redis
redis

7 view log:

docker logs -f redis

2. Build cluster (three main)

1 create three redis containers

#Create first
docker create --name redis-node1 -v /application/redis/data/node1:/data -p 6379:6379 redis --cluster-enabled yes --cluster-config-file nodes-node-1.conf
#Create a second
docker create --name redis-node2 -v /application/redis/data/node2:/data -p 6380:6379 redis --cluster-enabled yes --cluster-config-file nodes-node-2.conf
#Create a third
docker create --name redis-node3 -v /application/redis/data/node3:/data -p 6381:6379 redis --cluster-enabled yes --cluster-config-file nodes-node-3.conf
#inspect
[root@db01 ~]#docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS    PORTS     NAMES
8f9dd4d7ef23   redis     "docker-entrypoint.s..."   24 seconds ago   Created             redis-node3
510843981a9d   redis     "docker-entrypoint.s..."   34 seconds ago   Created             redis-node2
15f6563cdde0   redis     "docker-entrypoint.s..."   2 minutes ago    Created             redis-node1

2 start up container:

#Start container
[root@db01 ~]#docker start redis-node1 redis-node2 redis-node3

3. Cluster formation:

1) View the ip node information allocated by three Redis in Docker:

[root@db01 ~]#docker inspect redis-node1
"IPAddress": "172.31.0.2",
[root@db01 ~]#docker inspect redis-node2
"IPAddress": "172.31.0.3",
[root@db01 ~]#docker inspect redis-node3
"IPAddress": "172.31.0.4",

2) Enter the container to form a cluster

#Enter redis-node1 container
[root@db01 ~]#docker exec -it redis-node1 /bin/bash
#Execute the build cluster command
root@15f6563cdde0:/data# redis-cli --cluster create 172.31.0.2:6379 172.31.0.3:6379 172.31.0.4:6379 --cluster-replicas 0
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: d7787de362dc6665e6eaf419210539befbb5e7b4 172.31.0.2:6379
   slots:[0-5460] (5461 slots) master
M: 692a4be4742138a64b53d3c319c59c5e36eb7e46 172.31.0.3:6379
   slots:[5461-10922] (5462 slots) master
M: 7407c25542d23791f325ccadbfb48c49eae8e63f 172.31.0.4:6379
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes #Enter yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 172.31.0.2:6379)
M: d7787de362dc6665e6eaf419210539befbb5e7b4 172.31.0.2:6379
   slots:[0-5460] (5461 slots) master
M: 7407c25542d23791f325ccadbfb48c49eae8e63f 172.31.0.4:6379
   slots:[10923-16383] (5461 slots) master
M: 692a4be4742138a64b53d3c319c59c5e36eb7e46 172.31.0.3:6379
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@15f6563cdde0:/data# 

3) Similarly, enter redis-node2 and redis-node3 nodes to form clusters:

[root@db01 ~]#docker exec -it redis-node2 /bin/bash

5. Solve the pain point that Internet projects cannot access redis cluster:

Solution:

Let Docker use the network connection type of host mode. The container created by Docker in host mode does not have its own independent network namespace. It shares a network space with the physical machine, and then can share all ports and IP S of the physical machine. In this way, the public network can directly access the container, although this method has potential security risks, But at present, no other feasible model has been found

1) Stop a running container

[root@db01 ~]#docker stop redis-node1 redis-node2 redis-node3

2) Delete previously created containers

[root@db01 ~]#docker rm redis-node1 redis-node2 redis-node3

3) Empty the profile created above:

[root@db01 ~]#rm -rf /application/redis/data/node*

4) Recreate the container based on the host mode

Different from the previous create 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 external container port services need to be shared at this time, only exposed Ports - p 6379, - p 6380, etc. need to be specified.

#Create first
[root@db01 ~]#docker create --name redis-node1 -v /application/redis/data/node1:/data --net host redis --cluster-enabled yes --cluster-config-file nodes-node-1.conf --port 6379
#Create a second
[root@db01 ~]#docker create --name redis-node2 -v /application/redis/data/node2:/data --net host redis --cluster-enabled yes --cluster-config-file nodes-node-2.conf --port 6380
#Create a third
[root@db01 ~]#docker create --name redis-node3 -v /application/redis/data/node3:/data --net host redis --cluster-enabled yes --cluster-config-file nodes-node-3.conf --port 6381

[root@db01 ~]#

5) Build a cluster:

#Start command
[root@db01 ~]#docker start redis-node1 redis-node2 redis-node3
#Enter a container
[root@db01 ~]#docker exec -it redis-node1 /bin/bash
#Execute the build cluster command
root@db01:/data# redis-cli --cluster create 10.0.0.51:6379 10.0.0.51:6380 10.0.0.51:6381 --cluster-replicas 0

6) View cluster information

127.0.0.1:6379> cluster nodes
bb96dfd6d76e239f0f98d5247b8c92c7d1befdfa 10.0.0.51:6380@16380 master - 0 1642107589149 2 connected 5461-10922
1eacd70c6592416a5fe212f4d0300cf280633742 10.0.0.51:6379@16379 myself,master - 0 1642107587000 1 connected 0-5460
00b15024e2acc9070ab866ba13085cd2316a156e 10.0.0.51:6381@16381 master - 0 1642107590175 3 connected 10923-16383
127.0.0.1:6379> 

7) Test cluster:

root@db01:/data# redis-cli -c               
127.0.0.1:6379> set tyjs09 123456
-> Redirected to slot [9225] located at 10.0.0.51:6380
OK
10.0.0.51:6380> get tyjs09
"123456"
10.0.0.51:6380> 

So far, the Docker based redis cluster single replica mode has been built. The three redis in this paper are master nodes. The multi replica and master-slave architecture are highly available, which will be supplemented later.

 

Topics: Redis