Redis哨兵安装

哨兵集群信息说明

  • 监控:Sentinel会不断检查您的主实例和副本实例是否按预期工作。

  • 通知:Sentinel可以通过API通知系统管理员或其他计算机程序,其中一个受监视的Redis实例出了问题。

  • 自动故障转移:如果主服务器未按预期工作,则Sentinel可以启动故障转移过程,在该过程中,将副本升级为主服务器,将其他副本复制为使用新主服务器,并通知使用Redis服务器的应用程序要使用的新地址。连接时。

  • 配置提供程序:Sentinel充当客户端服务发现的授权来源:客户端连接到Sentinels,以询问负责给定服务的当前Redis主服务器的地址。如果发生故障转移,Sentinels将报告新地址。

一、部署主从环境

主机名地址角色
node1 10.186.60.140 初始主库-哨兵
node2 10.186.60.143 初始从库-哨兵
node3 10.186.60.146 初始从库-哨兵
1.1 先决条件
[root@node1 ~]# ./dbawsp_centos7_install_redis_v0.3.sh init 6379
[root@node2 ~]# ./dbawsp_centos7_install_redis_v0.3.sh init 6379
[root@node3 ~]# ./dbawsp_centos7_install_redis_v0.3.sh init 6379
1.2 建立主从
  • 使用交互模式手动命令建立主从关系

  • 示例只写 node2 节点连接 node1 节点的命令,node3 节点同 node2 一样的操作

[root@node2 ~]# /data/redis/base/5.0.8/bin/redis-cli -p 6379
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> slaveof 10.186.60.140 6379
OK
  • 或者加入参数到 redis 配置文件中
vim /data/redis/conf/6379/redis_6379.conf

slaveof 10.186.60.140 6379
  • 主库检测主从信息,例如从库节点数量,从库IP信息等
127.0.0.1:6379> info Replication 
# Replication
role:master
connected_slaves:2           # 当前主库 redis 一共连接了两个 slave 从库
slave0:ip=10.186.60.143,port=6379,state=online,offset=1764,lag=1      # 第一个从库的信息
slave1:ip=10.186.60.146,port=6379,state=online,offset=1764,lag=1      # 第二个从库的信息
master_replid:ce321673f2c1e7d8d02328a763d15337be12fde7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1764
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:2147483648
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1764

二、部署哨兵节点

  • 示例只写 node1 节点加入 systemd 管理的配置,其余两台 sentinel 哨兵节点同样操作
2.1 创建配置文件
[root@node1 ~]# mkdir -p /data/redis/{conf,data}/26379
[root@node1 ~]# vim /data/redis/conf/26379/sentinel_26379.conf

bind 0.0.0.0
port 26379
daemonize yes
dir /data/redis/data/26379
logfile "sentinel_26379.log"
pidfile "sentinel_26379.pid"
loglevel debug
sentinel monitor mymaster 10.186.60.140 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
2.2 托管启动管理
[root@node1 ~]# vim /etc/systemd/system/sentinel_26379.service

[Unit]
Description=Redis
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/redis/data/26379/sentinel_26379.pid
ExecStart=/data/redis/base/5.0.8/bin/redis-sentinel /data/redis/conf/26379/sentinel_26379.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
User=redis
Group=redis
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@node1 ~]# chown -R redis.redis /data/redis
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl start sentinel_26379.service

三、主从切换日志

  • 当前主库节点 node1 ,现在要关闭 node1 节点的 redis 服务,让 sentinel 哨兵进行高可用切换
3.1 node1日志
[root@node1 ~]# tailf /data/redis/data/26379/sentinel_26379.log

31108:X 18 Aug 2020 06:34:54.303 # +sdown master mymaster 10.186.60.140 6379
31108:X 18 Aug 2020 06:34:54.485 # +new-epoch 1
31108:X 18 Aug 2020 06:34:54.485 # +vote-for-leader b07a725900ca86d349da51563d95c37b69c337da 1
31108:X 18 Aug 2020 06:34:54.916 # +config-update-from sentinel b07a725900ca86d349da51563d95c37b69c337da 10.186.60.143 26379 @ mymaster 10.186.60.140 6379
31108:X 18 Aug 2020 06:34:54.917 # +switch-master mymaster 10.186.60.140 6379 10.186.60.143 6379
31108:X 18 Aug 2020 06:34:54.917 * +slave slave 10.186.60.146:6379 10.186.60.146 6379 @ mymaster 10.186.60.143 6379
31108:X 18 Aug 2020 06:34:54.917 * +slave slave 10.186.60.140:6379 10.186.60.140 6379 @ mymaster 10.186.60.143 6379
31108:X 18 Aug 2020 06:35:24.988 # +sdown slave 10.186.60.140:6379 10.186.60.140 6379 @ mymaster 10.186.60.143 6379
3.2 node2日志
[root@node2 ~]# tailf /data/redis/data/26379/sentinel_26379.log

30638:X 18 Aug 2020 06:34:54.413 # +sdown master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.484 # +odown master mymaster 10.186.60.140 6379 #quorum 2/2
30638:X 18 Aug 2020 06:34:54.484 # +new-epoch 1
30638:X 18 Aug 2020 06:34:54.484 # +try-failover master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.491 # +vote-for-leader b07a725900ca86d349da51563d95c37b69c337da 1
30638:X 18 Aug 2020 06:34:54.493 # a22f649b1930561d5c6a5eb9c5604032373a76e8 voted for b07a725900ca86d349da51563d95c37b69c337da 1
30638:X 18 Aug 2020 06:34:54.494 # fcf04d1271383a410d5c113d4c5886cd95e16e49 voted for b07a725900ca86d349da51563d95c37b69c337da 1
30638:X 18 Aug 2020 06:34:54.546 # +elected-leader master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.546 # +failover-state-select-slave master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.629 # +selected-slave slave 10.186.60.143:6379 10.186.60.143 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.629 * +failover-state-send-slaveof-noone slave 10.186.60.143:6379 10.186.60.143 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.691 * +failover-state-wait-promotion slave 10.186.60.143:6379 10.186.60.143 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.865 - -role-change slave 10.186.60.143:6379 10.186.60.143 6379 @ mymaster 10.186.60.140 6379 new reported role is master
30638:X 18 Aug 2020 06:34:54.866 # +promoted-slave slave 10.186.60.143:6379 10.186.60.143 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.866 # +failover-state-reconf-slaves master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:54.924 * +slave-reconf-sent slave 10.186.60.146:6379 10.186.60.146 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:55.619 # -odown master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:55.870 * +slave-reconf-inprog slave 10.186.60.146:6379 10.186.60.146 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:55.870 * +slave-reconf-done slave 10.186.60.146:6379 10.186.60.146 6379 @ mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:55.952 # +failover-end master mymaster 10.186.60.140 6379
30638:X 18 Aug 2020 06:34:55.952 # +switch-master mymaster 10.186.60.140 6379 10.186.60.143 6379
30638:X 18 Aug 2020 06:34:55.952 * +slave slave 10.186.60.146:6379 10.186.60.146 6379 @ mymaster 10.186.60.143 6379
30638:X 18 Aug 2020 06:34:55.953 * +slave slave 10.186.60.140:6379 10.186.60.140 6379 @ mymaster 10.186.60.143 6379
30638:X 18 Aug 2020 06:35:25.986 # +sdown slave 10.186.60.140:6379 10.186.60.140 6379 @ mymaster 10.186.60.143 6379
3.3 node3日志
[root@node3 ~]# tailf /data/redis/data/26379/sentinel_26379.log

30858:X 18 Aug 2020 06:34:54.437 # +sdown master mymaster 10.186.60.140 6379
30858:X 18 Aug 2020 06:34:54.496 # +new-epoch 1
30858:X 18 Aug 2020 06:34:54.497 # +vote-for-leader b07a725900ca86d349da51563d95c37b69c337da 1
30858:X 18 Aug 2020 06:34:54.497 # +odown master mymaster 10.186.60.140 6379 #quorum 3/2
30858:X 18 Aug 2020 06:34:54.497 # Next failover delay: I will not start a failover before Tue Aug 18 06:40:55 2020
30858:X 18 Aug 2020 06:34:54.928 # +config-update-from sentinel b07a725900ca86d349da51563d95c37b69c337da 10.186.60.143 26379 @ mymaster 10.186.60.140 6379
30858:X 18 Aug 2020 06:34:54.928 # +switch-master mymaster 10.186.60.140 6379 10.186.60.143 6379
30858:X 18 Aug 2020 06:34:54.928 * +slave slave 10.186.60.146:6379 10.186.60.146 6379 @ mymaster 10.186.60.143 6379
30858:X 18 Aug 2020 06:34:54.928 * +slave slave 10.186.60.140:6379 10.186.60.140 6379 @ mymaster 10.186.60.143 6379
30858:X 18 Aug 2020 06:35:24.971 # +sdown slave 10.186.60.140:6379 10.186.60.140 6379 @ mymaste
posted @ 2020-10-29 15:19  老虎逛大街  阅读(151)  评论(0编辑  收藏  举报
levels of contents