今天来记录一下使用DockerCompose部署Redis分片集群的过程,前面写了几篇关于redis的博客了,这里就不再过多介绍了,直接上配置就好了
version: "3.0"
services:
redisServer1:
image: redis:6.2.4
container_name: redis_server1
volumes:
- /home/redis/redis1/data:/data
- /home/redis/redis1/logs:/logs
- ./redis.conf:/redis.conf
command:
# 服务启动
/bin/bash -c "redis-server /redis.conf --port 7001"
#ports:
# - 7001:7001
network_mode: host
redisServer2:
image: redis:6.2.4
container_name: redis_server2
volumes:
- /home/redis/redis2/data:/data
- /home/redis/redis2/logs:/logs
- ./redis.conf:/redis.conf
command:
# 服务启动
/bin/bash -c "redis-server /redis.conf --port 7002"
#ports:
# - 7001:7001
network_mode: host
redisServer3:
image: redis:6.2.4
container_name: redis_server3
volumes:
- /home/redis/redis3/data:/data
- /home/redis/redis3/logs:/logs
- ./redis.conf:/redis.conf
command:
# 服务启动
/bin/bash -c "redis-server /redis.conf --port 7003"
#/bin/bash -c "redis-server /usr/local/etc/redis/redis.conf"
#ports:
# - 7001:7001
network_mode: host
redisServer4:
image: redis:6.2.4
container_name: redis_server4
volumes:
- /home/redis/redis4/data:/data
- /home/redis/redis4/logs:/logs
- ./redis.conf:/redis.conf
command:
# 服务启动
/bin/bash -c "redis-server /redis.conf --port 7004"
#/bin/bash -c "redis-server /usr/local/etc/redis/redis.conf"
#ports:
# - 7001:7001
network_mode: host
redisServer5:
image: redis:6.2.4
container_name: redis_server5
volumes:
- /home/redis/redis5/data:/data
- /home/redis/redis5/logs:/logs
- ./redis.conf:/redis.conf
command:
# 服务启动
/bin/bash -c "redis-server /redis.conf --port 7005"
#/bin/bash -c "redis-server /usr/local/etc/redis/redis.conf"
#ports:
# - 7001:7001
network_mode: host
redisServer6:
image: redis:6.2.4
container_name: redis_server6
volumes:
- /home/redis/redis6/data:/data
- /home/redis/redis6/logs:/logs
- ./redis.conf:/redis.conf
command:
# 服务启动
/bin/bash -c "redis-server /redis.conf --port 7006"
#/bin/bash -c "redis-server /usr/local/etc/redis/redis.conf"
#ports:
# - 7001:7001
network_mode: host
其中有一个redis.config配置文件。
下载路径
https://gitee.com/840312696/redis-demo/blob/%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4/%E6%96%87%E4%BB%B6/redis.conf
使用docker-composer up -d启动之后 ,每个redis都是相互独立的 ,我们需要把他们加入集群
需要进入到其中任意一个docker容器当中。
查看容器
docker ps
进入到容器当中
docker exec -it redis_server1 /bin/bash
建立集群
redis-cli --cluster create --cluster-replicas 1 192.168.21.69:7001 192.168.21.69:7002 192.168.21.69:7003 192.168.21.69:7004 192.168.21.69:7005 192.168.21.69:7006
需要确认主从配置服务,输入yes
这就搞定了
查看集群状态
redis-cli -p 7001 cluster nodes
测试
测试代码地址如下:
https://gitee.com/840312696/redis-demo/tree/%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4/
向redis中插入值
从redis中查询值
打印日志
从日志看集群已经实现了分部署,并且是读写分离的