这里写自定义目录标题
- 配置配置、数据和logs目录
- 配置docker-compose
- 配置集群
- 总结
本文是通过docker-compose在linux配置redis高可用。前提条件是系统中需要安装docker和docker-compose。
配置配置、数据和logs目录
mkdir -p /home/docker-redis
在当前目录下创建cluster-conf.conf配置文件,并且在文件中添加如下配置文件。
# redis端口
port ${PORT}
# 连接密码
requirepass 123456
masterauth 123456
# 关闭保护模式
protected-mode no
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP
cluster-announce-ip 10.10.20.100
# 集群节点端口 7001 - 7006
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
# 开启 appendonly 备份模式
appendonly yes
# 每秒钟备份
appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
auto-aof-rewrite-min-size 64mb
配置六台服务的data、conf和logs。
for port in `seq 7001 7006`; \
do mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./cluster-conf.conf > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data \
&& mkdir -p ./${port}/logs; \
done
结果如下:
配置docker-compose
在当前目录下创建docker-compse.yml文件,然后将如配置件添加到文件中
version: '3.7'
services:
redis-1:
restart: always
image: redis:latest
container_name: redis-1
environment:
- TZ=Asia/Shanghai
network_mode: "host"
volumes:
- $PWD/7001/data:/data:rw
- $PWD/7001/conf/redis.conf:/etc/redis/redis.conf:rw
command: ["redis-server","/etc/redis/redis.conf"]
redis-2:
restart: always
image: redis:latest
container_name: redis-2
environment:
- TZ=Asia/Shanghai
network_mode: "host"
volumes:
- $PWD/7002/data:/data:rw
- $PWD/7002/conf/redis.conf:/etc/redis/redis.conf:rw
command: ["redis-server","/etc/redis/redis.conf"]
redis-3:
restart: always
image: redis:latest
container_name: redis-3
environment:
- TZ=Asia/Shanghai
network_mode: "host"
volumes:
- $PWD/7003/data:/data:rw
- $PWD/7003/conf/redis.conf:/etc/redis/redis.conf:rw
command: ["redis-server","/etc/redis/redis.conf"]
redis-4:
restart: always
image: redis:latest
container_name: redis-4
environment:
- TZ=Asia/Shanghai
network_mode: "host"
volumes:
- $PWD/7004/data:/data:rw
- $PWD/7004/conf/redis.conf:/etc/redis/redis.conf:rw
command: ["redis-server","/etc/redis/redis.conf"]
redis-5:
restart: always
image: redis:latest
container_name: redis-5
environment:
- TZ=Asia/Shanghai
network_mode: "host"
volumes:
- $PWD/7005/data:/data:rw
- $PWD/7005/conf/redis.conf:/etc/redis/redis.conf:rw
command: ["redis-server","/etc/redis/redis.conf"]
redis-6:
restart: always
image: redis:latest
container_name: redis-6
environment:
- TZ=Asia/Shanghai
network_mode: "host"
volumes:
- $PWD/7006/data:/data:rw
- $PWD/7006/conf/redis.conf:/etc/redis/redis.conf:rw
command: ["redis-server","/etc/redis/redis.conf"]
执行下面命令
docker-compose -f docker-compose.yml up -d
配置集群
ip设置之前创建redis对应的机器ip和端口号,密码对应cluster-conf.conf中配置的masterauth。运行后需要添加一个yes
docker exec -it redis-1 \
redis-cli -a "123456" \
--cluster create \
10.10.20.133:7001 \
10.10.20.133:7002 \
10.10.20.133:7003 \
10.10.20.133:7004 \
10.10.20.133:7005 \
10.10.20.133:7006 \
--cluster-replicas 1
总结
至此docker-compose配置redis多主多从完成。