哨兵用于监控整个集群的状态,如果有节点宕机,执行故障转移,将出现问题的服务器一处当前集群,出现哨兵是因为一旦主节点宕机整个集群崩溃,需要通过人工干预的方式实现切换主服务器的操作,很不智能,哨兵可以保住程序员自动在节点上输出slaveof no one ,避免长时间没有故障转移的问题,在redis2.8版本之后开始使用,用于保证数据接地那的高可用性
概念 | 逻辑结构 | 物理结构 |
---|---|---|
主节点 | Redis主服务器 | 一个独立的redis进程 |
从节点 | redis从服务器 | 一个独立的redis进程 |
数据节点 | redis主从服务的集合 | 主从节点进程集合 |
哨兵节点 | 监控redis数据节点 | 一个独立的哨兵节点 |
哨兵节点集合 | 多个redis哨兵的集合 | 若干个哨兵节点集合 |
哨兵部署数量最少有三个且以奇数形式建立
一主两从实现高可用哨兵
1) 主节点发生故障从节点复制失败
2) 如果从节点无法正常启动需要选出,一个从节点进行执行slaveofs no one 成为新的主节点进行对外提供服务器
3) 原来的从接地那成为主节点之后的更新信息
4) 另外一个从节点变成晋升为当前主节点的丛节点这个过程全为哨兵自动执行,不需要人工干预
bind 0.0.0.0
# 更改监控地址
sentinel monitor mymaster 127.0.0.1 6379 2
# 设置哨兵监控的节点信息(只填写主节点信息), mymaster 表示监控节点的名字, ip 端口 2表示哨兵中只要有两个哨兵判定该节点宕机执行故障转移(数量设置为,半数哨兵为宜)
# sentinel auth-pass
# 用于设置哨兵所使用redis的密码
sentinel down-after-milliseconds mymaster 30000
# 哨兵监控的节点30000毫秒内没有对反映自动执行故障移动
sentinel parallel-syncs mymaster 1
# 执行故障转移时全量复制的人数
sentinel failover-timeout mymaster 180000
# 故障转移的超时时间 单位 : 毫秒
sentinel deny-scripts-reconfig yes
设置故障转移的优先级(从服务器中 redis.conf配置文件)
slave-priority 100 # 数值越小优先级越高
1) 多个哨兵可以有效的防止误判发生
2) 多个哨兵的部署可以保证整个集群的健壮
1) redis-sentinel 哨兵配置文件
2) redis-server --sentinel 哨兵配置文件
redis-cli -h ip -p port
info sentinel
监控的一台哨兵判断监控的节点宕机
+sdown 有主观宕机
-sdown 服务器 重新上线
监控的半数哨兵判定该节点是否宕机
+odown 有客观宕机
-odown 服务器重新上线
使用redis搭建一个带有三个哨兵,redis一主两从服务的架构,且当主服务器宕机时,从服务器可以顶替主服务器继续提供服务
要求:redis主服务器为6379端口,两台从服务器为6380和6381端口,哨兵端口为26379,26380,26381
[root@localhost ~]# tar -zxf redis-4.0.11.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/redis-4.0.11/
[root@localhost redis-4.0.11]# make && make install
[root@localhost redis-4.0.11]# cp redis.conf /root/redis.conf
[root@localhost redis-4.0.11]# cp redis.conf /root/redis6380.conf
[root@localhost redis-4.0.11]# cp redis.conf /root/redis6381.conf
[root@localhost redis-4.0.11]# cd
[root@localhost ~]# vim redis.conf
bind 0.0.0.0
[root@localhost ~]# vim redis6380.conf
bind 0.0.0.0
port 6380
[root@localhost ~]# vim redis6381.conf
bind 0.0.0.0
port 6381
[root@localhost ~]# redis-server redis.conf
并解决warning,再次启动
[root@localhost ~]# echo 511 > /proc/sys/net/core/somaxconn
[root@localhost ~]# echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
vm.overcommit_memory = 1
[root@localhost ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@localhost ~]# netstat -anpt | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5925/redis-server 1
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 5977/redis-server 0
tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 5981/redis-server 0
[root@localhost ~]# redis-cli
127.0.0.1:6379> ping
PONG
[root@localhost ~]# redis-cli -p 6380
127.0.0.1:6380> ping
PONG
[root@localhost ~]# redis-cli -p 6381
127.0.0.1:6381> ping
PONG
[root@localhost ~]# vim /root/redis6380.conf
slaveof 1.1.1.105 6379
[root@localhost ~]# vim /root/redis63
slaveof 1.1.1.105 6379
[root@localhost ~]# cp /usr/src/redis-4.0.11/sentinel.conf /
[root@localhost ~]# cp /usr/src/redis-4.0.11/sentinel.conf /root/sentinel26379.conf
[root@localhost ~]# cp /usr/src/redis-4.0.11/sentinel.conf /root/sentinel26380.conf
[root@localhost ~]# cp /usr/src/redis-4.0.11/sentinel.conf /root/sentinel26381.conf
[root@localhost ~]# ls /root/ | grep sentinel
sentinel26379.conf
sentinel26380.conf
sentinel26381.conf
[root@localhost ~]# vim /root/sentinel26379.conf
bind 0.0.0.0
port 26379
sentinel monitor mymaster 1.1.1.105 6380 2
sentinel down-after-milliseconds mymaster 10000
[root@localhost ~]# vim /root/sentinel26380.conf
bind 0.0.0.0
port 26380
sentinel monitor mymaster 1.1.1.105 6380 2
sentinel down-after-milliseconds mymaster 10000
[root@localhost ~]# vim /root/sentinel26381conf
bind 0.0.0.0
port 26381
sentinel monitor mymaster 1.1.1.105 6380 2
sentinel down-after-milliseconds mymaster 10000
[root@localhost ~]# redis-sentinel sentinel26379.conf
[root@localhost ~]# redis-sentinel sentinel26380.conf
[root@localhost ~]# redis-sentinel sentinel26381.conf