基于docker实现哨兵集群部署

简单dockerfile文件,用于演示sentinel哨兵故障转移
FROM centos:latest
MAINTAINER BIXIAOYU
RUN groupadd -r redis &&useradd -r -g redis redis
RUN yum -y update && yum -y install epel-release && yum -y install redis && yum -y install net-tools
EXPOSE 6379

# docker build --no-cache -t redis .

启动docker容器实例

docker run -itd --name redis-master --net=mynetwork -p 6383:6379 --ip 172.60.0.3 redis

docker run -itd --name redis-slave1 --net=mynetwork -p 6384:6379 --ip 172.60.0.4 redis

docker run -itd --name redis-slave2 --net=mynetwork -p 6385:6379 --ip 172.60.0.5 redis

docker run -itd --name redis-sentinel1 --net=mynetwork -p : --ip 172.60.0.5 redis

docker run -itd --name redis-sentinel2 --net=mynetwork -p : --ip 172.60.0.6 redis

docker run -itd --name redis-sentinel3 --net=mynetwork -p : --ip 172.60.0.7 redis

 #docker network create --subnet=172.60.0.0/16 mynetwork  

基于docker实现哨兵集群部署

【配置】

先完成主从同步,主从同步的配置可参考https://www.cnblogs.com/bixiaoyu/p/10706811.html这篇文章,在此就不必细说了,重点是sentinel的配置

bind 0.0.0.0            #设置允许访问的IP,这里仍然设置0.0.0.0
protected-mode no #允许在连接时,密码为空
port
dir "/tmp"
sentinel myid 6d0d4099c13cdeab018e1f2005455be6f1cd6f6b
sentinel monitor mymaster 172.60.0.3 2 #设置监听
sentinel config-epoch mymaster
sentinel leader-epoch mymaster
logfile "/var/log/redis/sentinel.log"
sentinel known-slave mymaster 172.60.0.2
sentinel known-slave mymaster 172.60.0.4
sentinel known-sentinel mymaster 172.60.0.7 dfce433e021aa3e82276974aa12fa0684fb0b4f0
sentinel known-sentinel mymaster 172.60.0.6 ceb363cf84103950cfa2a785816c4e8a36c02143
sentinel current-epoch

基于docker实现哨兵集群部署

基于docker实现哨兵集群部署

 选择一个sentinel节点,查看日志信息,发现默认主节点为172.60.0.2

基于docker实现哨兵集群部署

【测试故障转移】

先通过pkill模拟故障master故障宕机,随后观察哨兵任意节点的情况

基于docker实现哨兵集群部署

我们可以从下面sentinel节点上看到172.0.0.3被选举成为新主节点

[root@ef77b61448fc /]# tail /var/log/redis/sentinel.log
:X Apr ::26.472 * +sentinel sentinel ceb363cf84103950cfa2a785816c4e8a36c02143 172.60.0.6 @ mymaster 172.60.0.2
:X Apr ::24.154 * +sentinel sentinel dfce433e021aa3e82276974aa12fa0684fb0b4f0 172.60.0.7 @ mymaster 172.60.0.2
:X Apr ::05.724 # +sdown master mymaster 172.60.0.2
:X Apr ::05.895 # +new-epoch
:X Apr ::05.895 # +vote-for-leader dfce433e021aa3e82276974aa12fa0684fb0b4f0
:X Apr ::06.276 # +config-update-from sentinel dfce433e021aa3e82276974aa12fa0684fb0b4f0 172.60.0.7 @ mymaster 172.60.0.2
:X Apr ::06.276 # +switch-master mymaster 172.60.0.2 172.60.0.3
:X Apr ::06.276 * +slave slave 172.60.0.4: 172.60.0.4 @ mymaster 172.60.0.3
:X Apr ::06.276 * +slave slave 172.60.0.2: 172.60.0.2 @ mymaster 172.60.0.3

查看sentinel节点的日志信息

基于docker实现哨兵集群部署

此时,我们把pkill掉的172.60.0.2(旧主节点)重启,在看看新任Master主节点172.60.0.3有何变化

重启的旧主节点成为从节点并加入进来,此时正在进行复制动作,offset的偏移量还没有同步一致,

基于docker实现哨兵集群部署

【小结】

进入哨兵节点客户端执行SENTINEL masterts命令显示被监控的所有master以及状态

127.0.0.1:> SENTINEL masters
) ) "name"
) "mymaster" #被监控主节点的名称
) "ip"
) "172.60.0.3" #被监控主节点的IP
) "port"
) ""
) "runid"
) "dd3696a2793e4e19892fca48793d75cec3f07bea" #被监控主节点的runid值
) "flags"
) "master"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) "" #监控节点不可达超时时间
) "info-refresh"
) ""
) "role-reported"
) "master"
) "role-reported-time"
) ""
) "config-epoch"
) ""
) "num-slaves" #检测剩余slave节点个数
) ""
) "num-other-sentinels" #检测其它sentinel节点个数
) ""
) "quorum" #允许主节点不可用的sentinels的数量(最多允许两个sentinel节点故障)
) ""
) "failover-timeout" #延迟时间
) ""
) "parallel-syncs" #复制转移数量
) ""

 执行SENTINEL slaves mymastert查看从节点信息,此时你会看到两个从节点

)  ) "name"
) "172.60.0.2:6379"
) "ip"
) "172.60.0.2"
) "port"
) ""
) "runid"
) "3ede55439a3ce6fb1ab171ed7fd6b6c639725966"
) "flags"
) "slave"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) ""
) "info-refresh"
) ""
) "role-reported"
) "slave"
) "role-reported-time"
) ""
) "master-link-down-time"
) ""
) "master-link-status"
) "ok"
) "master-host"
) "172.60.0.3"
) "master-port"
) ""
) "slave-priority"
) ""
) "slave-repl-offset"
) ""
) ) "name"
) "172.60.0.4:6379"
) "ip"
) "172.60.0.4"
) "port"
) ""
) "runid"
) "f17cfcfc4b9217e1f5a3c0d0a2c55d82da46c37e"
) "flags"
) "slave"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) ""
) "info-refresh"
) ""
) "role-reported"
) "slave"
) "role-reported-time"
) ""
) "master-link-down-time"
) ""
) "master-link-status"
) "ok"
) "master-host"
) "172.60.0.3"
) "master-port"
) ""
) "slave-priority"
) ""
) "slave-repl-offset"
) ""

 查看主节点的端口

127.0.0.1:> sentinel get-master-addr-by-name mymaster
) "172.60.0.3"
) ""

执行sentinel failover mymaster命令,强制切换主节点!下图所示,发现主节点已经由172.60.0.3变为172.60.0.2了,

基于docker实现哨兵集群部署

基于docker实现哨兵集群部署

待更。。。。

上一篇:PTA 面向对象程序设计 6-1 引用作函数形参交换两个整数


下一篇:linux(ubuntu)下安装phantomjs