V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
JasonLaw
V2EX  ›  Docker

由 Docker Redis 组成的 Redis cluster 在 macOS 上无法成功创建

  •  
  •   JasonLaw · Aug 27, 2020 · 2707 views
    This topic created in 2081 days ago, the information mentioned may be changed or developed.

    启动多个实例

    for i in {7000..7002}
    do
       docker run -d --name Redis-cache-cluster-node-${i} \
          --network=host \
          -v Redis-cache-cluster-node-${i}_data:/data \
          redis:6.0.6 \
          redis-server --port ${i} --cluster-enabled yes --cluster-config-file nodes.conf -- cluster-node-timeout 5000 --appendonly yes
    done
    

    创建 cluster

    docker run -it --rm --network=host redis \
    redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
    

    在 Ubuntu 上面,是可以成功创建 cluster 的,但是在 macOS 上面却不行,一直卡在“Waiting for the cluster to join”,到底是为什么呢?


    关于网络问题,我已经查阅了ip - Docker container doesn't expose ports when --net=host is mentioned in the docker run command - Stack OverflowBlock/disallow --net=host (host networking) on Mac OS · Issue #2716 · docker/for-mac,并且尝试过使用-p ${i}:${i} -p $((${i} + 10000)):$((${i} + 10000))替换--network=host,结果还是不行。

    Supplement 1  ·  Aug 27, 2020

    最后通过如下方法解决。

    docker network create --subnet=192.168.200.0/24 Redis-cache-cluster-network
    
    for i in {7000..7002}
    do
       docker run -d --name Redis-cache-cluster-node-${i} \
          --network=Redis-cache-cluster-network \
          --ip 192.168.200.$((${i} - 6990)) \
          -p ${i}:${i} -p $((${i} + 10000)):$((${i} + 10000)) \
          -v Redis-cache-cluster-node-${i}_data:/data \
          redis:6.0.6 \
          redis-server --port ${i} --cluster-enabled yes --cluster-config-file nodes.conf -- cluster-node-timeout 5000 --appendonly yes
    done
    
    docker run -it --rm --network=host redis \
    redis-cli --cluster create 192.168.200.10:7000 192.168.200.11:7001 192.168.200.12:7002
    
    Supplement 2  ·  Aug 27, 2020

    创建了网络之后,Redis cluster里面的节点之间的通信没问题,但是应用不是运行在这个网络的。所以最后我选择使用Grokzen/docker-redis-cluster: Dockerfile for Redis Cluster (redis 3.0+)来创建本地Redis cluster。

    6 replies    2020-08-27 19:22:30 +08:00
    windghoul
        1
    windghoul  
       Aug 27, 2020
    创建一个虚拟网络,然后进到 docker 里面执行 cli 命令
    whileFalse
        3
    whileFalse  
       Aug 27, 2020
    先看看你的 Docker for Mac 有没有开代理。
    JasonLaw
        4
    JasonLaw  
    OP
       Aug 27, 2020
    @windghoul #2
    @whileFalse #3

    我想确认一下我理解的是不是正确的。--network=host 不行是因为 macOS 不支持,单纯使用-p ${i}:${i} -p $((${i} + 10000)):$((${i} + 10000))替换--network=host 不行是因为 redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 中的 127.0.0.1 (因为它们这三个容器都有自己的“动态”IP 地址)。对吗?
    windghoul
        5
    windghoul  
       Aug 27, 2020
    @JasonLaw #4 我之前创建的,在 linux 上,也不能用宿主机的 ip 进行集群添加,只能用它的虚拟 ip 进行添加,具体的原因我还没来得及研究
    qfdk
        6
    qfdk  
    PRO
       Aug 27, 2020 via iPhone
    你要用 host 代替你的 127.0.0.1 你这个属于容器内部的通信 link 估计也可以
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4798 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 09:54 · PVG 17:54 · LAX 02:54 · JFK 05:54
    ♥ Do have faith in what you're doing.