为了构建一个集群,我们首先要让 redis 启用集群模式
一个简单的配置文件如下redis.conf
# redis.conf file port 6379 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes
因为是在单机上演示,所以我们得修改 port 防止冲突
比如我们要创建3主5副的集群,那么这八个 redis 实例启动的时候就应该用不同的端口(7000,7001...7007),这样能防止冲突。
. ├── 7000 │ └── redis.conf ├── 7001 │ └── redis.conf ├── 7002 │ └── redis.conf ├── 7003 │ └── redis.conf ├── 7004 │ └── redis.conf ├── 7005 │ └── redis.conf ├── 7006 │ └── redis.conf ├── 7007 │ └── redis.conf
./7000/redis.conf
中的 port 设置成7000,依此类推./7007/redis.conf
中的 port 应该设置成7007
万事俱备,只需运行redis-server ./redis.conf
命令即可,当然你需要依次启动八个 redis server
for i in `seq 7000 7007`; do exec redis-server ./${i}/redis.conf & done redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 127.0.0.1:7007 --cluster-replicas 1
这样整个集群就搭建好了,你可以选择运行在 port 7000-7007中的任意一个 servr 登录,然后去执行一些你喜欢的命令验证一下[狗头]
现在让我们来明确一下流程,首先我们需要启动八个 redis server 实例,然后运行 redis-cli --cluster create
命令创建集群,这意味着我们需要创建一个 docker network 使得集群成员能够互相通讯
创建一个 network 也很简单
networks: redis-network: name: redis-network ## 使用网桥 driver: bridge ipam: driver: default ## 指定网段,需要注意不能与已有的 docker network 相冲突 config: - subnet: 172.18.0.0/24
还记得之前命令行是如何启动集群的吗?最后需要运行命令redis-cli --cluster create
,整个集群才算成功创建,那在 yml 文件中该怎么写呢?
我们可以借助 depends_on
来告诉 docker 当前的 service 依赖其他的 service
具体的细节请看完整的yml文件
## redis.conf port 6379 cluster-enabled yes cluster-config-file nodes-redis.conf cluster-node-timeout 5000 appendonly yes appendfilename "appendonly-redis.aof"
version: "3.7" networks: redis-network: name: redis-network driver: bridge ipam: driver: default config: - subnet: 172.18.0.0/24 services: redis-cluster: image: redis:6-alpine command: 'redis-cli --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 172.18.0.17:6379 172.18.0.18:6379 --cluster-yes --cluster-replicas 1' networks: - redis-network depends_on: - redis1 - redis2 - redis3 - redis4 - redis5 - redis6 - redis7 - redis8 redis1: image: redis:6-alpine container_name: redis1 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.11 expose: - 6379 volumes: - $PWD/docker-data/redis1:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis2: image: redis:6-alpine container_name: redis2 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.12 expose: - 6379 volumes: - $PWD/docker-data/redis2:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis3: image: redis:6-alpine container_name: redis3 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.13 expose: - 6379 volumes: - $PWD/docker-data/redis3:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis4: image: redis:6-alpine container_name: redis4 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.14 expose: - 6379 volumes: - $PWD/docker-data/redis4:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis5: image: redis:6-alpine container_name: redis5 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.15 expose: - 6379 volumes: - $PWD/docker-data/redis5:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis6: image: redis:6-alpine container_name: redis6 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.16 expose: - 6379 volumes: - $PWD/docker-data/redis6:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis7: image: redis:6-alpine container_name: redis7 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.17 expose: - 6379 volumes: - $PWD/docker-data/redis7:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf redis8: image: redis:6-alpine container_name: redis8 command: redis-server /usr/local/etc/redis/redis.conf networks: redis-network: ipv4_address: 172.18.0.18 expose: - 6379 volumes: - $PWD/docker-data/redis8:/data - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf
最后不要忘记了最基本的命令
sudo docker-compose up # 在当前命令下运行,启动集群 sudo docker-compose down # 关闭集群的同时会将 docker-network也一并删除
# 登录任意一个节点执行命令即可,集群会根据key的哈希值,将你的命令重定位 sudo docker exec -it redis5 redis-cli -p 6379 set foo bar
sudo docker exec -it redis1 redis-cli -p 6379 get foo # output bar