1. Prepare tools

Install Docker (fromwebsite)

(1) Install required software packages

$ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
Copy the code

(2) Use the following command to set up the stable repository

$ sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
Copy the code

(3) Install docker CE (Docker community Edition)

$ sudo yum install docker-ce
Copy the code

(4) start the docker

$ sudo systemctl start docker
Copy the code

Obtain the redis image from the Docker library (as of 03-27, the latest version is 5.0.4)

$ docker pull redis
Copy the code

At this point, all the tools of Redis Cluster on Docker are ready. We can enter Docker images on the command line to view the installed images

Second, cluster building

Create a Redis container

##### (1) Create redis configuration file (redis-cluster.tmpl)

I will create a folder redis-cluster under /home/redis-cluster and a file redis-cluster. TMPL under /home/redis-cluster and copy the following contents. (Note: path is customizable, I used /home/redis-cluster)

Port ${port} ## protected-mode no ## cluster-enabled yes ##cluster cluster-config-file nodes.conf ##cluster configuration name XX ## Cluster-node-timeout 5000 ## cluster-node-ip 192.168.xx.XX ## Use the gateway IP address instead of cluster-node-port to assign IP addresses to network adapters on nodes ${PORT} ## cluster-announce-bus-port 1${PORT} ## nodebus PORT appendonly yes ## Persistent modeCopy the code

You can view the information by running commands

[root@node-01 redis-cluster]# cat /home/redis-cluster/redis-cluster.tmpl port ${PORT} protected-mode no cluster-enabled Yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-node-ip 192.168.1.221 cluster-nose-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly yesCopy the code

Note: This template file is a common file of cluster nodes. ${PORT} reads the command line variable IP and replaces it with the IP assigned by the network adapter to ensure that the node configuration file is consistent except for PORT and IP.

(2) Create a custom network
$ docker network create redis-net
Copy the code

Docker network ls ##

[root@node-01 conf]# docker network ls 
NETWORK ID NAME DRIVER SCOPE 7f804fa5fbd2 redis-net bridge local
Copy the code

Note: The purpose of creating redis-net virtual network card is to enable the Docker container to bridge the network with the host (Centos7) and indirectly connect with the outside world

(3) View the gateway IP address of the redis-net virtual nic
/ root @ node - 01 conf # docker network inspect redis - net | grep "Gateway" | grep -- color = auto - P '(\ d {1, 3}. {3} \ d {1, 3} '-o 172.21.0.1Copy the code

Note: docker network inspect network-name Displays the configuration information corresponding to network-name (GERP filters gateway configuration lines and filters IP addresses)

(4) Generate conf and data targets and configuration information in /home/redis-cluster
$ for port in `seq 7000 7005`; do \ mkdir -p ./${port}/conf \ && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p  ./${port}/data; \ doneCopy the code

Six folders, ranging from 7000 to 7005, are generated. Each folder contains the data and conf folders, and conf contains the redis.conf configuration file

Create 6 Redis containers
$ for port in `seq 7000 7005`; do \
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v /home/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /home/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
done
Copy the code
Note: the command is translated into loops 7010-7015 running redis docker run-dDaemon mode --restart always keeps the container started --name Redis-710 * Container name --net Redis-net container uses virtual network card -p to specify the port mapping between the host machine and the container 701*:701* -p Specify host and container redis bus port mapping 1701*:1701* -- Privileged =true -v /home/redis-cluster/701*/conf/redis.conf:/usr/local/etc/redis/redis.conf pays the right to mount the host 701* node file to the container /usr/local/etc/redis/redis.conf file --privileged=true -v /home/redis-cluster/${port}Sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; The container starts the Redis server based on the mounted configuration fileCopy the code
6. Run the docker ps command to view the information about the six containers just generated
[root@node-01 redis-cluster]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15c479074b87 redis "docker-entrypoint..." 10 seconds ago Up 9 seconds 0.0.0.0:7005->7005/ TCP, 6379/ TCP, 0.0.0.0:17005->17005/ TCP redis-7005 45bec33c2C35 redis "docker-entryPoint..." 11 seconds ago Up 10 seconds 0.0.0.0:7004->7004/ TCP, 6379/ TCP, 0.0.0.0:17004->17004/ TCP redIS-7004 45482d9e5bb8 redis "docker-entryPoint..." 11 seconds ago Up 10 seconds 0.0.0.0:7003->7003/ TCP, 6379/ TCP, 0.0.0.0:17003->17003/ TCP redis-7003 f633d5C767c9 redis "docker-entryPoint..." 0.0.0.0:17003->17003/ TCP redis-7003 f633d5C767c9 11 seconds ago Up 10 seconds 0.0.0.0:7002->7002/ TCP, 6379/ TCP, 0.0.0.0:17002->17002/ TCP redis-7002 eEFc0d49fedf redis "docker-entryPoint..." 11 seconds ago Up 10 seconds 0.0.0.0:7001->7001/ TCP, 6379/ TCP, 0.0.0.0:17001->17001/ TCP redis-7001 58b311e5dbcb redis "docker-entryPoint" 0.0.0.0:17001->17001/ TCP redis-7001 58b311e5dbcb redis "docker-entryPoint... 12 seconds ago Up 11 seconds 0.0.0.0:7000->7000/ TCP, 6379/ TCP, 0.0.0.0:17000->17000/ TCP redIS-7000Copy the code
7. Check the IP address assigned to the container
[root@node-01 conf]# docker network inspect redis-net [ { "Name": "redis-net", "Id": "7f804fa5fbd2473f1e6e9783ac52aa980c355e3253b15a28e10f8793c46fc39b", "Created": "2019-03-27T12:59:14.45569686+08:00", "Scope": "local", "EnableIPv6": false, "IPAM": {"Driver": "Default", "Options" : {}, "Config" : [{" Subnet configures ":" 172.21.0.0/16 ", "Gateway" : "172.21.0.1}]}," Internal ": false, "Attachable": false, "Containers": { "449c38916b4ef919d1b1828da3f69e0f5256eb2023204f25f69fbb8c449f2163": { "Name": "redis-7005", "EndpointID": "5c89e6fdd0e983d6d2b950c9bc41616920265dda93f2852650ca5e9da1da22b1", "MacAddress": "02:42: ac: 15:00:07", "IPv4Address" : "172.21.0.7/16", "IPv6Address" : "" }, "600aa2f84dfbe72a19caf5dc684cb7691e89a43753f70d74355468b7659caa23": { "Name": "redis-7000", "EndpointID": "75873081222c7a2eedc5f3ee639df5cc7645790012dd72e34c97fd97dcb5cf47", "MacAddress": "02:42:ac:15:00:02", "IPv4Address": "" IPv6Address 172.21.0.2/16", ":" "}, "78 aeae8b2d381d3789848f45f766dd01d7c0d5443505920decf41fce751f765d" : {" Name ": "redis-7002", "EndpointID": "a70d816cd9e3650b1d10507a85fa85c0c2e143ba99c0974fb000ec87ad5b173d", "MacAddress": "02:42: ac: 15:00:04", "IPv4Address" : "172.21.0.4/16", "IPv6Address" : "" }, "7ecd2e1372a2bc15cb89fad856ab410757b49936f662ce170990c04e1861037d": { "Name": "redis-7004", "EndpointID": "f8cdb522601e9667ec06dc0dc0d39b46331b3d24cbea1155b00b73e600eca8c7", "MacAddress": "02:42:ac:15:00:06", "IPv4Address": "" IPv6Address 172.21.0.6/16", ":" "}, "bd8254045c66bc6569c9a585b58cb028e864b6c7f16d836009debd3a3350913c" : {" Name ": "redis-7003", "EndpointID": "6a25ff0046e6d6575579cdf08bccbb2a62b29460e3932de3f8e476bb908fdc9c", "MacAddress": "02:42: ac: 15:00:05", "IPv4Address" : "172.21.0.5/16", "IPv6Address" : "" }, "cfef6ca3aeea9d3b4fa527538fbe2a2f79aac25d52921b4edc31839b6f330887": { "Name": "redis-7001", "EndpointID": "98a36b61bc6f50f78444a6cefa5e9999f5366e72425602511bc95f602ef0dc93", "MacAddress": "02:42:ac:15:00:03", "IPv4Address": "" IPv6Address 172.21.0.3/16", ":" "}}, "Options" : "Labels" : {} {},}]Copy the code

Note: Each container has an IP address. You need to modify the node configuration file redis.conf and restart the container.

(8) Modify the configuration file of the host mount directory
Vim /home/redis-cluster/700*/conf/redis.conf # Wq Exit port 7000 protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 Cluster-announce-ip 172.21.0.2 cluster-announce-port 7000 cluster-announce-bus-port 17000 appendonly yesCopy the code

Start the cluster

Enter a node
docker exec -it redis-7000 bash
Copy the code
To create a cluster using Redis 5, Redis -cli simply type:
Redis -cli --cluster create 172.21.0.2:7000 172.21.0.3:7001 172.21.0.4:7002 172.21.0.5:7003 172.21.0.6:7004 172.21.0.7:7005-1 - cluster - replicasCopy the code
At this point, the cluster is created.
[root@node-01 data]# docker exec -it redis-7000 bash
root@600aa2f84dfb:/data# redis-cli --cluster create 172.21.0.2:7000 172.21.0.3:7001 172.21.0.4:7002 172.21.0.5:7003 172.21.0.6:7004 172.21.0.7:7005-1 - cluster - replicas
>>> Performing hashslots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 172.21.0.6:7004 to 172.21.0.2:7000 Adding Replica 172.21.0.7:7005 to 172.21.0.3:7001 Adding Replica 172.21.0.5:7003 to 172.21.0.4:7002 M: 0 d403d866c4bdfa90f3a1e21b2a33356474014ee 172.21.0.2:7000 slots: [0-5460] (5461 slots) master M: Ab4bf4833926dd7ffca5f2d34ef754466730e7d7 172.21.0.3:7001 slots: [5461-10922] (5462 slots) master M: 846 f087ee5885f17d5a5c05d2eb2ba470441f700 172.21.0.4:7002 slots: [10923-16383] (5461 slots) master S: E44c2b96f1446b1860fe582547634b12a461407d 172.21.0.5:7003 replicates 846 f087ee5885f17d5a5c05d2eb2ba470441f700 S: 4 efa0a9b7b4b159807900efa5f86410e55c1d2b3 172.21.0.6:7004 replicates 0 d403d866c4bdfa90f3a1e21b2a33356474014ee S: 8 a616ee30a7104bcd341d57af825c6b5b30176ff 172.21.0.7:7005 replicates ab4bf4833926dd7ffca5f2d34ef754466730e7d7 Can Iset the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting forthe cluster to join .... >>> Performing Cluster Check (using node 172.21.0.2:7000) M: 0 d403d866c4bdfa90f3a1e21b2a33356474014ee 172.21.0.2:7000 slots: [0-5460] (5461 slots) master 1 additional up (s) s: E44c2b96f1446b1860fe582547634b12a461407d 172.21.0.5:7003 slots: (0 slots) slave replicates 846f087ee5885f17d5a5c05d2eb2ba470441f700 M: Ab4bf4833926dd7ffca5f2d34ef754466730e7d7 172.21.0.3:7001 slots: [5461-10922] (5462 slots) master 1 additional up (s) S: 8 a616ee30a7104bcd341d57af825c6b5b30176ff 172.21.0.7:7005 slots: (0 slots) slave replicates ab4bf4833926dd7ffca5f2d34ef754466730e7d7 M: 846 f087ee5885f17d5a5c05d2eb2ba470441f700 172.21.0.4:7002 slots: [10923-16383] (5461 slots) master 1 additional up (s) S: 4 efa0a9b7b4b159807900efa5f86410e55c1d2b3 172.21.0.6:7004 slots: (0 slots) slave replicates 0d403d866c4bdfa90f3a1e21b2a33356474014ee [OK] All nodes agree about slots configuration. >>> Checkfor open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Copy the code
Connect to the client to view primary and secondary information
root@600aa2f84dfb:/data# redis-cli -c -p 7000     127.0.0.1:7000 > info replication# ReplicationRole: master connected_slaves: 1 slave0: IP = 172.21.0.6, port = 7004, state = online, offset = 3556, lag = 1 master_replid:ef97265d34769b68096786c3e802fb3a13968fe9 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:3556 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:3556Copy the code

Other Matters needing attention

Host Open IP address of the host
for port in `seq 7000 7005`; do \  
firewall-cmd --zone=public --add-port=${port}/tcp --permanent  
done
# reload
firewall-cmd --reload
Copy the code

##### You need to pause the container and delete it to repeat step 6

for port in `seq 7000 7005`; do \
  docker stop redis-${port};
  docker rm redis-${port};
done
Copy the code