redis 哨兵模式配置

redis 哨兵模式配置

jimlu JustGotoStudy 2023-08-02 08:59 发表于甘肃

1 前提:

  • 先安装好redis  (见redis安装)
  • 配置好一主两从  (见redis一主两从配置)
  • 哨兵模式的好处:当master挂了后,哨兵会随机指定一个slave做master

2 创建哨兵目录和配置文件

[root@192 redis]# mkdir sentinel-26379
[root@192 redis]# mkdir sentinel-26380
[root@192 redis]# mkdir sentinel-26381
[root@192 redis]# cp redis.conf sentinel-26379/sentinel-26379.conf
[root@192 redis]# cp redis.conf sentinel-26380/sentinel-26380.conf
[root@192 redis]# cp redis.conf sentinel-26381/sentinel-26381.conf
[root@192 redis]# ll sentinel-263*
sentinel-26379:
total 108
-rw-r--r-- 1 root root 106563 Jul 30 22:09 sentinel-26379.conf

sentinel-26380:
total 108
-rw-r--r-- 1 root root 106563 Jul 30 22:09 sentinel-26380.conf

sentinel-26381:
total 108
-rw-r--r-- 1 root root 106563 Jul 30 22:10 sentinel-26381.conf

3 修改哨兵配置文件

[root@192 redis]# vim sentinel-26379/sentinel-26379.conf 
[root@192 redis]# cat sentinel-26379/sentinel-26379.conf |grep -E "^dir|^port|^pidfile|^requirepass|^replicapass|^masterauth|^logfile|^sentinel"
port 26379
pidfile /var/run/redis_26379.pid
logfile ./sentinel-26379.log
#让哨兵监控master的IP地址、端口,但有2个哨兵发现master异常时就切换,如果集群中有多个IP哨兵可以适当的增大这个值
sentinel monitor jin 192.168.45.132 6379 2
#下面配置的123456是哨兵的密码
sentinel auth-pass jin 123456
dir ./sentinel-26379
#下面时主从连接的密码
masterauth 123456
#下面时客户端登陆的密码
requirepass 123456

[root@192 redis]# vim sentinel-26380/sentinel-26380.conf 
[root@192 redis]# cat sentinel-26380/sentinel-26380.conf |grep -E "^dir|^port|^pidfile|^requirepass|^replicapass|^masterauth|^logfile|^sentinel"
port 26380
pidfile /var/run/redis_26380.pid
logfile ./sentinel-26380
sentinel monitor jin 192.168.45.132 6379 2
sentinel auth-pass jin 123456
dir ./sentinel-26380
masterauth 123456
requirepass 123456

[root@192 redis]# vim sentinel-26381/sentinel-26381.conf 
[root@192 redis]# cat sentinel-26381/sentinel-26381.conf |grep -E "^dir|^port|^pidfile|^requirepass|^replicapass|^masterauth|^logfile|^sentinel"
port 26381
pidfile /var/run/redis_26381.pid
logfile ./sentinel-26381.log
sentinel monitor jin 192.168.45.132 6379 2
sentinel auth-pass jin 123456
dir ./sentinel-26381
masterauth 123456
requirepass 123456

4 启动redis

4.1 启动redis服务

[root@192 redis]# redis-server master/redis-master-6379.conf 
[root@192 redis]# redis-server slave1/redis-slave1-6380.conf 
[root@192 redis]# redis-server slave2/redis-slave2-6381.conf 
[root@192 redis]# ps -ef |grep redis
root      29353      1  0 22:45 ?        00:00:00 redis-server 192.168.45.132:6379
root      29365      1  0 22:45 ?        00:00:00 redis-server 192.168.45.132:6380
root      29376      1  0 22:45 ?        00:00:00 redis-server 192.168.45.132:6381
root      29383  27571  0 22:45 pts/2    00:00:00 grep --color=auto redis

4.2 启动哨兵

[root@192 redis]# redis-sentinel sentinel-26379/sentinel-26379.conf 
[root@192 redis]# redis-sentinel sentinel-26380/sentinel-26380.conf 
[root@192 redis]# redis-sentinel sentinel-26381/sentinel-26381.conf 
[root@192 redis]# ps -ef |grep redis                                
root      29353      1  0 22:45 ?        00:00:00 redis-server 192.168.45.132:6379
root      29365      1  0 22:45 ?        00:00:00 redis-server 192.168.45.132:6380
root      29376      1  0 22:45 ?        00:00:00 redis-server 192.168.45.132:6381
root      29401      1  0 22:46 ?        00:00:00 redis-sentinel 192.168.45.132:26379 [sentinel]
root      29415      1  1 22:46 ?        00:00:00 redis-sentinel 192.168.45.132:26380 [sentinel]
root      29427      1  5 22:46 ?        00:00:00 redis-sentinel 192.168.45.132:26381 [sentinel]
root      29433  27571  0 22:46 pts/2    00:00:00 grep --color=auto redis

4.3 查看主从状态

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6379 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.45.132,port=6380,state=online,offset=23997,lag=1
slave1:ip=192.168.45.132,port=6381,state=online,offset=23997,lag=1
master_failover_state:no-failover
master_replid:c3b0280b560d0e9d207e465aab61b1c9ea62cc91
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:24273
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:24273
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6380 info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.45.132
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:31077
slave_repl_offset:31077
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c3b0280b560d0e9d207e465aab61b1c9ea62cc91
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:31077
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:31077
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6381 info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.45.132
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:36225
slave_repl_offset:36225
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c3b0280b560d0e9d207e465aab61b1c9ea62cc91
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:36225
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:36211

4.4 查看哨兵状态

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 26379 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 26379 info sentinel
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=jin,status=ok,address=192.168.45.132:6379,slaves=2,sentinels=3
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 26380 info sentinel  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=jin,status=ok,address=192.168.45.132:6379,slaves=2,sentinels=3
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 26381 info sentinel 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=jin,status=ok,address=192.168.45.132:6379,slaves=2,sentinels=3

4.5 哨兵不能查看到数据但GUI工具可以

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.45.132:26379> get name
(error) ERR unknown command 'get', with args beginning with: 'name' 
图片图片

5 测试master自动故障转移

5.1 杀死进程号为6379的master进程

[root@192 redis]# ps -ef |grep redis
root      29353      1  0 22:45 ?        00:00:04 redis-server 192.168.45.132:6379
root      29365      1  0 22:45 ?        00:00:04 redis-server 192.168.45.132:6380
root      29376      1  0 22:45 ?        00:00:04 redis-server 192.168.45.132:6381
root      29401      1  0 22:46 ?        00:00:07 redis-sentinel 192.168.45.132:26379 [sentinel]
root      29415      1  0 22:46 ?        00:00:07 redis-sentinel 192.168.45.132:26380 [sentinel]
root      29427      1  0 22:46 ?        00:00:07 redis-sentinel 192.168.45.132:26381 [sentinel]
root      29909  27571  0 23:24 pts/2    00:00:00 grep --color=auto redis
[root@192 redis]# kill -9 29353
[root@192 redis]# ps -ef |grep redis
root      29365      1  0 22:45 ?        00:00:04 redis-server 192.168.45.132:6380
root      29376      1  0 22:45 ?        00:00:04 redis-server 192.168.45.132:6381
root      29401      1  0 22:46 ?        00:00:07 redis-sentinel 192.168.45.132:26379 [sentinel]
root      29415      1  0 22:46 ?        00:00:07 redis-sentinel 192.168.45.132:26380 [sentinel]
root      29427      1  0 22:46 ?        00:00:07 redis-sentinel 192.168.45.132:26381 [sentinel]
root      29911  27571  0 23:24 pts/2    00:00:00 grep --color=auto redis

5.2 查看主从状态

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6380 info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.45.132
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:489778
slave_repl_offset:489778
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:24b1b5d8ccabaee3150484cc97a3dd5e32185b0c
master_replid2:c3b0280b560d0e9d207e465aab61b1c9ea62cc91
master_repl_offset:489778
second_repl_offset:467398
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:489778

可以看到主从状态切换到了6381端口上

在6381端口上测试redis读写,同样在6380上也可以看到

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6381 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.45.132:6381> get name
(nil)
192.168.45.132:6381> get age
(nil)
192.168.45.132:6381> set name jin
OK
192.168.45.132:6381> get name
"jin"
192.168.45.132:6381> set age 18
OK
192.168.45.132:6381> get name
"jin"
192.168.45.132:6381> exit
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6380 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.45.132:6380> get name
"jin"
192.168.45.132:6380> get age
"18"

5.3 查看哨兵状态

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 26379 info sentinel
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=jin,status=ok,address=192.168.45.132:6381,slaves=2,sentinels=3

5.4 恢复6379端口

[root@192 redis]# redis-server master/redis-master-6379.conf 
[root@192 redis]# ps -ef |grep redis
root      29365      1  0 22:45 ?        00:00:05 redis-server 192.168.45.132:6380
root      29376      1  0 22:45 ?        00:00:05 redis-server 192.168.45.132:6381
root      29401      1  0 22:46 ?        00:00:09 redis-sentinel 192.168.45.132:26379 [sentinel]
root      29415      1  0 22:46 ?        00:00:09 redis-sentinel 192.168.45.132:26380 [sentinel]
root      29427      1  0 22:46 ?        00:00:09 redis-sentinel 192.168.45.132:26381 [sentinel]
root      30056      1  1 23:35 ?        00:00:00 redis-server 192.168.45.132:6379
root      30064  27571  0 23:35 pts/2    00:00:00 grep --color=auto redis

5.5 观察redis主节点在那个端口上

如下可以看到,即使之前的master节点6379恢复,它依然变不成主节点,此时被加入到了从节点

[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6381 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.45.132,port=6380,state=online,offset=603241,lag=0
slave1:ip=192.168.45.132,port=6379,state=online,offset=603241,lag=0
master_failover_state:no-failover
master_replid:24b1b5d8ccabaee3150484cc97a3dd5e32185b0c
master_replid2:c3b0280b560d0e9d207e465aab61b1c9ea62cc91
master_repl_offset:603255
second_repl_offset:467398
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:603241
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6380 info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.45.132
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:604635
slave_repl_offset:604635
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:24b1b5d8ccabaee3150484cc97a3dd5e32185b0c
master_replid2:c3b0280b560d0e9d207e465aab61b1c9ea62cc91
master_repl_offset:604635
second_repl_offset:467398
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:604635
[root@192 redis]# redis-cli -h 192.168.45.132 -a 123456 -p 6379 info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.45.132
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:605753
slave_repl_offset:605753
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:24b1b5d8ccabaee3150484cc97a3dd5e32185b0c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:605753
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:595311
repl_backlog_histlen:10443
收录于合集 #redis
 3
上一篇redis 一主两从配置
 
阅读 121
文章已于2023-08-02修改
JustGotoStudy
 
 
写下你的留言
 
 
 
 
posted @ 2023-09-12 14:40  往事已成昨天  阅读(679)  评论(0编辑  收藏  举报