1 环境资源
虚拟机服务器 | IP |
---|---|
Master | 192.168.51.4 |
Slave1 | 192.168.51.5 |
Slave2 | 192.168.51.6 |
2 配置哨兵
2.1 master
2.1.1 修改配置文件
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ll
总用量 68
-rw-r--r--. 1 root root 61847 2月 8 17:14 6379.conf
drwxr-xr-x. 2 root root 134 9月 24 2017 bin
-rw-r--r--. 1 root root 854 2月 16 14:00 sentinel.conf
drwxr-xr-x. 2 root root 44 2月 15 22:02 working
[root@localhost redis]# vi sentinel.conf
# Base configuration
# 端口号
port 26379
# pid文件位置
pidfile "/usr/local/redis/sentinel/redis-sentinel.pid"
# 工作目录
dir "/usr/local/redis/sentinel"
# 是否后台运行
daemonize yes
# 是否启用安全模式(非外部访问)
protected-mode no
# 日志文件位置
logfile "/usr/local/redis/sentinel/redis-sentinel.log"
# Core configuration
# 配置哨兵
sentinel monitor auskat-master 192.168.51.4 6379 2
# 密码
sentinel auth-pass auskat-master auskat
# master被sentinel认定为失效的间隔时间
sentinel down-after-milliseconds auskat-master 30000
# 剩余的slaves重新和新的master做同步的并行个数
sentinel parallel-syncs auskat-master 1
# 主备切换的超时时间,哨兵要去做故障转移,这个时候哨兵也是一个进程,如果他没有去执行,超过这个时间后,会由其他的哨兵来处理
sentinel failover-timeout auskat-master 180000
2.1.2 创建目录
[root@localhost redis]# mkdir /usr/local/redis/sentinel -p
2.1.3 启动哨兵
[root@localhost redis]# redis-sentinel sentinel.conf
[root@localhost redis]# ps -ef | grep redis
root 16596 1 0 07:38 ? 00:00:42 /usr/local/bin/redis-server 0.0.0.0:6379
root 18725 1 2 14:11 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 18730 18414 0 14:12 pts/1 00:00:00 grep --color=auto redis
2.1.4 拷贝配置文件
如果没有安装scp,执行下命令 yum -y install scp
拷贝到slave1
[root@localhost redis]# scp sentinel.conf root@192.168.51.5:/usr/local/redis
root@192.168.51.6's password:
sentinel.conf
拷贝到slave2
[root@localhost redis]# scp sentinel.conf root@192.168.51.6:/usr/local/redis
root@192.168.51.6's password:
sentinel.conf
2.2 slave1
2.2.1 创建目录
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# mkdir /usr/local/redis/sentinel -p
2.2.2 启动哨兵
[root@localhost redis]# redis-sentinel sentinel.conf
[root@localhost redis]# ps -ef | grep redis
root 16596 1 0 07:38 ? 00:00:42 /usr/local/bin/redis-server 0.0.0.0:6379
root 18725 1 2 14:11 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 18730 18414 0 14:12 pts/1 00:00:00 grep --color=auto redis
2.3 slave2
2.3.1 创建目录
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# mkdir /usr/local/redis/sentinel -p
2.3.2 启动哨兵
[root@localhost redis]# redis-sentinel sentinel.conf
[root@localhost redis]# ps -ef | grep redis
root 16596 1 0 07:38 ? 00:00:42 /usr/local/bin/redis-server 0.0.0.0:6379
root 18725 1 2 14:11 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 18730 18414 0 14:12 pts/1 00:00:00 grep --color=auto redis
3 功能测试
3.1 查看哨兵信息
master机器上连接redis,查看信息
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.51.6,port=6379,state=online,offset=55875,lag=0
slave1:ip=192.168.51.5,port=6379,state=online,offset=55875,lag=0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:55875
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:55875
127.0.0.1:6379>
slave1上连接redis,查看信息
[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:519987
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:519987
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:519987
slave2上连接redis,查看信息
[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:528985
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:528985
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1663
repl_backlog_histlen:527323
3.2 查看日志信息
[root@localhost redis]# tail -f /usr/local/redis/sentinel/redis-sentinel.log
18724:X 16 Feb 2021 14:11:59.828 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18724:X 16 Feb 2021 14:11:59.828 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18724, just started
18724:X 16 Feb 2021 14:11:59.828 # Configuration loaded
18725:X 16 Feb 2021 14:11:59.920 * Increased maximum number of open files to 10032 (it was originally set to 1024).
18725:X 16 Feb 2021 14:11:59.922 * Running mode=sentinel, port=26379.
18725:X 16 Feb 2021 14:11:59.922 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18725:X 16 Feb 2021 14:11:59.932 # Sentinel ID is eb24234aa458e8cc09278fd35e0de1b2d05ec47a
18725:X 16 Feb 2021 14:11:59.932 # +monitor master auskat-master 192.168.51.4 6379 quorum 2
18725:X 16 Feb 2021 14:11:59.948 * +slave slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:11:59.949 * +slave slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:13:54.628 * +sentinel sentinel a22ea4b2dbb61c34f337b533bbb2e04e376bbdaa 192.168.51.5 26379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:14:19.326 * +sentinel sentinel 8f7db63bce26663c1d0c2616024ac66fa9d560c4 192.168.51.6 26379 @ auskat-master 192.168.51.4 6379
3.3 切换master
3.3.1 停止master
[root@localhost ~]# /etc/init.d/redis_init_script stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Waiting for Redis to shutdown ...
Redis stopped
日志信息
18725:X 16 Feb 2021 14:55:55.371 # +sdown master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.443 # +odown master auskat-master 192.168.51.4 6379 #quorum 2/2
18725:X 16 Feb 2021 14:55:55.443 # +new-epoch 1
18725:X 16 Feb 2021 14:55:55.443 # +try-failover master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.444 # +vote-for-leader eb24234aa458e8cc09278fd35e0de1b2d05ec47a 1
18725:X 16 Feb 2021 14:55:55.446 # 8f7db63bce26663c1d0c2616024ac66fa9d560c4 voted for eb24234aa458e8cc09278fd35e0de1b2d05ec47a 1
18725:X 16 Feb 2021 14:55:55.492 # a22ea4b2dbb61c34f337b533bbb2e04e376bbdaa voted for eb24234aa458e8cc09278fd35e0de1b2d05ec47a 1
18725:X 16 Feb 2021 14:55:55.511 # +elected-leader master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.511 # +failover-state-select-slave master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.588 # +selected-slave slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.588 * +failover-state-send-slaveof-noone slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.641 * +failover-state-wait-promotion slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.157 # +promoted-slave slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.157 # +failover-state-reconf-slaves master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.232 * +slave-reconf-sent slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.588 # -odown master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:57.169 * +slave-reconf-inprog slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:58.205 * +slave-reconf-done slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:58.288 # +failover-end master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:58.289 # +switch-master auskat-master 192.168.51.4 6379 192.168.51.5 6379
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:56:28.329 # +sdown slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
slave1信息
[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.51.6,port=6379,state=online,offset=584942,lag=1
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_repl_offset:585230
second_repl_offset:566952
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:585230
slave2 信息
[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.5
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:582336
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_repl_offset:582336
second_repl_offset:566952
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1663
repl_backlog_histlen:580674
3.3.2 启动master
[root@localhost ~]# /etc/init.d/redis_init_script start
Starting Redis server...
18888:C 16 Feb 2021 15:01:50.343 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18888:C 16 Feb 2021 15:01:50.343 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18888, just started
18888:C 16 Feb 2021 15:01:50.343 # Configuration loaded
原来的master
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.5
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1613459043
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:535fba18b992a3e677a777ad678d313a581ee991
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
3.3.3 解决原master恢复不同步问题
master_link_status:down
,同步状态不ok
这是因为我们只设置了5和6的 masterauth ,这是用于同步master的数据,但是4一开始是master是不受影响的,当master转变为slave后,由于他没有auth ,所以他不能从新的master同步数据,随之导致 info replication 的时候,同步状态为 down ,所以只需要修改 redis.conf 中的 masterauth 为 auskat。
一般master数据无法同步给slave的方案检查为如下:
- 网络通信问题,要保证互相ping通,内网互通。
- 关闭防火墙,对应的端口开发(虚拟机中建议永久关闭防火墙,云服务器的话需要保证内网互通)。
- 统一所有的密码,不要漏了某个节点没有设置。
修改mater配置
[root@localhost redis]# vi /usr/local/redis/6379.conf
# masterauth <master-password>
masterauth auskat
[root@localhost redis]# /etc/init.d/redis_init_script stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Waiting for Redis to shutdown ...
Redis stopped
[root@localhost redis]# /etc/init.d/redis_init_script start
Starting Redis server...
18913:C 16 Feb 2021 15:13:32.193 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18913:C 16 Feb 2021 15:13:32.193 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18913, just started
18913:C 16 Feb 2021 15:13:32.193 # Configuration loaded
原master查看
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:56:28.329 # +sdown slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 15:01:51.284 # -sdown slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 15:13:32.481 * +reboot slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.5
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:845873
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:845873
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:792365
repl_backlog_histlen:53509
现在的master查看
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.51.6,port=6379,state=online,offset=808901,lag=0
slave1:ip=192.168.51.4,port=6379,state=online,offset=808757,lag=1
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_repl_offset:808901
second_repl_offset:566952
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:808901
3.3.4 总结
当master宕机之后,由于哨兵监控,剩余的slave会进行选举,选举其中的一个slave晋升为master,当宕机的master恢复之后,会自动变成slave。
3.4 哨兵信息检查
# 查看auskat-master下的master节点信息
sentinel master auskat-master
# 查看auskat-master下的slaves节点信息
sentinel slaves auskat-master
# 查看auskat-master下的哨兵节点信息
sentinel sentinels auskat-master
4 相关信息
- 博文不易,辛苦各位猿友点个关注和赞,感谢
网友评论