Docker容器下Redis的主从配置以及Sentinel哨兵配置
1、拉取redis镜像
docker pull redis
2、启动3个redis容器服务,分别使用到6379、6380、6381端口
docker run --name redis-master -p 6379:6379 -d redis-server
docker run --name redis-slave1 -p 6380:6379 -d redis-server
docker run --name redis-slave2 -p 6381:6379 -d redis-server
3、查看已启动的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb349796fee6 redis "docker-entrypoint..." 2 hours ago Up 21 minutes 0.0.0.0:6381->6379/tcp redis-slave2
6091eaf2475c redis "docker-entrypoint..." 2 hours ago Up 22 minutes 0.0.0.0:6380->6379/tcp redis-slave1
bc84f8185f77 redis "docker-entrypoint..." 2 hours ago Up 24 minutes 0.0.0.0:6379->6379/tcp redis-master
4、测试容器,成功
docker exec -it ab54741166e1(容器id) redis-cli:进入容器内部
127.0.0.1:6379> set a test
OK
127.0.0.1:6379> get a
"test"
127.0.0.1:6379> quit
root@6091eaf2475c:/data#
5、开始redis集群配置
5.1、看容器内网的ip地址
[root@localhost ~]# docker inspect redis-slave1
这里我们得到3个redis的内网ip地址为:
redis-master:172.17.0.2:6379
redis-slave1:172.17.0.3:6379
redis-slave2:172.17.0.4:6379
5.2、这里我们进入docker容器内部,查看当前redis角色(主还是从)目前三个都是master状态
[root@localhost ~]# docker exec -it 6091eaf2475c /bin/bash
root@6091eaf2475c:/data# redis-cli
127.0.0.1:6379> info replication
#Replication
role:slave
master_host:172.17.0.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:532406
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:16f555d7ac3b32ae0584d6b5f320efec3e35b9e6
master_replid2:d8af8d9da6752ca2f0c7a733f5462a0c163c8f7c
master_repl_offset:532406
second_repl_offset:73526
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:532378
5.3、使用redis-cli命令修改redis-slave1、redis-slave2的主机为172.17.0.2:6379
[root@localhost /]# docker exec -it redis-slave1 /bin/bash
root@6091eaf2475c:/data# redis-cli
127.0.0.1:6379> SLAVEOF 172.17.0.2 6379
OK
127.0.0.1:6379> quit
root@6091eaf2475c:/data# exit
exit
[root@localhost /]# docker exec -it redis-slave2 /bin/bash
root@6091eaf2475c:/data# redis-cli
127.0.0.1:6379> SLAVEOF 172.17.0.2 6379
OK
127.0.0.1:6379> quit
root@6091eaf2475c:/data# exit
exit
5.4、查看redis-master是否已经拥有2个从机,connected_slaves:2,没错
[root@localhost /]# docker exec -it redis-master /bin/bash
root@bc84f8185f77:/data# redis-cli
127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.4,port=6379,state=online,offset=70,lag=1
slave1:ip=172.17.0.3,port=6379,state=online,offset=70,lag=1
master_replid:d8af8d9da6752ca2f0c7a733f5462a0c163c8f7c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
5.5、配置Sentinel哨兵
Sentinel哨兵
Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务(具体介绍可参考 链接):
监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
进入3台redis容器内部进行配置,在容器根目录里面创建sentinel.conf文件和log.txt文件
文件内容为: sentinel monitor redis-master 172.17.0.2 6379 1 #添加为后台运行 daemonize yes #指定日志目录 logfile "/log.txt"[root@localhost /]# docker exec -it bc84f8185f77 /bin/bash root@6091eaf2475c:/data# cd / && touch sentinel.conf && touch log.txt root@6091eaf2475c:/# vim /sentinel.conf
这里如果出现 shell bash: vi: command not found
解决:1、apt-get update 2、apt-get install vim
最后依次启动Redis哨兵:root@bc84f8185f77:/# redis-sentinel /sentinel.conf 42:X 11 Jun 2019 09:17:41.415 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 42:X 11 Jun 2019 09:17:41.415 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=42, just started 42:X 11 Jun 2019 09:17:41.415 # Configuration loaded _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 42 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
42:X 11 Jun 2019 09:17:41.417 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
42:X 11 Jun 2019 09:17:41.418 # Sentinel ID is 4d8e0038c5a041074bc86b4dcad46f6798cb0fab
42:X 11 Jun 2019 09:17:41.418 # +monitor master redis-master 172.17.0.2 6379 quorum 1
42:X 11 Jun 2019 09:17:41.422 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-master 172.17.0.2 6379
42:X 11 Jun 2019 09:17:41.423 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-master 172.17.0.2 6379便于观察,这里开多个窗口。
5.6、测试
关闭redis-master 同时在容器内查看redis的日志tail -f /log.txt
[root@localhost ~]# docker stop bc84f8185f77 bc84f8185f77
这时,剩余的2个从机,会自动选举产生新的主机,这里选举172.17.0.2为主机。
查看redis-slave2,变成了主机。
[root@localhost ~]# docker exec -it fb349796fee6 /bin/bash root@fb349796fee6:/data# redis-cli 127.0.0.1:6379> info replication #Replication role:master connected_slaves:1 slave0:ip=172.17.0.3,port=6379,state=online,offset=81124,lag=0 master_replid:16f555d7ac3b32ae0584d6b5f320efec3e35b9e6 master_replid2:d8af8d9da6752ca2f0c7a733f5462a0c163c8f7c master_repl_offset:81124 second_repl_offset:73526 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:81124