Redis-哨兵机制以及灾难演练

3次阅读

共计 3482 个字符,预计需要花费 9 分钟才能阅读完成。

哨兵都采用这个配置即可

1、修改 sentinel.conf 配置文件

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

3、配置监控的服务器配置

  解释:配置监听的主服务器,这里 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 <master-name> <ip> <redis-port> <quorum>
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> 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

  设置成 redis.conf 中配置的密码:

  上一篇文章已经介绍了主从的搭建这里就不介绍了,有需求查看上一篇文章

6、直接启动三个服务器的 redis 服务

7、查看一下 redis 服务启动的日志

  启动成功并且连接到了主机

8、接下来分别启动三个哨兵从主机开始启动

  通过下图可知:主机是 172.16.178.2 从机是 172.16.178.3、172.16.178.4

9、启动三个哨兵:成功启动

10、查看主机的 sentinel.conf 配置可知,该哨兵在监控两个从机和配置的主机,以及另外两个哨兵

11、同理另外两个哨兵也是监控着两个从机和配置的主机,以及另外两个哨兵

12、连接配置好的哨兵

13、输入 info 命令

  看到哨兵正在监控着一个主机两个从机以及三个哨兵

14、故障演练:

 1)、先查询主机的进程 ID

 2)、kill -9 70817

  此时当前节点只有哨兵了

 3)、连接当前哨兵,输入 info

  这是最小的哨兵配置,如果发生了 master-slave 故障转移,或者新的哨兵进程加入哨兵集群,那么哨兵会自动更新自己的配置文件

  此时主机节点变成了 172.16.178.4 了

 4)、连接 172.16.178.4 上的 redis

  当前节点为主机节点,172.16.178.3 为从节点

 5)、重新启动 172.16.178.2 主机上的 redis

 6)、此时再一次连接主机

 7)、主机 set 一个值

 8)、1 号从机 get 主机中设置的 key

 9)、2 号从机 get 主机中设置的 key

帮忙关注一下 微信公众号一起学习:chengxuyuan95(不一样的程序员)

正文完
 0

Redis-哨兵机制以及灾难演练

3次阅读

共计 3444 个字符,预计需要花费 9 分钟才能阅读完成。

哨兵都采用这个配置即可

1、修改 sentinel.conf 配置文件

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

3、配置监控的服务器配置

  解释:配置监听的主服务器,这里 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 <master-name> <ip> <redis-port> <quorum>
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> 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

  设置成 redis.conf 中配置的密码:

  上一篇文章已经介绍了主从的搭建这里就不介绍了,有需求查看上一篇文章

6、直接启动三个服务器的 redis 服务

7、查看一下 redis 服务启动的日志

  启动成功并且连接到了主机

8、接下来分别启动三个哨兵从主机开始启动

  通过下图可知:主机是 172.16.178.2 从机是 172.16.178.3、172.16.178.4

9、启动三个哨兵:成功启动

10、查看主机的 sentinel.conf 配置可知,该哨兵在监控两个从机和配置的主机,以及另外两个哨兵

11、同理另外两个哨兵也是监控着两个从机和配置的主机,以及另外两个哨兵

12、连接配置好的哨兵

13、输入 info 命令

  看到哨兵正在监控着一个主机两个从机以及三个哨兵

14、故障演练:

 1)、先查询主机的进程 ID

 2)、kill -9 70817

  此时当前节点只有哨兵了

 3)、连接当前哨兵,输入 info

  这是最小的哨兵配置,如果发生了 master-slave 故障转移,或者新的哨兵进程加入哨兵集群,那么哨兵会自动更新自己的配置文件

  此时主机节点变成了 172.16.178.4 了

 4)、连接 172.16.178.4 上的 redis

  当前节点为主机节点,172.16.178.3 为从节点

 5)、重新启动 172.16.178.2 主机上的 redis

 6)、此时再一次连接主机

 7)、主机 set 一个值

 8)、1 号从机 get 主机中设置的 key

 9)、2 号从机 get 主机中设置的 key

正文完
 0