Redis配置sentinel模式
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
哨兵(sentinel)主要是完成三个功能:监控,通知,自动故障转移功能。sentinel是安装Redis服务时默认安装的,因此我们可以直接使用!
一.配置项说明
1>.port
服务的监听端口,比如:port 26479。
2>.sentinel monitor <master-name> <ip><redis-port><quorm>
<quorum>表示sentinel集群的quorum机制,即至少有quorum个sentinel节点同时判断主节点故障时,才任务其真的故障。
s_down:subjectively down
o_down:objectively down
3>.semtinel auth-pass <master-name><password>
4>.sentinel down-afer-milliseconds <master-name><millseconds>
监听到指定的集群的主节点异常状态持久多久方才将标记为“故障”。
5>.sentinel parallel-syncs <master-name> <numslaves>
指在failover过程中,能够被sentinel并行配置的从节点的数量。
6>.sentinel failover-timeout <master-name> <milliseconds>
sentinel必须在此指定的时间长内完成故障转移操作,否则,将视为故障转移操作失败。
7>.sentinel notification-script <master-name> <script-path>
通知脚本,此脚本被自动传递多个参数。
8>.redis-cli -h SENTINEL_HOST -p SENTINEL_PORT
二.实操案例
1>.编写sentinel的配置文件(我们复用redis集群的虚拟机)
[[email protected] ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
bind node101.yinzhengjie.org.cn
port
dir /tmp
sentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn
sentinel auth-pass yinzhengjie-redis yinzhengjie
sentinel down-after-milliseconds yinzhengjie-redis
sentinel parallel-syncs yinzhengjie-redis
sentinel failover-timeout yinzhengjie-redis
logfile /var/log/redis/sentinel.log
[[email protected] ~]#
[[email protected] ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
[[email protected] ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
bind node102.yinzhengjie.org.cn
port
dir /tmp
sentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn
sentinel auth-pass yinzhengjie-redis yinzhengjie
sentinel down-after-milliseconds yinzhengjie-redis
sentinel parallel-syncs yinzhengjie-redis
sentinel failover-timeout yinzhengjie-redis
logfile /var/log/redis/sentinel.log
[[email protected] ~]#
[[email protected] ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
[[email protected] ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
bind node103.yinzhengjie.org.cn
port
dir /tmp
sentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn
sentinel auth-pass yinzhengjie-redis yinzhengjie
sentinel down-after-milliseconds yinzhengjie-redis
sentinel parallel-syncs yinzhengjie-redis
sentinel failover-timeout yinzhengjie-redis
logfile /var/log/redis/sentinel.log
[[email protected] ~]#
[[email protected] ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
2>.启动sentinel服务
[[email protected] ~]# systemctl start redis-sentinel
[[email protected] ~]#
[[email protected] ~]# systemctl status redis-sentinel
● redis-sentinel.service - Redis Sentinel
Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis-sentinel.service.d
└─limit.conf
Active: active (running) since Sat -- :: CST; 11s ago
Main PID: (redis-sentinel)
CGroup: /system.slice/redis-sentinel.service
└─ /usr/bin/redis-sentinel node101.yinzhengjie.org.cn: [sentinel] Apr :: node101.yinzhengjie.org.cn systemd[]: Starting Redis Sentinel...
Apr :: node101.yinzhengjie.org.cn systemd[]: Started Redis Sentinel.
[[email protected] ~]#
[[email protected] ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 172.30.1.101: *:*
LISTEN 172.30.1.101: *:*
LISTEN *: *:*
LISTEN ::: :::*
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# tail /etc/redis-sentinel.conf
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel config-epoch yinzhengjie-redis
sentinel leader-epoch yinzhengjie-redis
sentinel known-slave yinzhengjie-redis 172.30.1.102
sentinel known-slave yinzhengjie-redis 172.30.1.103
sentinel known-sentinel yinzhengjie-redis 172.30.1.102 9c195f20e954d9032f57900c1839857ee4c31be4
sentinel known-sentinel yinzhengjie-redis 172.30.1.103 fbafb3bdbd04c61cf9c4e8dcf0147c7a20f22243
sentinel current-epoch
[[email protected] ~]#
[[email protected] ~]# systemctl start redis-sentinel
[[email protected] ~]# systemctl start redis-sentinel
[[email protected] ~]#
[[email protected] ~]# systemctl status redis-sentinel
● redis-sentinel.service - Redis Sentinel
Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis-sentinel.service.d
└─limit.conf
Active: active (running) since Sat -- :: CST; 42s ago
Main PID: (redis-sentinel)
CGroup: /system.slice/redis-sentinel.service
└─ /usr/bin/redis-sentinel node102.yinzhengjie.org.cn: [sentinel] Apr :: node102.yinzhengjie.org.cn systemd[]: Starting Redis Sentinel...
Apr :: node102.yinzhengjie.org.cn systemd[]: Started Redis Sentinel.
[[email protected] ~]#
[[email protected] ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 172.30.1.102: *:*
LISTEN 172.30.1.102: *:*
LISTEN *: *:*
LISTEN ::: :::*
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# tail /etc/redis-sentinel.conf
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel config-epoch yinzhengjie-redis
sentinel leader-epoch yinzhengjie-redis
sentinel known-slave yinzhengjie-redis 172.30.1.103
sentinel known-slave yinzhengjie-redis 172.30.1.102
sentinel known-sentinel yinzhengjie-redis 172.30.1.103 fbafb3bdbd04c61cf9c4e8dcf0147c7a20f22243
sentinel known-sentinel yinzhengjie-redis 172.30.1.101 5697f4a91fe1bf43f075e729bdde6ec504dc820e
sentinel current-epoch
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# systemctl start redis-sentinel
[[email protected] ~]# systemctl start redis-sentinel
[[email protected] ~]#
[[email protected] ~]# systemctl status redis-sentinel
● redis-sentinel.service - Redis Sentinel
Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis-sentinel.service.d
└─limit.conf
Active: active (running) since Sat -- :: PDT; 5s ago
Main PID: (redis-sentinel)
CGroup: /system.slice/redis-sentinel.service
└─ /usr/bin/redis-sentinel node103.yinzhengjie.org.cn: [sentinel] Apr :: node103.yinzhengjie.org.cn systemd[]: Starting Redis Sentinel...
Apr :: node103.yinzhengjie.org.cn systemd[]: Started Redis Sentinel.
[[email protected] ~]#
[[email protected] ~]# tail /etc/redis-sentinel.conf
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel config-epoch yinzhengjie-redis
sentinel leader-epoch yinzhengjie-redis
sentinel known-slave yinzhengjie-redis 172.30.1.102
sentinel known-slave yinzhengjie-redis 172.30.1.103
sentinel known-sentinel yinzhengjie-redis 172.30.1.101 5697f4a91fe1bf43f075e729bdde6ec504dc820e
sentinel known-sentinel yinzhengjie-redis 172.30.1.102 9c195f20e954d9032f57900c1839857ee4c31be4
sentinel current-epoch
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# systemctl start redis-sentinel
3>.查看当前集群的状态
[[email protected] ~]# redis-cli -h node101.yinzhengjie.org.cn -p
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> SENTINEL masters
) ) "name"
) "yinzhengjie-redis"
) "ip"
) "172.30.1.101"
) "port"
) ""
) "runid"
) "514f95dd0a54449ec221b662da02caa65f74353e"
) "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"
) ""
) "num-other-sentinels"
) ""
) "quorum"
) ""
) "failover-timeout"
) ""
) "parallel-syncs"
) ""
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:26379> SENTINEL masters #查看主节点信息
node101.yinzhengjie.org.cn:> SENTINEL slaves yinzhengjie-redis
) ) "name"
) "172.30.1.102:6379"
) "ip"
) "172.30.1.102"
) "port"
) ""
) "runid"
) "2b4d70685d17183e80816314d160162770d01aec"
) "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.30.1.101"
) "master-port"
) ""
) "slave-priority"
) ""
) "slave-repl-offset"
) ""
) ) "name"
) "172.30.1.103:6379"
) "ip"
) "172.30.1.103"
) "port"
) ""
) "runid"
) "4ed8d98123094a5922650a4d7047bba03851f6b7"
) "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.30.1.101"
) "master-port"
) ""
) "slave-priority"
) ""
) "slave-repl-offset"
) ""
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:26379> SENTINEL slaves yinzhengjie-redis #查看从节点信息
我们可以使用SENTINEL实现自动容灾,也可以根据SENTINEL提供的信息手动修改master节点等操作。