使用Docker Swarm和Docker Compose构建Redis集群

6

我是一名正在学习Docker及其优点,如Swarm和Compose的人。我的目的是在Docker Swarm中创建一个Redis群集。

以下是我的Compose文件:

version: '3'

services:
  redis:
    image: redis:alpine
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 60000","--cluster-require-full-coverage no"]
    deploy:
      replicas: 5
      restart_policy:
        condition: on-failure
    ports:
      - 6379:6379
      - 16379:16379

networks:
  host:
    external: true

如果我添加了network: - host,则容器都不会启动,如果我删除它,那么容器就能启动了,但是当我尝试连接时,会抛出CLUSTERDOWN Hash slot not served的错误。
规格 -
Windows 10

Docker Swarm Nodes -
2 Virtual Box VMs running Alpine Linux 3.7.0 with two networks

VirtualBox VM Network - 
eth0 - NAT
eth1 - VirtualBox Host-only network

Docker running inside the above VMs - 
17.12.1-ce

1
有几件事情需要注意,Redis服务需要具有网络标签“hosts”。其次,当您使用“external”时,这意味着通过docker network create创建了一个名为true的网络,而不是它的布尔值。 - ealeon
这可能有所帮助: https://gist.github.com/Richard-Mathie/d39ffb5c381344200ead1dee9bb99782 我试图将其发布为答案,但是这里的审核不允许。他们最好检查其他答案是否有效。 - Desprit
3个回答

2

对于那些遇到困难的人,很遗憾目前无法通过docker-compose.yml来完成。请参考这个问题:启动Redis集群#79。唯一的方法是获取运行Redis的所有节点的IP地址和端口,然后在任何一个swarm节点上运行此命令。

# Gives you all the command help
docker run --rm -it thesobercoder/redis-trib 

# This creates all master nodes
docker run --rm -it thesobercoder/redis-trib create 172.17.8.101:7000 172.17.8.102:7000 172.17.8.103:7000 

# This creates slaves nodes. Note that this requires at least six nodes running master
docker run --rm -it thesobercoder/redis-trib create --replicas 1 172.17.8.101:7000 172.17.8.102:7000 172.17.8.103:7000 172.17.8.104:7000 172.17.8.105:7000 172.17.8.106:7000

2

2

这对我来说似乎有效,网络配置来自这里

最初的回答

version: '3.6'

services:
  redis:
    image: redis:5.0.3
    command:
      - "redis-server"
      - "--cluster-enabled yes"
      - "--cluster-config-file nodes.conf"
      - "--cluster-node-timeout 5000"
      - "--appendonly yes"
    deploy:
      mode: global
      restart_policy:
        condition: on-failure
    networks:
      hostnet: {}

networks:
  hostnet:
    external: true
    name: host

最初的回答:
例如运行以下命令: echo yes | docker run -i --rm --entrypoint redis-cli redis:5.0.3 --cluster create 1.2.3.4{1,2,3}:6379 --cluster-replicas 0 显然替换为您自己的IP。

我尝试了这个,但是我得到了一个“仅支持用户定义网络中容器的网络作用域别名”的错误。 - CalculatingKraken
1
@CalculatingKraken 这是因为问题涉及到 Docker Swarm,所以你需要一个 Swarm (docker swarm init) 并使用 docker swarm deploy -c docker-compose.yml 部署该文件,而不是使用 docker-compose up - exic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接