Redis Sentinel是Redis分布式系统的监控工具,它能够监控Redis集群中每个节点的运行状态,并在节点故障时进行自动故障转移,从而保证Redis集群的高可用性。下面是采用Redis Sentinel实现高可用集群监控的完整配置步骤:
- 安装Redis Sentinel
首先需要安装Redis Sentinel。可以通过以下命令进行安装:
sudo apt-get install redis-server redis-tools redis-sentinel
- 配置Redis主从及Sentinel节点
主从节点分别运行在不同的机器上,Sentinel节点也可以运行在不同的机器上。配置文件的示例如下:
主节点:
# /etc/redis/redis.conf
port 6379 # main port
bind 127.0.0.1 # bind to local IP address
dir /var/lib/redis # data directory
logfile "/var/log/redis/redis-server.log"
requirepass password # set password for redis server
masterauth password # set password for slave server to connect
masterinfoformat "ip=%s,port=%s,state=%s,info=%s" # set the format of master node info that is sent to the slave node
从节点:
# /etc/redis/redis.conf
port 6380 # slave port
slaveof 127.0.0.1 6379 # set the address and port of the master node
bind 127.0.0.1 # bind to local IP address
dir /var/lib/redis # data directory
logfile "/var/log/redis/redis-server.log"
requirepass password # set password for redis server
masterauth password # set password for slave server to connect
Sentinel节点:
# /etc/redis/sentinel.conf
port 26379 # sentinel port
bind 127.0.0.1 # bind to local IP address
dir /var/lib/redis # data directory
logfile "/var/log/redis/redis-sentinel.log"
sentinel monitor mymaster 127.0.0.1 6379 2 # set sentinel to monitor the master node, and set the quorum to 2
sentinel down-after-milliseconds mymaster 5000 # set the time after which a node is considered down
sentinel failover-timeout mymaster 60000 # set the time after which a failover is considered successful
sentinel auth-pass mymaster password # set the password for monitoring
- 启动Redis主从及Sentinel节点
在启动前,需要确保主从节点的Redis服务已启动。然后可以依次启动主从节点和Sentinel节点:
启动主节点:
redis-server /etc/redis/redis.conf
启动从节点:
redis-server /etc/redis/redis.conf --port 6380 --slaveof 127.0.0.1 6379
启动Sentinel节点:
redis-sentinel /etc/redis/sentinel.conf
- 检查Redis Sentinel是否生效
在已启动Redis Sentinel节点的机器上,运行以下命令:
redis-cli -p 26379 sentinel master mymaster
则会显示当前master节点的信息,如下所示:
1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
8) "long-string-of-characters"
9) "flags"
10) "master"
11) "pending-commands"
12) "0"
13) "last-ok-ping-reply"
14) "429"
15) "last-ping-reply"
16) "429"
17) "info-refresh"
18) "5847"
19) "num-other-sentinels"
20) "0"
21) "quorum"
22) "2"
23) "failover-timeout"
24) "60000"
25) "parallel-syncs"
26) "1"
- 故障转移测试
在主节点所在的机器上,在Redis配置文件中的bind
后加上0.0.0.0
,并重启Redis服务。然后可以观察到Sentinel节点会自动检测到主节点故障,进行故障转移,将从节点提升为主节点。运行以下命令,检查主节点信息:
redis-cli -p 26379 sentinel masters
切换完成后,从节点会输出如下信息:
slave: ip=127.0.0.1,port=6380,state=online,offset=XXX,lag=0,slave_read_only=1
示例1:添加Sentinel节点
可以通过添加Sentinel节点的方式,提高Redis集群的可用性。示例如下:
- 在另外一台机器上配置Redis Sentinel:
# /etc/redis/sentinel.conf
port 26380 # sentinel port
bind 0.0.0.0 # bind to all IP addresses
dir /var/lib/redis # data directory
logfile "/var/log/redis/redis-sentinel.log"
sentinel monitor mymaster 127.0.0.1 6379 2 # set sentinel to monitor the master node, and set the quorum to 2
sentinel down-after-milliseconds mymaster 5000 # set the time after which a node is considered down
sentinel failover-timeout mymaster 60000 # set the time after which a failover is considered successful
sentinel auth-pass mymaster password # set the password for monitoring
- 启动新的Sentinel节点:
redis-sentinel /etc/redis/sentinel.conf
- 在已启动的Sentinel节点中添加新的Sentinel节点:
redis-cli -p 26379 sentinel add mymaster 127.0.0.1 26380
示例2:查看Redis Sentinel状态
可以通过以下命令查看每个Sentinel节点的状态:
redis-cli -p 26379 sentinel sentinels mymaster
输出示例如下:
1) 1) "name"
2) "127.0.0.1:26379"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "26379"
7) "runid"
8) "f35aa380bd0595e2f5d85e22ee5fec5eaaa18476"
9) "flags"
10) "sentinel"
11) "pending-commands"
12) "0"
13) "last-ok-ping-reply"
14) "0"
15) "last-ping-reply"
16) "0"
17) "info-refresh"
18) "1644"
19) "num-other-sentinels"
20) "3"
21) "quorum"
22) "2"
23) "failover-timeout"
24) "60000"
25) "parallel-syncs"
26) "1"
2) 1) "name"
2) "127.0.0.1:26381"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "26381"
7) "runid"
8) "c63ec77f2b11654a3e7a23acacc046ff5d43caf5"
9) "flags"
10) "sentinel"
11) "pending-commands"
12) "0"
13) "last-ok-ping-reply"
14) "0"
15) "last-ping-reply"
16) "0"
17) "info-refresh"
18) "1644"
19) "num-other-sentinels"
20) "3"
21) "quorum"
22) "2"
23) "failover-timeout"
24) "60000"
25) "parallel-syncs"
26) "1"
可见当前存在两个Sentinel节点。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:redis sentinel监控高可用集群实现的配置步骤 - Python技术站