docker compose 集群 network: host
prepare
集群搭建
编写redis.conf
1 2 3 4 5
| cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6379
|
在/root/redis-cluster目录下创建redis-1,redis2,redis-3,redis-4,redis-5,redis-6文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mkdir -p /root/apps/redis-cluster #然后将上一步的 redis.conf 放到 ~/docker/redis/r1 目录下 cd /root/apps/redis-cluster mkdir redis-1 cd redis-1 mkdir data vim redis.conf
#然后复制6个 redis-1 文件夹 cd /root/apps/redis-cluster cp -r redis-1 redis-2 cp -r redis-1 redis-3 cp -r redis-1 redis-4 cp -r redis-1 redis-5 cp -r redis-1 redis-6
|
- 目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| ├── docker-compose.yaml ├── redis-1 │ ├── data │ │ └── nodes.conf │ └── redis.conf ├── redis-2 │ ├── data │ │ └── nodes.conf │ └── redis.conf ├── redis-3 │ ├── data │ │ └── nodes.conf │ └── redis.conf ├── redis-4 │ ├── data │ │ └── nodes.conf │ └── redis.conf ├── redis-5 │ ├── data │ │ └── nodes.conf │ └── redis.conf └── redis-6 ├── data │ └── nodes.conf └── redis.conf
|
在每个redis-*文件夹下创建redis.conf文件,并写入如下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| echo 'cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6379 ' > /root/apps/redis-cluster/redis-1/redis.conf
echo 'cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6380 ' > /root/apps/redis-cluster/redis-2/redis.conf
echo 'cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6381 ' > /root/apps/redis-cluster/redis-3/redis.conf
echo 'cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6382 ' > /root/apps/redis-cluster/redis-4/redis.conf
echo 'cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6383 ' > /root/apps/redis-cluster/redis-5/redis.conf
echo 'cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes port 6384 ' > /root/apps/redis-cluster/redis-6/redis.conf
|
编写docker-compose.yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| version: '3.1' services: # redis1配置 redis1: image: daocloud.io/library/redis:6.0.4 container_name: redis-1 restart: always network_mode: "host" volumes: - ./redis-1/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-1/data:/data command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis2配置 redis2: image: daocloud.io/library/redis:6.0.4 container_name: redis-2 restart: always network_mode: "host" volumes: - ./redis-2/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-2/data:/data command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis3配置 redis3: image: daocloud.io/library/redis:6.0.4 container_name: redis-3 restart: always network_mode: "host" volumes: - ./redis-3/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-3/data:/data command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis4配置 redis4: image: daocloud.io/library/redis:6.0.4 container_name: redis-4 restart: always network_mode: "host" volumes: - ./redis-4/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-4/data:/data command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis5配置 redis5: image: daocloud.io/library/redis:6.0.4 container_name: redis-5 restart: always network_mode: "host" volumes: - ./redis-5/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-5/data:/data command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis6配置 redis6: image: daocloud.io/library/redis:6.0.4 container_name: redis-6 restart: always network_mode: "host" volumes: - ./redis-6/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6/data:/data command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
|
启动容器
1 2 3 4
| systemctl start docker
docker-compose up -d
|
查看容器启动状态
开启集群
随便找一个容器进入,这里我选择redis-1进入。
1 2
| docker exec -it redis-1 bash
|
在进入容器后,输入如下命令开启集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| redis-cli --cluster create 192.168.171.136:6379 \ 192.168.171.136:6380 \ 192.168.171.136:6381 \ 192.168.171.136:6382 \ 192.168.171.136:6383 \ 192.168.171.136:6384 \ --cluster-replicas 1
# 集群信息查看 redis-cli --cluster info 192.168.171.136:6379
# Connect redis cluster nodes with – C parameter redis-cli –c -h 172.17.0.1 –p 6391
redis-cli -h redis-1
cluster nodes
|
redis-cli –cluster cmd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| # 1 创建集群主节点 redis-cli --cluster create 192.168.163.132:6379 192.168.163.132:6380 192.168.163.132:6381
# 2 创建集群主从节点
redis-cli --cluster create 192.168.163.132:6379 192.168.163.132:6380 192.168.163.132:6381 192.168.163.132:6382 192.168.163.132:6383 192.168.163.132:6384 --cluster-replicas 1 redis-cli --cluster create 172.21.0.2:6379 172.21.0.3:6379 172.21.0.4:6379 --cluster-replicas 2 # 说明:--cluster-replicas 参数为数字,1表示每个主节点需要1个从节点。
# 通过该方式创建的带有从节点的机器不能够自己手动指定主节点,所以如果需要指定的话,需要自己手动指定,先使用①或③创建好主节点后,再通过④来处理。
# 3 添加集群主节点
redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379 #说明:为一个指定集群添加节点,需要先连到该集群的任意一个节点IP(192.168.163.132:6379),再把新节点加入。该2个参数的顺序有要求:新加入的节点放前
# 4 添加集群从节点
redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379 --cluster-slave --cluster-master-id 117457eab5071954faab5e81c3170600d5192270 #说明:把6382节点加入到6379节点的集群中,并且当做node_id为 117457eab5071954faab5e81c3170600d5192270 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。
# 说明:把6380节点加入到6379节点的集群中,并且当做node_id为 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。 redis-cli --cluster add-node 192.168.171.136:6380 192.168.171.136:6379 --cluster-slave --cluster-master-id 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7 redis-cli --cluster add-node 192.168.171.136:6381 192.168.171.136:6379 --cluster-slave --cluster-master-id 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7
redis-cli --cluster add-node 172.21.0.3:6379 172.21.0.2:6379 --cluster-slave --cluster-master-id 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7 #说明:把 172.21.0.3:6379 节点加入到 172.21.0.2:6379 节点的集群中,并且当做node_id为 117457eab5071954faab5e81c3170600d5192270 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。
redis-cli -h redis-master
# 5 删除节点 redis-cli --cluster del-node 192.168.163.132:6384 f6a6957421b80409106cb36be3c7ba41f3b603ff # 说明:指定IP、端口和node_id 来删除一个节点,从节点可以直接删除,有slot分配的主节点不能直接删除。删除之后,该节点会被shutdown。
# 检查集群 redis-cli --cluster check 192.168.171.136:6371 --cluster-search-multiple-owners # 修复集群 redis-cli --cluster fix 192.168.171.136:6371 --cluster-search-multiple-owners # 说明:检查key、slots、从节点个数的分配情况
# 集群信息查看 redis-cli --cluster info 192.168.171.136:6379
# 说明:把6380节点加入到6379节点的集群中,并且当做node_id为 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。 redis-cli --cluster add-node 192.168.171.136:6380 192.168.171.136:6379 --cluster-slave --cluster-master-id 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7 redis-cli --cluster add-node 192.168.171.136:6381 192.168.171.136:6379 --cluster-slave --cluster-master-id 551c30a7093bcb8dc8068cd30cf6e81d9b8b47c7
|
参考文章