xredis-docker

ACTION

################################## 准备的机器 ##################################
192.168.1.52  master node1
192.168.1.45  slave  node2
192.168.1.54  slave  node3

################################## 创建好存放redis配置文件和数据的目录 #################
mkdir -p /xdata/docker-redis-data

################################## redis配置文件 ##################################
cat << 'EOF' > /xdata/docker-redis-data/redis.conf
daemonize no
protected-mode no
port 6379
dbfilename dump.rdb
bind 0.0.0.0

#192.168.1.52  master node1
#192.168.1.45  slave  node2
#192.168.1.54  slave  node3

slave-announce-ip 192.168.1.54
slave-announce-port 6379
# change above adress according to actural local ip

# next config only for slave
slaveof 192.168.1.52 6379
EOF

上述配置文件涉及的IP地址,注意根据实际情况修改

slave-announce-ip 和 slave-announce-port 是本机的物理IP地址,是用来告知其它redis机器,连接我的时候使用这个地址,而不是容器默认的IP地址

################################## redis启动脚本 ##################################
cat << 'EOF' > /root/redis-docker.sh
docker run --name xredis -d \
-v /root/busybox-x86_64:/root/busybox-x86_64 \
-v /xdata/docker-redis-data:/data \
-v /xdata/docker-redis-data/redis.conf:/data/redis.conf \
-p 6379:6379 \
daocloud.io/library/redis:4.0.14 /usr/local/bin/redis-server /data/redis.conf
EOF

映射目录时,容器外和容器里假如不存在相关目录,则会自动创建

# docker exec -it xredis ls /usr/local/bin
docker-entrypoint.sh  redis-benchmark  redis-check-rdb	redis-sentinel
gosu		      redis-check-aof  redis-cli	redis-server

################################## 验证主从复制是否搭建成功 ##################################
docker exec -it xredis redis-cli info replication
尤其注意观察slave redis指向的master redis IP地址,应为服务器物理地址,而不是容器地址,不引入额外机制,跨机器两个容器之间无法通过内部容器IP地址通信,我们是通过物理IP地址加上端口转发通信的

################################## sentinel配置文件 ##################################
cat << 'EOF' > /xdata/docker-redis-data/sentinel.conf
bind 0.0.0.0
port 26379
daemonize no
protected-mode no
sentinel monitor mymaster 192.168.1.52 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

#192.168.1.52  master node1
#192.168.1.45  slave  node2
#192.168.1.54  slave  node3

sentinel announce-ip   192.168.1.54
sentinel announce-port 26379
# change above adress according to actural local ip
EOF

同样这里也需要告诉其它机器上的sentinel,使用物理IP地址,而不是容器IP地址,进行通信

################################## sentinel启动脚本 ###################################################
cat << 'EOF' > /root/redis-sentinel-docker.sh
docker run --name xredis-sentinel -d \
-v /root/busybox-x86_64:/root/busybox-x86_64 \
-v /xdata/docker-redis-data:/data \
-v /xdata/docker-redis-data/sentinel.conf:/data/sentinel.conf \
-p 26379:26379 \
daocloud.io/library/redis:4.0.14 /usr/local/bin/redis-sentinel /data/sentinel.conf
EOF

################################## 验证sentinel高可用 ###################################################
docker exec -it xredis-sentinel redis-cli -h localhost -p 26379 info Sentinel
cat /xdata/docker-redis-data/sentinel.conf 启动后,这份配置文件会被自动修改,里面不应该出现容器内网IP地址(一般是默认172开头)

################################## 启动和停止 ####################################################################
bash redis-docker.sh
bash redis-sentinel-docker.sh
docker rm -fv xredis
docker rm -fv xredis-sentinel

################################## 清空数据 ####################################################################
先停服务,再清空数据,重新部署
rm -f /xdata/docker-redis-data/dump.rdb

参考资料

https://juejin.im/post/5d26b03de51d454fa33b1960#heading-0
https://hub.daocloud.io/repos/beb958f9-ffb6-4f68-817b-c17e1ff476c3
https://github.com/docker-library/redis/
https://hub.docker.com/_/redis/
http://download.redis.io/redis-stable/
http://download.redis.io/redis-stable/redis.conf
http://download.redis.io/redis-stable/sentinel.conf
https://redis.io/topics/sentinel

csdn 111589435