Docker 部署Redis集群

docker  

集群架构图

部署过程

  • 创建独立网段
docker network create redis --subnet 172.38.0.0/16  
  • 查看网段
docker network inspect redis

[
    {
        "Name": "redis",
        "Id": "7c48ab17f7977136433421c13997e9f8756be3582d056eb9550896072adbe244",
        "Created": "2022-03-01T14:00:21.324965991+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.38.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
  • 通过脚本创建六个redis配置
#!/bin/bash
for port in {1..6}  
do  
    reids_conf_dir=/data/docker/redis/node-${port}/conf
    mkdir -p $reids_conf_dir
    touch $reids_conf_dir/redis.conf
    cat << EOF > $reids_conf_dir/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  
  • 启动Redis
#r-m1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v /data/docker/redis/node-1/data:/data -v /data/docker/redis/node-1/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.11 redis redis-server /etc/redis/redis.conf  
#r-m2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 -v /data/docker/redis/node-2/data:/data -v /data/docker/redis/node-2/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.12 redis redis-server /etc/redis/redis.conf  
#r-m3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 -v /data/docker/redis/node-3/data:/data -v /data/docker/redis/node-3/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.13 redis redis-server /etc/redis/redis.conf

#r-s1
docker run -p 6374:6379 -p 16374:16379 --name redis-4 -v /data/docker/redis/node-4/data:/data -v /data/docker/redis/node-4/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.14 redis redis-server /etc/redis/redis.conf  
#r-s2
docker run -p 6375:6379 -p 16375:16379 --name redis-5 -v /data/docker/redis/node-5/data:/data -v /data/docker/redis/node-5/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.15 redis redis-server /etc/redis/redis.conf  
#r-s3
docker run -p 6376:6379 -p 16376:16379 --name redis-6 -v /data/docker/redis/node-6/data:/data -v /data/docker/redis/node-6/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.16 redis redis-server /etc/redis/redis.conf  
  • 查看6个redis容器是否启动
[root@centos72 redis]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                                              NAMES  
720bf426dba9   redis     "docker-entrypoint.s…"   5 seconds ago        Up 3 seconds        0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp   redis-6  
7119b1cc7975   redis     "docker-entrypoint.s…"   6 seconds ago        Up 4 seconds        0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp   redis-5  
4be4887030ed   redis     "docker-entrypoint.s…"   7 seconds ago        Up 5 seconds        0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp   redis-4  
acf95c6d551c   redis     "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp   redis-3  
d56e03756c27   redis     "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp   redis-2  
b64c87b706b1   redis     "docker-entrypoint.s…"   3 minutes ago        Up 3 minutes        0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp   redis-1  

创建集群

#进入redis-1
[root@centos72 ~]# docker exec -it redis-1 /bin/bash
#创建cluster
root@b64c87b706b1:/data# 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  
>>> Performing hash slots 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 to 172.38.0.13:6379  
M: 181cd3b1b8a206f06b5651954916ba52f6af0c42 172.38.0.11:6379  
   slots:[0-5460] (5461 slots) master
M: 0de0065f12500b2b1bfbd42dbb52157970bf3eb0 172.38.0.12:6379  
   slots:[5461-10922] (5462 slots) master
M: 40e458a0458234bd524a4dbcc398077ce846ecef 172.38.0.13:6379  
   slots:[10923-16383] (5461 slots) master
S: 271201f6a268845bd5eff78ebdc181f73b26462e 172.38.0.14:6379  
   replicates 40e458a0458234bd524a4dbcc398077ce846ecef
S: 5330e9aedfa5169731ecfeb04e852ceeed8b65c9 172.38.0.15:6379  
   replicates 181cd3b1b8a206f06b5651954916ba52f6af0c42
S: 062fb85f1ce1a6ba430ecedb3aed9203a780bfa3 172.38.0.16:6379  
   replicates 0de0065f12500b2b1bfbd42dbb52157970bf3eb0
Can I set the above configuration? (type 'yes' to accept): yes  
# 是否按3主3从创建,输入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.38.0.11:6379)
M: 181cd3b1b8a206f06b5651954916ba52f6af0c42 172.38.0.11:6379  
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 062fb85f1ce1a6ba430ecedb3aed9203a780bfa3 172.38.0.16:6379  
   slots: (0 slots) slave
   replicates 0de0065f12500b2b1bfbd42dbb52157970bf3eb0
M: 0de0065f12500b2b1bfbd42dbb52157970bf3eb0 172.38.0.12:6379  
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 40e458a0458234bd524a4dbcc398077ce846ecef 172.38.0.13:6379  
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 271201f6a268845bd5eff78ebdc181f73b26462e 172.38.0.14:6379  
   slots: (0 slots) slave
   replicates 40e458a0458234bd524a4dbcc398077ce846ecef
S: 5330e9aedfa5169731ecfeb04e852ceeed8b65c9 172.38.0.15:6379  
   slots: (0 slots) slave
   replicates 181cd3b1b8a206f06b5651954916ba52f6af0c42
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 查看集群信息
redic-cli -c  
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:7  
cluster_my_epoch:1  
cluster_stats_messages_ping_sent:645  
cluster_stats_messages_pong_sent:655  
cluster_stats_messages_auth-ack_sent:1  
cluster_stats_messages_update_sent:1  
cluster_stats_messages_sent:1302  
cluster_stats_messages_ping_received:650  
cluster_stats_messages_pong_received:645  
cluster_stats_messages_meet_received:5  
cluster_stats_messages_fail_received:1  
cluster_stats_messages_auth-req_received:1  
cluster_stats_messages_received:1302


127.0.0.1:6379> cluster nodes  
062fb85f1ce1a6ba430ecedb3aed9203a780bfa3 172.38.0.16:6379@16379 slave 0de0065f12500b2b1bfbd42dbb52157970bf3eb0 0 1646115968000 2 connected  
0de0065f12500b2b1bfbd42dbb52157970bf3eb0 172.38.0.12:6379@16379 master - 0 1646115968000 2 connected 5461-10922  
40e458a0458234bd524a4dbcc398077ce846ecef 172.38.0.13:6379@16379 slave 271201f6a268845bd5eff78ebdc181f73b26462e 0 1646115968678 7 connected  
181cd3b1b8a206f06b5651954916ba52f6af0c42 172.38.0.11:6379@16379 myself,master - 0 1646115968000 1 connected 0-5460  
271201f6a268845bd5eff78ebdc181f73b26462e 172.38.0.14:6379@16379 master - 0 1646115968577 7 connected 10923-16383  
5330e9aedfa5169731ecfeb04e852ceeed8b65c9 172.38.0.15:6379@16379 slave 181cd3b1b8a206f06b5651954916ba52f6af0c42 0 1646115968578 1 connected  
127.0.0.1:6379>  
  • 这时可以set一个值
root@b64c87b706b1:/data# redis-cli -c  
127.0.0.1:6379> set h 1  
-> Redirected to slot [11694] located at 172.38.0.14:6379
OK  
  • 可以看出这个值被分配到了 172.38.0.14,redis-4,手动关掉这个节点
docker stop redis-4  
  • 再次查看集群节点状态
127.0.0.1:6379> CLUSTER nodes  
062fb85f1ce1a6ba430ecedb3aed9203a780bfa3 172.38.0.16:6379@16379 slave 0de0065f12500b2b1bfbd42dbb52157970bf3eb0 0 1646116183837 2 connected  
0de0065f12500b2b1bfbd42dbb52157970bf3eb0 172.38.0.12:6379@16379 master - 0 1646116183000 2 connected 5461-10922  
40e458a0458234bd524a4dbcc398077ce846ecef 172.38.0.13:6379@16379 master - 0  
1646116183332 8 connected 10923-16383  
# 这时redis-3被提升为master
181cd3b1b8a206f06b5651954916ba52f6af0c42 172.38.0.11:6379@16379 myself,master - 0 1646116181000 1 connected 0-5460  
271201f6a268845bd5eff78ebdc181f73b26462e 172.38.0.14:6379@16379 master,fail - 1646116175581 1646116173000 7 connected  
# redis-4 已经挂了
5330e9aedfa5169731ecfeb04e852ceeed8b65c9 172.38.0.15:6379@16379 slave 181cd3b1b8a206f06b5651954916ba52f6af0c42 0 1646116183534 1 connected  
  • redis-4 已经挂了,redis-3已经提升为master,再次get h,发现还是可以取到值
127.0.0.1:6379> get h  
-> Redirected to slot [11694] located at 172.38.0.13:6379
"1"
172.38.0.13:6379>