使用宿主机ip:port 搭建外部可用集群
构建目录
-- data
-- config
-- redis1.conf
-- redis2.conf
-- redis3.conf
-- redis4.conf
-- redis5.conf
-- redis6.conf
docker-compose.yml
version: "3"
services:
redis1:
image: redis:latest
restart: "no"
container_name: redis1
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data1:/data
- ./config/redis1.conf:/etc/redis/redis.conf
network_mode: "host"
redis2:
image: redis:latest
restart: "no"
container_name: redis2
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data2:/data
- ./config/redis2.conf:/etc/redis/redis.conf
network_mode: "host"
redis3:
image: redis:latest
restart: "no"
container_name: redis3
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data3:/data
- ./config/redis3.conf:/etc/redis/redis.conf
network_mode: "host"
redis4:
image: redis:latest
restart: "no"
container_name: redis4
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data4:/data
- ./config/redis4.conf:/etc/redis/redis.conf
network_mode: "host"
redis5:
image: redis:latest
restart: "no"
container_name: redis5
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data5:/data
- ./config/redis5.conf:/etc/redis/redis.conf
network_mode: "host"
redis6:
image: redis:latest
restart: "no"
container_name: redis6
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data6:/data
- ./config/redis6.conf:/etc/redis/redis.conf
network_mode: "host"
redis.conf
#端口号,写文件夹对映的端口 每个文件的端口配置都不一样 6370 - 6375
port 6370
#开启集群
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
#开启aof存储
appendonly yes
#设置登录密码
requirepass 123456
#设置节点密码,集群必设
masterauth 123456
# 关闭保护,外网可直接访问
protected-mode no
配置集群
宿主机ip 192.168.56.102
docker exec -ti redis1 bash -c 'redis-cli --cluster create --cluster-replicas 1 -a 123456 192.168.56.102:6370 192.168.56.102:6371 192.168.56.102:6372 192.168.56.102:6373 192.168.56.102:6374 192.168.56.102:6375'
使用
在宿主机上链接
redis-cli -h 192.168.56.102 -p 6370 -a 123456 -c