一 拉取镜像
docker pull redis:6.0.7
二 创建模板文件
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
#cluster-announce-ip 192.168.64.2
#cluster-announce-port ${PORT}
#cluster-announce-bus-port 1${PORT}
bind 0.0.0.0
appendonly yes
logfile "redis.log"
三 生成配置文件
生成配置文件和文件夹
cd /Users/hezhaoming/Documents/docker/redis
for port in `seq 7010 7015`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
注意mac需要安装命令工具
brew install gettext
brew link --force gettext
四 编写编排文件
version: '3'
services:
redis1:
image: redis:6.0.7
hostname: redis1
container_name: redis1
networks:
redis-net:
ipv4_address: 172.35.0.10
restart: always
privileged: true
ports:
- 7010:7010
- 17010:17010
volumes:
- /Users/hezhaoming/Documents/docker/redis/7010/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /Users/hezhaoming/Documents/docker/redis/7010/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
redis2:
image: redis:6.0.7
hostname: redis2
container_name: redis2
networks:
redis-net:
ipv4_address: 172.35.0.20
restart: always
privileged: true
ports:
- 7011:7011
- 17011:17011
volumes:
- /Users/hezhaoming/Documents/docker/redis/7011/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /Users/hezhaoming/Documents/docker/redis/7011/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
redis3:
image: redis:6.0.7
hostname: redis3
container_name: redis3
networks:
redis-net:
ipv4_address: 172.35.0.30
restart: always
privileged: true
ports:
- 7012:7012
- 17012:17012
volumes:
- /Users/hezhaoming/Documents/docker/redis/7012/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /Users/hezhaoming/Documents/docker/redis/7012/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
redis4:
image: redis:6.0.7
hostname: redis4
container_name: redis4
networks:
redis-net:
ipv4_address: 172.35.0.40
restart: always
privileged: true
ports:
- 7013:7013
- 17013:17013
volumes:
- /Users/hezhaoming/Documents/docker/redis/7013/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /Users/hezhaoming/Documents/docker/redis/7013/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
redis5:
image: redis:6.0.7
hostname: redis5
container_name: redis5
networks:
redis-net:
ipv4_address: 172.35.0.50
restart: always
privileged: true
ports:
- 7014:7014
- 17014:17014
volumes:
- /Users/hezhaoming/Documents/docker/redis/7014/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /Users/hezhaoming/Documents/docker/redis/7014/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
redis6:
image: redis:6.0.7
hostname: redis6
container_name: redis6
networks:
redis-net:
ipv4_address: 172.35.0.60
restart: always
privileged: true
ports:
- 7015:7015
- 17015:17015
volumes:
- /Users/hezhaoming/Documents/docker/redis/7015/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /Users/hezhaoming/Documents/docker/redis/7015/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
redis-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.35.0.1/24
五 执行集群命令
docker-compose up -d
//随机进入一个节点 执行集群命令
redis-cli --cluster create 172.35.0.10:7010 172.35.0.20:7011 172.35.0.30:7012 172.35.0.40:7013 172.35.0.50:7014 172.35.0.60:7015 --cluster-replicas 1
// 交互提示是否创建,输入yes 回车
//创建成功
//随便进入一个节点 比如最后一个节点
redis-cli -c -h 172.35.0.60 -p 7015
//输入命令,查看节点信息
info
cluster nodes
六 测试使用
工具查看,也是OK的