我只是在学习docker和它的所有优点,比如swarm和compose。我的目的是在 docker 群中创建一个 Redis 集群。
这是我的撰写文件 -
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
这似乎对我有用,从这里进行网络配置:
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。
不幸的是,对于任何为此苦苦挣扎的人来说,这还不能通过docker-compose.yml
来完成。请参阅此问题启动 Redis 集群 #79。执行此操作的唯一方法是获取运行 Redis 的所有节点的 IP 地址和端口,然后在任何群节点中运行此命令。
# 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
这里是 Redis 集群的存储库
https://github.com/jay-johnson/docker-redis-cluster/blob/master/docker-compose.yml