Redis 哨兵机制以及灾难演练

#### 哨兵都采用这个配置即可 ##### 1、修改sentinel.conf配置文件
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165743993-90469300.png)
##### 2、禁止保护模式 protected-mode no
  protected-mode参数是为了禁止外网访问redis,如果启用了,则只能够通过lookback ip(127.0.0.1)访问Redis,如果外网访问redis,会报出异常
  注意: 如果redis实例配置文件中禁用了bind参数,并将protected-mode设置为no后,外网访问redis依然报上述错误,因为 sentinel 实例的配置文件中需要增加参数 protected-mode no
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165744318-1684197053.png)
##### 3、配置监控的服务器配置
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165744671-968596366.png)
######   解释:配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,172.16.178.2代表监控的主服务器,6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作
quorum的解释如下:
 (1)至少多少个哨兵要一致同意,master进程挂掉了,或者slave进程挂掉了,或者要启动一个故障转移操作
 (2)quorum是用来识别故障的,真正执行故障转移的时候,还是要在哨兵集群执行选举,选举一个哨兵进程出来执行故障转移操作
 (3)假设有5个哨兵,quorum设置了2,那么如果5个哨兵中的2个都认为master挂掉了; 2个哨兵中的一个就会做一个选举,选举一个哨兵出来,执行故障转移; 如果5个哨兵中有3个哨兵都是运行的,那么故障转移就会被允许执行 ``` # 语法:sentinel monitor # Tells Sentinel to monitor this master, and to consider it in O_DOWN # (Objectively Down) state only if at least sentinels agree. # 告诉Sentinel监视这个master,并且考虑它O_DOWN # (仅在客观上O_DOWN) 状态 只有在至少 quorum 这个数量的哨兵同意才可以。 # Note that whatever is the O_DOWN quorum, a Sentinel will require to # be elected by the majority of the known Sentinels in order to # start a failover, so no failover can be performed in minority. # 请注意,无论quorum数是多少,哨兵都需要这样做由大多数已知的哨兵 # 选出来启动故障转移,因此不能在少数情况下执行故障转移。 # Replicas are auto-discovered, so you don't need to specify replicas in # any way. Sentinel itself will rewrite this configuration file adding # the replicas using additional configuration options. # 从机是自动发现的,因此不需要在任何方式指定。哨兵自己将重写配置文件添加 # 到从机使用的配置文件的配置选项上。 # Also note that the configuration file is rewritten when a # replica is promoted to master. # 注意当从机被指定主机的时候将重写配置文件 # Note: master name should not include special characters or spaces. # The valid charset is A-z 0-9 and the three characters ".-_". # 注意:主机名称不应该包含特殊字符或空格。有效的字符集是A-z 0-9和三个字符“.-_”。 ``` ##### 4、其他的配置
  (1)、down-after-milliseconds,超过多少毫秒跟一个redis实例断了连接,哨兵就可能认为这个redis实例挂了
  (2)、parallel-syncs,新的master别切换之后,同时有多少个slave被切换到去连接新master,重新做同步,数字越低,花费的时间越多 假设你的redis是1个master,4个slave,然后master宕机了,4个slave中有1个切换成了master,剩下3个slave就要挂到新的master上面去这个时候,如果parallel-syncs是1,那么3个slave,一个一个地挂接到新的master上面去,1个挂接完,而且从新的master sync完数据之后,再挂接下一个,如果parallel-syncs是3,那么一次性就会把所有slave挂接到新的master上去
  (3)、failover-timeout,执行故障转移的timeout超时时长
##### 5、设置密码权限
``` # Set the password to use to authenticate with the master and replicas. # Useful if there is a password set in the Redis instances to monitor. # 设置用于主服务器和从机进行身份验证的密码。 # 如果要监视的Redis实例中设置了密码,则非常有用 # Note that the master password is also used for replicas, so it is not # possible to set a different password in masters and replicas instances # if you want to be able to monitor these instances with Sentinel. # 注意,主密码也用于从机,因此它不可能在主机和从机实例中设置不同的密码 # 如果你想用哨兵来监控这些实例。 # However you can have Redis instances without the authentication enabled # mixed with Redis instances requiring the authentication (as long as the # password set is the same for all the instances requiring the password) as # the AUTH command will have no effect in Redis instances with authentication # switched off. # 而且您可以在不启用身份验证的情况下使用Redis实例与需要身份验证的Redis实例混合(只要 # 对于需要密码的所有实例,密码集都是相同的在使用身份验证的Redis实例中),AUTH命令将不起作用。 # 语法:sentinel auth-pass mymaster MySUPER--secret-0123passw0rd ``` ![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165744918-1182879529.png)
  设置成redis.conf中配置的密码:
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165745278-1090427477.png)
  上一篇文章已经介绍了主从的搭建这里就不介绍了,有需求查看上一篇文章
##### 6、直接启动三个服务器的redis服务
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165747529-1922226641.png)
##### 7、查看一下redis服务启动的日志
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165747859-91539084.png)
  启动成功并且连接到了主机
##### 8、接下来分别启动三个哨兵从主机开始启动
  通过下图可知:主机是172.16.178.2 从机是 172.16.178.3 、172.16.178.4
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165748386-1665931188.png)
##### 9、启动三个哨兵:成功启动
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165749043-490898213.png)
##### 10、查看主机的sentinel.conf 配置可知,该哨兵在监控两个从机和配置的主机,以及另外两个哨兵
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165749564-655264993.png)
##### 11、同理另外两个哨兵也是监控着两个从机和配置的主机,以及另外两个哨兵
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165750067-211023336.png)
##### 12、连接配置好的哨兵
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165750323-756653120.png)
##### 13、输入 info 命令
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165750680-1123341330.png)
  看到哨兵正在监控着一个主机两个从机以及三个哨兵
##### 14、故障演练:
 1)、先查询主机的进程ID
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165750880-1495826734.png)
 2)、kill -9 70817
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165751396-773562519.png)
  此时当前节点只有哨兵了
 3)、连接当前哨兵,输入info
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165751910-548701323.png)
  这是最小的哨兵配置,如果发生了master-slave故障转移,或者新的哨兵进程加入哨兵集群,那么哨兵会自动更新自己的配置文件
  此时主机节点变成了 172.16.178.4 了
 4)、连接172.16.178.4 上的redis
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165752514-1813256018.png)
  当前节点为主机节点,172.16.178.3 为从节点
 5)、重新启动172.16.178.2主机上的redis
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165752761-1733842442.png)
 6)、此时再一次连接主机
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165753141-1995935168.png)
 7)、主机set 一个值
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165753519-1125501993.png)
 8)、1号从机get 主机中设置的key
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165753699-2051881829.png)
 9)、2号从机get 主机中设置的key
![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165753861-1988085137.png)
帮忙关注一下 微信公众号一起学习 :chengxuyuan95(不一样的程序员)![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191022165754169-745034528.jpg)
posted @ 2019-10-22 16:58  雪中孤狼  阅读(295)  评论(0编辑  收藏  举报