Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Docker learn | create Redis cluster
How does Docker create a Redis cluster
The preparatory work
Sharding + High availability + load balancing
TODO picturesCopy the code
Create network cards for the Redis cluster
$docker network create redis --subnet 172.38.0.0/16 docker network inspect redis [{"Name": "redis"."Id": "5e57da31eae5c3492d092139450fa8976abc15719d2682dc17be0ffc73a4e7e9"."Created": "The 2021-09-30 T00: meat. 939271085 + 08:00"."Scope": "local"."Driver": "bridge"."EnableIPv6": false."IPAM": {
"Driver": "default"."Options": {},
"Config": [{"Subnet": "172.18.0.0/16"."Gateway": "172.18.0.1"}},"Internal": false."Attachable": false."Ingress": false."ConfigFrom": {
"Network": ""
},
"ConfigOnly": false."Containers": {},
"Options": {},
"Labels": {}}]Copy the code
Create six Redis configurations from a Shell script
for port in $(seq 1 6); \ do \ mkdir -p /root/docker-test/redis/mydata/redis/node-${port}/conf touch /root/docker-test/redis/mydata/redis/node-${port}/conf/redis.conf cat << EOF > / root/docker - test/redis/mydata/redis/node - ${port} / conf/redis. Conf port 6379 bind 0.0.0.0 cluster - enabled yes Cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 172.38.0.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done vim /root/docker-test/redis/mydata/redis/node-1/conf/redis.confCopy the code
Start the Redis service
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \ -v /root/docker-test/redis/mydata/redis/node-1/data:/data \ -v / root/docker - test/redis/mydata/redis/node - 1 / conf/redis. Conf: / etc/redis/redis conf \ - d -.net redis - IP 172.38.0.11 Redis :5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6372:6379 -p 16372:16379 --name redis-2 \ -v /root/docker-test/redis/mydata/redis/node-2/data:/data \ -v / root/docker - test/redis/mydata/redis/node - 2 / conf/redis. Conf: / etc/redis/redis conf \ - d -.net redis - IP 172.38.0.12 Redis :5.0.9-alpine3.11 redis-server /etc/redis/redis. Conf docker run -p 6373:6379 -p 16373:16379 --name redis-3 \ -v /root/docker-test/redis/mydata/redis/node-3/data:/data \ -v / root/docker - test/redis/mydata/redis/node - 3 / conf/redis. Conf: / etc/redis/redis conf \ - d -.net redis - IP 172.38.0.13 Redis :5.0.9-alpine3.11 redis-server /etc/redis/redis /redis. Conf docker run -p 6374:6379 -p 16374:16379 --name redis-4 \ -v /root/docker-test/redis/mydata/redis/node-4/data:/data \ -v / root/docker - test/redis/mydata/redis/node - 4 / conf/redis. Conf: / etc/redis/redis conf \ - d -.net redis - IP 172.38.0.14 Redis :5.0.9-alpine3.11 redis-server /etc/redis/redis. Conf docker run -p 6375:6379 -p 16375:16379 --name redis-5 \ -v /root/docker-test/redis/mydata/redis/node-5/data:/data \ -v / root/docker - test/redis/mydata/redis/node - 5 / conf/redis. Conf: / etc/redis/redis conf \ - d -.net redis - IP 172.38.0.15 Redis :5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6376:6379 -p 16376:16379 --name redis-6 \ -v /root/docker-test/redis/mydata/redis/node-6/data:/data \ -v / root/docker - test/redis/mydata/redis/node - 6 / conf/redis. Conf: / etc/redis/redis conf \ - d -.net redis - IP 172.38.0.16 Redis: 5.0.9 - alpine3.11 redis server/etc/redis/redis. ConfCopy the code
Check the service
Docker PS CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7118FD848C2E Redis :5.0.9- AlPINE3.11"Docker - entrypoint. S..."37 seconds ago Up 35 seconds 0.0.0.0:6376->6379/ TCP, ::6376->6379/ TCP, 0.0.0.0:16376->16379/ TCP, : : : 16376-16379 / TCP redis - > 6 2 ffcadf0502c redis: 5.0.9 - alpine3.11"Docker - entrypoint. S..."40 seconds ago Up 39 seconds 0.0.0.0:6375->6379/ TCP, :::6375->6379/ TCP, 0.0.0.0:16375->16379/ TCP, : : : 16375-16379 / TCP redis - > 5 766 b6d549e34 redis: 5.0.9 - alpine3.11"Docker - entrypoint. S..."44 seconds ago Up 43 seconds 0.0.0.0:6374->6379/ TCP, ::6374->6379/ TCP, 0.0.0.0:16374->16379/ TCP, : : : 16374-16379 / TCP redis - > 4 1 df5978f6506 redis: 5.0.9 - alpine3.11"Docker - entrypoint. S..."About a minute ago Up About a minute 0.0.0.0:6373->6379/ TCP, ::6373->6379/ TCP, 0.0.0.0:16373->16379/ TCP, : : : 16373-16379 / TCP redis - > 3 5 c2a44097f1b redis: 5.0.9 - alpine3.11"Docker - entrypoint. S..."About a minute ago Up About a minute 0.0.0.0:6372->6379/ TCP, :::6372->6379/ TCP, 0.0.0.0:16372->16379/ TCP, : : : 16372-16379 / TCP redis - > 2 cb4b52984a05 redis: 5.0.9 - alpine3.11"Docker - entrypoint. S..."About a minute ago Up About a minute 0.0.0.0:6371->6379/ TCP, ::6371->6379/ TCP, 0.0.0.0:16371->16379/ TCP, :::16371->16379/tcp redis-1Copy the code
Create the cluster
$ docker exec-it redis-1 /bin/sh $redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1 /data# ls
appendonly.aof nodes.conf
>>> Performing hashslots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 172.38.0.15:6379 to 172.38.0.11:6379 Adding Replica 172.38.0.16:6379 to 172.38.0.12:6379 Adding replica 172.38.0.14:6379 The to 172.38.0.13:6379 M: c7ea33c6860fefd31eeb24d86f23868dee7d1235 172.38.0.11:6379 slots: [0-5460] (5461 slots) master M: 1687 d37587aa73fa878bf0f02ba1c07c87021a74 172.38.0.12:6379 slots: [5461-10922] (5462 slots) master M: D5bf0a7cc1d57d77db7ba84318b5ef981363161d 172.38.0.13:6379 slots: [10923-16383] (5461 slots) master S: 743 d7976dd1bbee2d93332a9d5a8510b1a8a0f9d 172.38.0.14:6379 replicates d5bf0a7cc1d57d77db7ba84318b5ef981363161d S: Eec9dcd743073a821375ed93931b54cd8646f31e 172.38.0.15:6379 replicates c7ea33c6860fefd31eeb24d86f23868dee7d1235 S: 78 bd3fed02a3640b917003c8ad76254a270ec4c4 172.38.0.16:6379 replicates 1687 d37587aa73fa878bf0f02ba1c07c87021a74 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.38.0.11:6379) M: C7ea33c6860fefd31eeb24d86f23868dee7d1235 172.38.0.11:6379 slots: [0-5460] (5461 slots) master 1 additional up (s) s: 743 d7976dd1bbee2d93332a9d5a8510b1a8a0f9d 172.38.0.14:6379 slots: (0 slots) slave replicates d5bf0a7cc1d57d77db7ba84318b5ef981363161d S: Eec9dcd743073a821375ed93931b54cd8646f31e 172.38.0.15:6379 slots: (0 slots) slave replicates c7ea33c6860fefd31eeb24d86f23868dee7d1235 M: D5bf0a7cc1d57d77db7ba84318b5ef981363161d 172.38.0.13:6379 slots: [10923-16383] (5461 slots) 1 additional master Up (s) s: 78 bd3fed02a3640b917003c8ad76254a270ec4c4 172.38.0.16:6379 slots: (0 slots) slave replicates 1687d37587aa73fa878bf0f02ba1c07c87021a74 M: 1687 d37587aa73fa878bf0f02ba1c07c87021a74 172.38.0.12:6379 slots: [5461-10922] (5462 slots) master 1 additional up (s) [OK] All nodes agree about slots configuration. >>> Checkfor open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
The cluster is successfully created
Copy the code
Viewing Cluster Information
$redis-cli -c $cluster info 127.0.0.1:6379> Cluster info Cluster_state: OK Cluster_SLOts_assigned :16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:257 cluster_stats_messages_pong_sent:247 cluster_stats_messages_sent:504 cluster_stats_messages_ping_received:242 cluster_stats_messages_pong_received:257 Cluster_stats_messages_meet_received :5 Cluster_stats_Messages_received :504 127.0.0.1:6379> $cluster NodesBoth master and slave are working properly743 d7976dd1bbee2d93332a9d5a8510b1a8a0f9d 172.38.0.14:6379 @ 16379 slave d5bf0a7cc1d57d77db7ba84318b5ef981363161d 0 1632937462545 4 connected eec9dcd743073a821375ed93931b54cd8646f31e 172.38.0.15:6379 @ 16379 slave c7ea33c6860fefd31eeb24d86f23868dee7d1235 0 1632937463000 5 connected d5bf0a7cc1d57d77db7ba84318b5ef981363161d 172.38.0.13: master 6379 @ 16379-0 1632937463000 3 connected bd3fed02a3640b917003c8ad76254a270ec4c4 10923-16383-78 172.38.0.16:6379 @ 16379 slave d37587aa73fa878bf0f02ba1c07c87021a74 0 1687 1632937462946 6 connected C7ea33c6860fefd31eeb24d86f23868dee7d1235 172.38.0.11:6379 @ 16379 myself, master 1632937462000 connected 1 0 0-5460 1687 d37587aa73fa878bf0f02ba1c07c87021a74 172.38.0.12:6379 @ 16379 master - 0 1632937463947 2 connected. 5461-10922Copy the code