美文网首页Docker 基础
Docker 基础 ( 二十 ) 部署Redis集群

Docker 基础 ( 二十 ) 部署Redis集群

作者: yjtuuige | 来源:发表于2021-07-06 08:44 被阅读0次
    • 创建网卡:docker network create redis --subnet 172.36.0.0/16
    [root@VM-0-6-centos ~]# docker network create redis --subnet 172.36.0.0/16
    19dd16ec85ca189a1a317c809969d93e84eaf43810556dd5510f4458318bd652
    
    • 查看网络:docker network ls
    [root@VM-0-6-centos ~]# docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    cb59eddef222   bridge    bridge    local
    c86310353602   host      host      local
    8e8d25d2e62d   mynet     bridge    local
    1ab8f2e4028d   none      null      local
    19dd16ec85ca   redis     bridge    local    //  自定义网络
    
    • 查看定义网络信息:docker network inspect redis
    [root@VM-0-6-centos ~]# docker network inspect redis
    [
        {
            "Name": "redis",
            "Id": "19dd16ec85ca189a1a317c809969d93e84eaf43810556dd5510f4458318bd652",
            "Created": "2021-07-04T17:04:27.961331931+08:00",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "172.36.0.0/16"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {},
            "Options": {},
            "Labels": {}
        }
    ]
    
    • 通过脚本创建六个 redis 配置
    [root@VM-0-6-centos ~]# for port in $(seq 1 6);\
    > do \
    > mkdir -p /mydata/redis/node-${port}/conf
    > cat << EOF >> /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.36.0.1${port}
    > cluster-announce-port 6379
    > cluster-announce-bus-port 16379
    > appendonly yes
    > EOF
    > done
    
    • 查看目录:
    [root@VM-0-6-centos ~]# cd /mydata/redis
    [root@VM-0-6-centos redis]# ls
    node-1  node-2  node-3  node-4  node-5  node-6
    
    • 查看其中一个配置信息
    [root@VM-0-6-centos redis]# cd node-1
    [root@VM-0-6-centos node-1]# ls
    conf
    [root@VM-0-6-centos node-1]# cd conf
    [root@VM-0-6-centos conf]# ls
    redis.conf
    [root@VM-0-6-centos conf]# cat 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.36.0.11
    cluster-announce-port 6379
    cluster-announce-bus-port 16379
    appendonly yes
    
    • 启动单个 redis 容器:
    // 按格式直接复制,粘贴到命令符下
    docker run -p 6371:6379 -p 16671:16379 --name redis-1 \
    -v /mydata/redis/node-1/data:/data \
    -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
    -d --net redis --ip 172.36.0.11 redis redis-server /etc/redis/redis.conf
    
    [root@VM-0-6-centos conf]# docker run -p 6371:6379 -p 16671:16379 --name redis-1 \
    > -v /mydata/redis/node-1/data:/data \
    > -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
    > -d --net redis --ip 172.36.0.11 redis redis-server /etc/redis/redis.conf
    Unable to find image 'redis:latest' locally
    latest: Pulling from library/redis
    b4d181a07f80: Pull complete 
    86e428f79bcb: Pull complete 
    ba0d0a025810: Pull complete 
    ba9292c6f77e: Pull complete 
    b96c0d1da602: Pull complete 
    5e4b46455da3: Pull complete 
    Digest: sha256:7c540ceff53f0522f6b1c264d8142df08316173d103586ddf51ed91ca49deec8
    Status: Downloaded newer image for redis:latest
    deb3b531684ceff216644460e5957031bce49bb728cb1f851f9f383a86a5fe66
    
    • 或通过脚本运行六个 redis
    // 按格式直接复制,粘贴到命令符下
    for port in $(seq 1 6);\
    do \
    docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
    -v /mydata/redis/node-${port}/data:/data \
    -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
    -d --net redis --ip 172.36.0.1${port} redis redis-server /etc/redis/redis.conf
    done
    
    [root@VM-0-6-centos ~]# for port in $(seq 1 6);\
    > do \
    > docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
    > -v /mydata/redis/node-${port}/data:/data \
    > -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
    > -d --net redis --ip 172.36.0.1${port} redis redis-server /etc/redis/redis.conf
    > done
    Unable to find image 'redis:latest' locally
    latest: Pulling from library/redis
    b4d181a07f80: Already exists 
    86e428f79bcb: Already exists 
    ba0d0a025810: Already exists 
    ba9292c6f77e: Already exists 
    b96c0d1da602: Already exists 
    5e4b46455da3: Already exists 
    Digest: sha256:7c540ceff53f0522f6b1c264d8142df08316173d103586ddf51ed91ca49deec8
    Status: Downloaded newer image for redis:latest
    ec6875b95a36c5226b02f4cf51e56211a534556d95e87a8010cd91c63304845c
    933ea98b5d86e542b7ff5f2aad1ba02a2ee4f6645d734fb4b20e3c3483bd1035
    fde579b61f2193550fd553c624cb3301fd2cd15c350b3e6bad5475d5db8f3cd6
    ed76640673502954f7d31d13bcae3b2db646c4f2865f5033775d56702805a6eb
    c63b8fa9b41548eb3607f9eb125e9f4d1873c5012b830c3fa3ab35ae8f7c99cd
    7199b5bba12e0d5925dfc7026f4edc3e93b381464feb10752ec16d91dcc369fe
    
    • 查看容器:docker ps
    [root@VM-0-6-centos ~]# docker ps
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                                                      NAMES
    7199b5bba12e   redis     "docker-entrypoint.s…"   9 seconds ago    Up 8 seconds    0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16676->16379/tcp, :::16676->16379/tcp   redis-6
    c63b8fa9b415   redis     "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds    0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16675->16379/tcp, :::16675->16379/tcp   redis-5
    ed7664067350   redis     "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds    0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16674->16379/tcp, :::16674->16379/tcp   redis-4
    fde579b61f21   redis     "docker-entrypoint.s…"   11 seconds ago   Up 10 seconds   0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16673->16379/tcp, :::16673->16379/tcp   redis-3
    933ea98b5d86   redis     "docker-entrypoint.s…"   12 seconds ago   Up 11 seconds   0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16672->16379/tcp, :::16672->16379/tcp   redis-2
    ec6875b95a36   redis     "docker-entrypoint.s…"   13 seconds ago   Up 12 seconds   0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16671->16379/tcp, :::16671->16379/tcp   redis-1
    
    • 进入其中一个容器:docker exec -it redis-1 /bin/sh ( redis默认没有bash)
    [root@VM-0-6-centos ~]# docker exec -it redis-1 /bin/sh
    #
    
    • 配置集群:
      redis-cli --cluster create 172.36.0.11:6379 172.36.0.12:6379 172.36.0.13:6379 172.36.0.14:6379 172.36.0.15:6379 172.36.0.16:6379 --cluster-replicas 1
    [root@VM-0-6-centos ~]# docker exec -it redis-1 /bin/sh
    # redis-cli --cluster create 172.36.0.11:6379 172.36.0.12:6379 172.36.0.13:6379 172.36.0.14:6379 172.36.0.15:6379 172.36.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.36.0.15:6379 to 172.36.0.11:6379
    Adding replica 172.36.0.16:6379 to 172.36.0.12:6379
    Adding replica 172.36.0.14:6379 to 172.36.0.13:6379
    M: 38ea8512a00e8b738e058a808d8532f07b4974d5 172.36.0.11:6379
       slots:[0-5460] (5461 slots) master
    M: 65c85251d291925afb607bdf93c2ac24e19affd7 172.36.0.12:6379
       slots:[5461-10922] (5462 slots) master
    M: 8e51ddb832230925e3d559c8d3f83a87192759af 172.36.0.13:6379
       slots:[10923-16383] (5461 slots) master
    S: 540ec67115eda524a78d329855d0288d106a54ab 172.36.0.14:6379
       replicates 8e51ddb832230925e3d559c8d3f83a87192759af
    S: 58a78ca2ecf2d057f5b1dea4257dd71328ff68d1 172.36.0.15:6379
       replicates 38ea8512a00e8b738e058a808d8532f07b4974d5
    S: 235d08a53a2a938f35af6dbf38ee9ce6b51ce4ae 172.36.0.16:6379
       replicates 65c85251d291925afb607bdf93c2ac24e19affd7
    Can I set the above configuration? (type 'yes' to accept): 
    
    • 提示:是否按此配置,输入 yes 即可
    Can I set 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 for the cluster to join
    .
    >>> Performing Cluster Check (using node 172.36.0.11:6379)
    M: 38ea8512a00e8b738e058a808d8532f07b4974d5 172.36.0.11:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 8e51ddb832230925e3d559c8d3f83a87192759af 172.36.0.13:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 58a78ca2ecf2d057f5b1dea4257dd71328ff68d1 172.36.0.15:6379
       slots: (0 slots) slave
       replicates 38ea8512a00e8b738e058a808d8532f07b4974d5
    S: 540ec67115eda524a78d329855d0288d106a54ab 172.36.0.14:6379
       slots: (0 slots) slave
       replicates 8e51ddb832230925e3d559c8d3f83a87192759af
    M: 65c85251d291925afb607bdf93c2ac24e19affd7 172.36.0.12:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 235d08a53a2a938f35af6dbf38ee9ce6b51ce4ae 172.36.0.16:6379
       slots: (0 slots) slave
       replicates 65c85251d291925afb607bdf93c2ac24e19affd7
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    # 
    

    配置完成

    测试集群:

    # redis-cli -c      // redis-cli -c连接集群  redis-cli 连接单机
    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:647
    cluster_stats_messages_pong_sent:649
    cluster_stats_messages_sent:1296
    cluster_stats_messages_ping_received:644
    cluster_stats_messages_pong_received:647
    cluster_stats_messages_meet_received:5
    cluster_stats_messages_received:1296
    127.0.0.1:6379> cluster nodes      // 查看节点信息
    8e51ddb832230925e3d559c8d3f83a87192759af 172.36.0.13:6379@16379 master - 0 1625397547555 3 connected 10923-16383
    38ea8512a00e8b738e058a808d8532f07b4974d5 172.36.0.11:6379@16379 myself,master - 0 1625397548000 1 connected 0-5460
    58a78ca2ecf2d057f5b1dea4257dd71328ff68d1 172.36.0.15:6379@16379 slave 38ea8512a00e8b738e058a808d8532f07b4974d5 0 1625397548056 1 connected
    540ec67115eda524a78d329855d0288d106a54ab 172.36.0.14:6379@16379 slave 8e51ddb832230925e3d559c8d3f83a87192759af 0 1625397549561 3 connected
    65c85251d291925afb607bdf93c2ac24e19affd7 172.36.0.12:6379@16379 master - 0 1625397549561 2 connected 5461-10922
    235d08a53a2a938f35af6dbf38ee9ce6b51ce4ae 172.36.0.16:6379@16379 slave 65c85251d291925afb607bdf93c2ac24e19affd7 0 1625397549561 2 connected
    127.0.0.1:6379> 
    
    • 设置一个值,查找对应的主机
    127.0.0.1:6379> set a b
    // 响应的主机为 172.36.0.13(redis-3),对应从机为 172.36.0.14(redis-4)
    -> Redirected to slot [15495] located at 172.36.0.13:6379
    OK
    
    • redis-3 容器停止
    [root@VM-0-6-centos ~]# docker stop redis-3
    redis-3
    
    • 主机停止后,查看从机是否替换成功
    # redis-cli -c        // 连接集群
    127.0.0.1:6379> get a        // 获取之前输入的值
    -> Redirected to slot [15495] located at 172.36.0.14:6379
    "b"
    172.36.0.14:6379> cluster nodes        // 查看节点信息
    // 172.36.0.13(redis-3)被对应的从机 172.36.0.14(redis-4)自动替换成功
    540ec67115eda524a78d329855d0288d106a54ab 172.36.0.14:6379@16379 myself,master - 0 1625406048000 7 connected 10923-16383
    235d08a53a2a938f35af6dbf38ee9ce6b51ce4ae 172.36.0.16:6379@16379 slave 65c85251d291925afb607bdf93c2ac24e19affd7 0 1625406047146 2 connected
    65c85251d291925afb607bdf93c2ac24e19affd7 172.36.0.12:6379@16379 master - 0 1625406048149 2 connected 5461-10922
    58a78ca2ecf2d057f5b1dea4257dd71328ff68d1 172.36.0.15:6379@16379 slave 38ea8512a00e8b738e058a808d8532f07b4974d5 0 1625406049153 1 connected
    38ea8512a00e8b738e058a808d8532f07b4974d5 172.36.0.11:6379@16379 master - 0 1625406047146 1 connected 0-5460
    // 172.36.0.13(redis-3),故障
    8e51ddb832230925e3d559c8d3f83a87192759af 172.36.0.13:6379@16379 master,fail - 1625405967351 1625405964842 3 connected
    172.36.0.14:6379> 
    

    docker搭建redis集群完成,测试成功

    相关文章

      网友评论

        本文标题:Docker 基础 ( 二十 ) 部署Redis集群

        本文链接:https://www.haomeiwen.com/subject/vkxvultx.html