author:JevonWei

版权声明:原创作品

blog:http://119.23.52.191/

构建Sentinel监控Redis的主节点架构

拓扑结构结构

拓扑环境

master 	172.16.252.82
slave1 172.16.252.184
slave2 172.16.252.67
sentinl1 172.16.252.82
sentinl2 172.16.252.184
sentinl3 172.16.252.67

实现Redis的主从复制

各节点安装redis(epel)

    [root@master ~]# yum -y install redis
[root@master ~]# setenforce 0
[root@master ~]# iptables -F
[root@master ~]# systemctl start redis
[root@slave1 ~]# yum -y install redis
[root@slave1 ~]# setenforce 0
[root@slave1 ~]# iptables -F
[root@slave1 ~]# systemctl start redis
[root@slave2 ~]# yum -y install redis
[root@slave2 ~]# setenforce 0
[root@slave2 ~]# iptables -F

配置redis

master

    [root@slave1 ~]# vim /etc/redis.conf
bind 172.16.252.82 \\监听本地IP
requirepass "danran" 添加认证密码
[root@slave1 ~]# systemctl restart redis
[root@slave1 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.16.252.184:6379 *:*

slave1

    [root@slave1 ~]# vim /etc/redis.conf
bind 172.16.252.184 \\监听本地IP
[root@slave1 ~]# systemctl restart redis
[root@slave1 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.16.252.184:6379 *:*
[root@slave1 ~]# redis-cli -h 172.16.252.184
172.16.252.184:6379> SLAVEOF 172.16.252.82 6379 设置成为172.16.252.82的从数据库
OK
172.16.252.184:6379> CONFIG SET masterauth danran 设置master主服务端的认证密码
OK
172.16.252.184:6379> CONFIG REWRITE 保存到配置文件永久生效
OK

slave2

    [root@slave2 ~]# vim /etc/redis.conf
bind 172.16.252.67 \\监听本地IP
slaveof 172.16.252.82 6379
masterauth "danran"
修改REPLICAION配置段的slaveof和masterauth参数设置从节点
[root@slave2 ~]# systemctl start redis
[root@slave2 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.16.252.67:6379 *:*

master查看主从节点信息

    [root@master ~]# redis-cli -h 172.16.252.82
172.16.252.82:6379> AUTH danran
OK
172.16.252.82:6379> INFO Replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.252.184,port=6379,state=online,offset=652,lag=0 \\lag表示与master节点同步延时时长
slave1:ip=172.16.252.67,port=6379,state=online,offset=652,lag=0
master_repl_offset:652
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:651

测试数据同步

master

    [root@master ~]# redis-cli -h 172.16.252.82
172.16.252.82:6379> AUTH danran
OK
172.16.252.82:6379> ZADD colors 1 red 2 bulue 6 green 4 gray
(integer) 4

slave1

    [root@slave1 ~]# redis-cli -h 172.16.252.184
172.16.252.184:6379> ZRANGE colors 0 5
1) "red"
2) "bulue"
3) "gray"
4) "green"

slave2

    [root@slave2 ~]# redis-cli -h 172.16.252.67
172.16.252.67:6379> ZRANGE colors 0 5
1) "red"
2) "bulue"
3) "gray"
4) "green"

配置Sentinel系统

sentinel1

[root@sentinel ~]# vim /etc/redis-sentinel.conf
bind 172.16.252.82 监听IP
sentinel monitor mymaster 172.16.252.82 6379 2 mymaster为master的标识符,监控172.16.252.82主机的6379端口,至少2个主机投票推举方可推举出主节点
sentinel auth-pass mymaster danran sentinel认证密码,建议所有节点的密码认证一致
sentinel down-after-milliseconds mymaster 5000 当超出5s连接不到主节点时,则表示主节点故障,重新推选出主节点
[root@sentinel ~]# systemctl start redis-sentinel
[root@sentinel ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:26379 *:*

sentinel2

[root@sentine2 ~]# vim /etc/redis-sentinel.conf
bind 172.16.252.184 监听地址
sentinel monitor mymaster 172.16.252.82 6379 2 mymaster为master的标识符,监控172.16.252.82主机的6379端口,至少2个主机投票推举方可推举出主节点
sentinel auth-pass mymaster danran sentinel认证密码,建议所有节点的密码认证一致
sentinel down-after-milliseconds mymaster 5000 当超出5s连接不到主节点时,则表示主节点故障,重新推选出主节点
[root@sentine2 ~]# systemctl start redis-sentinel
[root@sentine2 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:26379 *:*

sentinel3

[root@sentine3 ~]# vim /etc/redis-sentinel.conf
bind 172.16.252.67 监听IP
sentinel monitor mymaster 172.16.252.82 6379 2 mymaster为master的标识符,监控172.16.252.82主机的6379端口,至少2个主机投票推举方可推举出主节点
sentinel auth-pass mymaster danran sentinel认证密码,建议所有节点的密码认证一致
sentinel down-after-milliseconds mymaster 5000 当超出5s连接不到主节点时,则表示主节点故障,重新推选出主节点
[root@sentine3 ~]# systemctl start redis-sentinel
[root@sentine3 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:26379 *:*

sentinel查看主从节点信息并测试主从节点切换

[root@sentinel ~]# redis-cli -h 172.16.252.82 -p 26379
172.16.252.82:26379> SENTINEL masters 显示主节点的属性信息,主节点为172.16.252.82
1) 1) "name"
2) "mymaster" 主节点名称
3) "ip"
4) "172.16.252.82"
5) "port"
6) "6379"
7) "runid"
8) "2847d8ceda8d3a03f0df814091fc5174921f57ea"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "198"
19) "last-ping-reply"
20) "198"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "4299"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "4305"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout" 超时时间
38) "180000"
39) "parallel-syncs"
40) "1" 172.16.252.82:26379> SENTINEL slave mymaster 显示主节点为mymaster的从节点属性信息
(error) ERR Unknown sentinel subcommand 'slave'
172.16.252.82:26379> SENTINEL slaves mymaster
1) 1) "name"
2) "172.16.252.184:6379"
3) "ip"
4) "172.16.252.184"
5) "port"
6) "6379"
7) "runid"
8) ""
9) "flags"
10) "s_down,slave"
11) "link-pending-commands"
12) "14"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "233250"
17) "last-ok-ping-reply"
18) "233250"
19) "last-ping-reply"
20) "233250"
21) "s-down-time"
22) "230233"
23) "down-after-milliseconds"
24) "3000"
25) "info-refresh"
26) "1505553054438"
27) "role-reported"
28) "slave"
29) "role-reported-time"
30) "233250"
31) "master-link-down-time"
32) "0"
33) "master-link-status"
34) "err"
35) "master-host"
36) "?"
37) "master-port"
38) "0"
39) "slave-priority"
40) "100"
41) "slave-repl-offset"
42) "0"
2) 1) "name"
2) "172.16.252.67:6379"
3) "ip"
4) "172.16.252.67"
5) "port"
6) "6379"
7) "runid"
8) "fc91077ac51c954817a85922908320f7f70e2d61"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "374"
19) "last-ping-reply"
20) "374"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "2509"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "233250"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "172.16.252.82"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "186072" 172.16.252.82:26379> SENTINEL failover mymaster 手动切换主节点
OK
172.16.252.82:26379> SENTINEL masters 主节点由172.16.252.82切换到了172.16.252.67
1) 1) "name"
2) "mymaster"
3) "ip"
4) "172.16.252.67"
5) "port"
6) "6379"
7) "runid"
8) "fc91077ac51c954817a85922908320f7f70e2d61"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "37"
19) "last-ping-reply"
20) "37"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "8118"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "27140"
29) "config-epoch"
30) "1"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1" 172.16.252.82:26379> SENTINEL slaves mymaster 显示主节点名称为mymaster的从节点属性信息
1) 1) "name"
2) "172.16.252.184:6379"
3) "ip"
4) "172.16.252.184"
5) "port"
6) "6379"
7) "runid"
8) ""
9) "flags"
10) "s_down,slave"
11) "link-pending-commands"
12) "9"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "138676"
17) "last-ok-ping-reply"
18) "138676"
19) "last-ping-reply"
20) "138676"
21) "s-down-time"
22) "135668"
23) "down-after-milliseconds"
24) "3000"
25) "info-refresh"
26) "1505553462755"
27) "role-reported"
28) "slave"
29) "role-reported-time"
30) "138676"
31) "master-link-down-time"
32) "0"
33) "master-link-status"
34) "err"
35) "master-host"
36) "?"
37) "master-port"
38) "0"
39) "slave-priority"
40) "100"
41) "slave-repl-offset"
42) "0"
2) 1) "name"
2) "172.16.252.82:6379"
3) "ip"
4) "172.16.252.82"
5) "port"
6) "6379"
7) "runid"
8) "2847d8ceda8d3a03f0df814091fc5174921f57ea"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "363"
19) "last-ping-reply"
20) "363"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "65"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "118575"
29) "master-link-down-time"
30) "1505553462000"
31) "master-link-status"
32) "err"
33) "master-host"
34) "172.16.252.67"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "1" 模拟主节点172.16.252.67故障,则主节点完成切换
[root@slave2 ~]# killall redis-server 172.16.252.82:26379> SENTINEL masters 主节点由172.16.252.67切换成了172.16.252.184
1) 1) "name"
2) "mymaster"
3) "ip"
4) "172.16.252.184"
5) "port"
6) "6379"
7) "runid"
8) ""
9) "flags"
10) "s_down,o_down,master"
11) "link-pending-commands"
12) "100"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "9613"
17) "last-ok-ping-reply"
18) "9613"
19) "last-ping-reply"
20) "9613"
21) "s-down-time"
22) "6611"
23) "o-down-time"
24) "5466"
25) "down-after-milliseconds"
26) "3000"
27) "info-refresh"
28) "14593"
29) "role-reported"
30) "master"
31) "role-reported-time"
32) "9613"
33) "config-epoch"
34) "2"
35) "num-slaves"
36) "2"
37) "num-other-sentinels"
38) "2"
39) "quorum"
40) "2"
41) "failover-timeout"
42) "180000"
43) "parallel-syncs"
44) "1" 使故障的172.16.252.67上线
[root@slave2 ~]# systemctl start redis 再次查看mymaster主节点的从节点信息
172.16.252.82:26379> SENTINEL slaves mymaster
1) 1) "name"
2) "172.16.252.67:6379"
3) "ip"
4) "172.16.252.67"
5) "port"
6) "6379"
7) "runid"
8) "a8be66745daee322e5c2a2796a5a07623e6ab25d"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "102"
19) "last-ping-reply"
20) "102"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "785"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "91331"
29) "master-link-down-time"
30) "1505554185000"
31) "master-link-status"
32) "err"
33) "master-host"
34) "172.16.252.184"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "1"
2) 1) "name"
2) "172.16.252.82:6379"
3) "ip"
4) "172.16.252.82"
5) "port"
6) "6379"
7) "runid"
8) "2847d8ceda8d3a03f0df814091fc5174921f57ea"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "913"
19) "last-ping-reply"
20) "913"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "572"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "306685"
29) "master-link-down-time"
30) "1505554185000"
31) "master-link-status"
32) "err"
33) "master-host"
34) "172.16.252.184"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "1" 切换过程注意查看sentinel的日志信息
[root@slave1 ~]# tail /var/log/redis/sentinel.log

Sentinel系统监控Redis主从节点的更多相关文章

  1. Grafana Prometheus系统监控Redis服务

    Grafana Prometheus系统监控Redis服务 一.Grafana Prometheus系统监控Redis服务 1.1流程 1.2安装redis_exporter 1.3配置prometh ...

  2. (六)Redis主从自动恢复-sentinel

    原文地址,转载请注明出处: http://blog.csdn.net/qq_34021712/article/details/72026313     ©王赛超 准备工作:(1个master,2个sl ...

  3. redis 主从哨兵02

    一.为什么要复制 1.实现数据的多副本存储,从而可以实现服务的高可用 2.提供更好的读性能,分担读请求 二.复制技术的关键点及难点 1.如何指定被复制对象 2.增量还是全量,以及如何实现增量 3.复制 ...

  4. Redis主从&哨兵集群搭建

    主从集群 在搭建主从集群前,我们先把Redis安装起来: #解压Redis压缩包 [root@master lf]# tar -zxvf redis-6.2.1.tar.gz -- #安装gcc [r ...

  5. Redis主从原理及哨兵模式

    1.Redis主从搭建 主从的搭建很简单,主节点设置连接密码,从节点的配置上主节点的ip和端口,以及密码,一般从节点我们都设置只读模式. 主节点配置: 主节点密码: requirepass xxx 从 ...

  6. redis主从+ 哨兵模式(sentinel)+漂移VIP实现高可用系统

    原文:https://www.jianshu.com/p/c2ab606b00b7 客户端程序 客户端程序(如PHP程序)连接redis时需要ip和port,但redis-server进行故障转移时, ...

  7. redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用

    目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...

  8. spring集成redis——主从配置以及哨兵监控

    Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...

  9. Redis Sentinel(哨兵)主从高可用方案

    环境搭建 三台服务器: 192.168.126.100(master) 192.168.126.110(slaver) 192.168.126.120(slaver) 拷贝192.168.126.10 ...

随机推荐

  1. hdu-1874 畅通工程续---模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 求起点到终点的最短距离 解题思路: 注意重边,其他的就是模板 #include&l ...

  2. 问题 M: 克隆玩具

    题目描述 你只有一个A类型玩具,现在有个有两种功能的机器:1. 加工一个A类型的玩具能够再得到一个A类型的玩具和一个B类型的玩具.2. 加工一个B类型的玩具,能得到两个B类型的玩具. 问经过多次加工之 ...

  3. subline 安装 package control

    subline text2 输入 import urllib2,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa154 ...

  4. PageHelper 记录总条数不正确问题处理

    //PageHelper.startPage会返回一个page对象,这个对象在查询结果出来后会把页数,记录总数给page对象,用page.getPages()和getTotal()获取页数和记录总数. ...

  5. Luogu [P1334] 瑞瑞的木板(手写堆)

    其实这个题完全不需要用手写堆,只需要一遍遍sort就行了…… 但是! 为了练习手写堆,还是用手写堆做了. 在做本题之前,如果你没有什么思路的话,建议先做Luogu的合并果子. 好,假设你已经做过了合并 ...

  6. JAVA 优先获取外网Ip,再获取内网Ip

    1.获取内网Ip private String getLocalhostIp(){ String hostAddress = ""; try { InetAddress addre ...

  7. zabbix 密码忘记了如何恢复

    1.进入数据库 2.使用zabbix的数据库 use zabbix: 3.查看表信息 show tables; 4.搜索users表 select * from users; 5.修改userid为1 ...

  8. 数据结构-哈夫曼(Huffman)

    #include <iostream> #include <cstdio> #include <malloc.h> #define LIST_INIT_SIZE 1 ...

  9. 科学计算库Numpy——运算

    np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...

  10. Python 交互模式中 Delete/Backspace 键乱码问题

    进入 Python 交互模式,按下 Delete/Backspace 键,会出现 ^H 字符 解决方式: 1. 进到 Python 的Modules目录 [root@cyt-test Python-2 ...