十八、Redis进阶-实战Redis哨兵模式配置的实现

作者: 温新

分类: 【Redis】

阅读: 3366

时间: 2020-09-05 16:28:28

实现的实现是基于主从复制的,因此要实现哨兵就先实现主从复制。关于主从复制已有记录文章,本篇文章实现哨兵是基于十六、Redis进阶-Redis主从复制一主多从一步一步实现,因此还没有实现主从复制,就先赶紧对着主从复制的文章来实现吧。

哨兵建议配置为奇数。

步骤一:配置并开启主从复制

此步骤省略,详情请参考十六、Redis进阶-Redis主从复制一主多从一步一步实现。

步骤二:使用配置文件配置哨兵

1)准备工作

# 切换到redis解压好的redis目录
[root@192 redis_sentinel_conf]# cd /usr/local/src/redis-6.0.6

# 创建配置文件哨兵目录
[root@192 redis-6.0.6]# mkdir /usr/local/bin/redis_sentinel_conf
# 复制默认的哨兵文件到目录
[root@192 redis-6.0.6]# cp ./sentinel.conf /usr/local/bin/redis_sentinel_conf/sentinel_6379.conf
[root@192 redis-6.0.6]# cd /usr/local/bin/redis_sentinel_conf/

2)编辑26379哨兵配置文件

[root@192 redis_sentinel_conf]# pwd
/usr/local/bin/redis_sentinel_conf

[root@192 redis_sentinel_conf]# vim sentinel_6379.conf

26379配置文件

# 端口
port 26379

# 是否守护进程启动
daemonize no

# 工作目录
dir /tmp

# 声明该哨兵的主库是mymaster,主库的ip和端口分别是127.0.0.1 和 6379
# 最后一个2的含义是:当哨兵发生领导选举时,需要2个哨兵通过才行
sentinel monitor mymaster 127.0.0.1 6379 2

# 在mymaster宕机30秒后进行主观下线
sentinel down-after-milliseconds mymaster 30000

# 指定在发生failover故障转移时最多可以有1个slave同时对新的master进行同步
sentinel parallel-syncs mymaster 1

# 设置故障转移超时时间为180秒
sentinel failover-timeout mymaster 180000

3)复制entinel_6379.conf配置文件为 entinel_6380.conf、entinel_6381.conf并修改对应的端口为26380、26381

[root@192 redis_sentinel_conf]# cp sentinel_6379.conf ./sentinel_6380.conf  
[root@192 redis_sentinel_conf]# cp sentinel_6379.conf ./sentinel_6381.conf 

步骤三:启动配置好的三个哨兵服务

1)启动26379服务并连接

[root@192 redis_sentinel_conf]# cd /usr/local/bin/

# 启动26379哨兵
[root@192 bin]# ./redis-sentinel ./redis_sentinel_conf/sentinel_6379.conf 
1560:X 06 Sep 2020 07:55:58.872 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1560:X 06 Sep 2020 07:55:58.872 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1560, just started
1560:X 06 Sep 2020 07:55:58.872 # Configuration loaded
1560:X 06 Sep 2020 07:55:58.873 * Increased maximum number of open files to 10032 (it was originally set to 1024).

  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 1560

1560:X 06 Sep 2020 07:55:58.874 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1560:X 06 Sep 2020 07:55:58.876 # Sentinel ID is 3b2ccac65c3bc10c2899bea807a6920f9edeec09
1560:X 06 Sep 2020 07:55:58.876 # +monitor master mymaster 127.0.0.1 6379 quorum 2
1560:X 06 Sep 2020 07:55:58.876 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:55:58.877 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379

客户端使用26379端口连接哨兵服务器

[root@192 ~]# cd /usr/local/bin/

[root@192 bin]# ./redis-cli -p 26379
127.0.0.1:26379> 

127.0.0.1:26379> info
#.......
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# 此处省略了一些信息
# .......

# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=1

查看26379配置文件

......

sentinel config-epoch mymaster 0

# Generated by CONFIG REWRITE
protected-mode no
user default on nopass ~* +@all
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6380
sentinel current-epoch 0

可以看到配置文件已经发生改变

2)启动26380哨兵

[root@192 bin]# ./redis-sentinel ./redis_sentinel_conf/sentinel_6380.conf 
1630:X 06 Sep 2020 08:04:37.209 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1630:X 06 Sep 2020 08:04:37.209 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1630, just started
1630:X 06 Sep 2020 08:04:37.209 # Configuration loaded
1630:X 06 Sep 2020 08:04:37.209 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                             
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26380
 |    `-._   `._    /     _.-'    |     PID: 1630
                                                             

1630:X 06 Sep 2020 08:04:37.210 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1630:X 06 Sep 2020 08:04:37.211 # Sentinel ID is a00ee57a0d039a3cb403ff2531f85d466fba37e3
1630:X 06 Sep 2020 08:04:37.211 # +monitor master mymaster 127.0.0.1 6379 quorum 2
1630:X 06 Sep 2020 08:04:37.212 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
1630:X 06 Sep 2020 08:04:37.213 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1630:X 06 Sep 2020 08:04:37.494 * +sentinel sentinel 3b2ccac65c3bc10c2899bea807a6920f9edeec09 127.0.0.1 26379 @ mymaster 127.0.0.1 6379

查看26379服务,已经多了2个配置

1560:X 06 Sep 2020 07:55:58.876 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:55:58.877 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:58:59.633 * +fix-slave-config slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:58:59.633 * +fix-slave-config slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379

3)启动26381哨兵服务

[root@192 bin]# ./redis-sentinel ./redis_sentinel_conf/sentinel_6381.conf 
1669:X 06 Sep 2020 08:07:50.657 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1669:X 06 Sep 2020 08:07:50.657 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1669, just started
1669:X 06 Sep 2020 08:07:50.657 # Configuration loaded
1669:X 06 Sep 2020 08:07:50.657 * Increased maximum number of open files to 10032 (it was originally set to 1024).

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26381
 |    `-._   `._    /     _.-'    |     PID: 1669
                                   

1669:X 06 Sep 2020 08:07:50.658 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1669:X 06 Sep 2020 08:07:50.661 # Sentinel ID is 84c2f53877a8f3ca3687f80495701ea0b2ae4f58
1669:X 06 Sep 2020 08:07:50.661 # +monitor master mymaster 127.0.0.1 6379 quorum 2
1669:X 06 Sep 2020 08:07:50.662 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
1669:X 06 Sep 2020 08:07:50.663 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1669:X 06 Sep 2020 08:07:50.929 * +sentinel sentinel a00ee57a0d039a3cb403ff2531f85d466fba37e3 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
1669:X 06 Sep 2020 08:07:51.373 * +sentinel sentinel 3b2ccac65c3bc10c2899bea807a6920f9edeec09 127.0.0.1 26379 @ mymaster 127.0.0.1 6379

步骤四:实战测试

1)6379主服务器中写入一条数据

127.0.0.1:6379> set ziruchu hello
OK

2)6380、6381从服务器中读取

127.0.0.1:6380> get ziruchu
"hello"
127.0.0.1:6381> get ziruchu
"hello"

3)宕机6379主服务器

由于6379主服务并没有使用守护进程启动,因此直接使用Ctrl+c断开服务

4)查看随意一个哨兵

1560:X 06 Sep 2020 07:55:58.874 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1560:X 06 Sep 2020 07:55:58.876 # Sentinel ID is 3b2ccac65c3bc10c2899bea807a6920f9edeec09
1560:X 06 Sep 2020 07:55:58.876 # +monitor master mymaster 127.0.0.1 6379 quorum 2
1560:X 06 Sep 2020 07:55:58.876 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:55:58.877 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:58:59.633 * +fix-slave-config slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 07:58:59.633 * +fix-slave-config slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 08:04:39.251 * +sentinel sentinel a00ee57a0d039a3cb403ff2531f85d466fba37e3 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 08:07:52.710 * +sentinel sentinel 84c2f53877a8f3ca3687f80495701ea0b2ae4f58 127.0.0.1 26381 @ mymaster 127.0.0.1 6379
## 6379服务器断线
1560:X 06 Sep 2020 08:15:31.852 # +sdown master mymaster 127.0.0.1 6379
1560:X 06 Sep 2020 08:15:31.949 # +new-epoch 1
1560:X 06 Sep 2020 08:15:31.953 # +vote-for-leader a00ee57a0d039a3cb403ff2531f85d466fba37e3 1
1560:X 06 Sep 2020 08:15:32.837 # +config-update-from sentinel a00ee57a0d039a3cb403ff2531f85d466fba37e3 127.0.0.1 26380 @ mymaster 127.0.0.1 6379

## 推选出6381作为新的主服务器
1560:X 06 Sep 2020 08:15:32.837 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381
# 把6380、6379连接到6381主服务器
1560:X 06 Sep 2020 08:15:32.837 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
1560:X 06 Sep 2020 08:15:32.837 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
# 6379没有上线,因此重新断开6379
1560:X 06 Sep 2020 08:16:02.854 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

5)6381服务器中设置一个值,然后从服务器查看

# 主
127.0.0.1:6381> set name6381 ziruchu
OK
127.0.0.1:6380> get name6381
"ziruchu"

6)重新启动6379服务并使用客户端连接

127.0.0.1:6379> get name6381
"ziruchu"

到这里,哨兵配置完成,下一篇将对哨兵原理进行记录。

请登录后再评论