docker搭建redis哨兵集群

docker 搭建 redis 哨兵集群


1 说明

  1. 使用redis:5.0.9
  2. 一主两从架构

=

2 实现主从

1 镜像

docker pull redis:5.0.9

=

2 配置文件

mkdir -pv /data/redis/{data,conf}

# cat /data/redis/conf/redis-6379.conf 
bind 0.0.0.0
port 6379
requirepass centos
dbfilename dump.rdb
dir "/tmp"
appendonly no
protected-mode no
daemonize no
loglevel debug

# cat /data/redis/conf/redis-6380.conf 
replicaof 192.168.1.56 6379
masterauth centos
bind 0.0.0.0
port 6380
requirepass centos
dbfilename dump.rdb
dir "/tmp"
appendonly no
protected-mode no
daemonize no
loglevel debug

# cat /data/redis/conf/redis-6381.conf 
replicaof 192.168.1.56 6379
masterauth centos
bind 0.0.0.0
port 6381
requirepass centos
dbfilename dump.rdb
dir "/tmp"
appendonly no
protected-mode no
daemonize no
loglevel debug

=

3 运行主从容器

docker run -p 6379:6379 --name redis5-01 \
-v /data/redis/conf:/etc/redis -v /data/redis/data:/data  \
--privileged=true -d redis:5.0.9 \
redis-server /etc/redis/redis-6379.conf

docker run -p 6380:6380 --name redis5-02 \
-v /data/redis/conf:/etc/redis -v /data/redis/data:/data  \
--privileged=true -d redis:5.0.9 \
redis-server /etc/redis/redis-6380.conf

docker run -p 6381:6381 --name redis5-03 \
-v /data/redis/conf:/etc/redis -v /data/redis/data:/data  \
--privileged=true -d redis:5.0.9 \
redis-server /etc/redis/redis-6381.conf

=

4 检查主从状态

# 从节点 02
docker exec -it redis5-02 redis-cli -p 6380 -a centos
info Replication
	# 注意到 master_link_status:up 即为 ok
	
# 从节点 03 同理
docker exec -it redis5-03 redis-cli -p 6381 -a centos
info Replication


# 主节点 01
docker exec -it redis5-01 redis-cli -p 6379 -a centos
info Replication
	# 注意到 connected_slaves:2

=

=

3 实现哨兵

1 配置文件

# cat sentinel-6379.conf
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile ""
dir /tmp
sentinel monitor mymaster 192.168.1.56 6379 2
sentinel auth-pass mymaster centos
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

# cat sentinel-6380.conf
port 26380
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile ""
dir /tmp
sentinel monitor mymaster 192.168.1.56 6379 2
sentinel auth-pass mymaster centos
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

# cat sentinel-6381.conf
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile ""
dir /tmp
sentinel monitor mymaster 192.168.1.56 6379 2
sentinel auth-pass mymaster centos
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

=

2 启动哨兵

启动哨兵,在其配置文件中会生成 sentinel myid,各个哨兵不能相同,若相同则手动修改,再重启

docker run -p 26379:26379 --name redis-sentinel-01 \
-v /data/redis/conf:/etc/redis  \
--privileged=true -d redis:5.0.9 \
redis-sentinel /etc/redis/sentinel-6379.conf

docker run -p 26380:26380 --name redis-sentinel-02 \
-v /data/redis/conf:/etc/redis  \
--privileged=true -d redis:5.0.9 \
redis-sentinel /etc/redis/sentinel-6380.conf

docker run -p 26381:26381 --name redis-sentinel-03 \
-v /data/redis/conf:/etc/redis  \
--privileged=true -d redis:5.0.9 \
redis-sentinel /etc/redis/sentinel-6381.conf

=

3 修改 sentinel myid

# 启动后检查哨兵状态发现为 down
docker logs --tail 30 redis-sentinel-01
	# +sdown master mymaster。。。。。。。。。。

# 发现三个哨兵生成了相同的 sentinel myid
# 于是手动修改
vim sentinel-6380.conf
vim sentinel-6381.conf

# 重启哨兵
docker restart redis-sentinel-02
docker restart redis-sentinel-03

# 再次检查
docker logs --tail 30 redis-sentinel-01
	# +slave slave 172.17.0.1:6380 172.17.0.1 6380 @ mymaster 192.168.1.56 6379
	# +slave slave 172.17.0.1:6381 172.17.0.1 6381 @ mymaster 192.168.1.56 6379

=

wan


版权声明:本文为qq_35396734原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。