美文网首页
【Redis】—05、Redis的哨兵模式sentinel部署—

【Redis】—05、Redis的哨兵模式sentinel部署—

作者: 云之图 | 来源:发表于2022-12-26 12:49 被阅读0次

Redis哨兵模式的部署依赖redis的主从模式。先部署好redis的主从模式(1主2从)。
部署方式:【Redis】—04、Redis的主从复制。

1、环境说明

序号 hostname IP地址 端口
1 db01 172.21.209.40 redis端口:6379 哨兵端口:26379
2 db02 172.21.209.41 redis端口:6379 哨兵端口:26379
3 db03 172.21.209.42 redis端口:6379 哨兵端口:26379

2、部署过程

2.1、创建目录

创建配置文件和数据目录并进入目录
mkdir /data/db/redis/26379  -p
cd /data/db/redis/26379

2.2、准备配置文件

配置文件:三个哨兵的配置文件都是一样的,如果采用多实例部署,需要修改对应的端口即可。
cat >> /data/db/redis/26379/sentinel.conf <<EOF
port 26379
dir "/data/db/redis/26379/"
daemonize yes
logfile "sentinel.log"

sentinel monitor mymaster 172.21.209.40 6379 1

#1:表示有一个哨兵认为主库宕机,则表示宕机,这是主观下线。一般集群有3个哨兵,需要两台认为主库宕机才能说明主库宕机,因此一般设置为2. 
#sentinel monitor mymaster 172.21.209.40 6379 2

sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster 123 

EOF

2.3、启动sentinel

redis-sentinel /data/db/redis/26379/sentinel.conf  &>/data/db/redis/26379/sentinel.log &

查看进程和端口

root@db01:/data/db/redis/26379# ps -ef|grep redis
root        7515       1  0 Dec15 ?        00:23:25 redis-server 172.21.209.40:6379
root       21555       1  0 12:27 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root       21564   21521  0 12:28 pts/0    00:00:00 grep --color=auto redis
root@db01:/data/db/redis/26379# netstat  -lntp|grep 6379
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      21555/redis-sentine 
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      7515/redis-server 1 
tcp        0      0 172.21.209.40:6379      0.0.0.0:*               LISTEN      7515/redis-server 1 
tcp6       0      0 :::26379                :::*                    LISTEN      21555/redis-sentine 
root@db01:/data/db/redis/26379# 

2.4、查看哨兵模式的状态

root@db01:/data/db/redis/26379# redis-cli -h 172.21.209.40 -p 26379 info Sentinel
# 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=172.21.209.40:6379,slaves=2,sentinels=3

3、Redis的高可用环境测试

3.1、停主库测试

1、停止主库:
root@db01:/data/db/redis/26379# redis-cli -a 123 -p 6379 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
root@db01:/data/db/redis/26379# 

2、检查进程和端口
root@db01:/data/db/redis/26379# ps -ef|grep redis 
root       21555       1  0 12:27 ?        00:00:01 redis-sentinel *:26379 [sentinel]
root       21640   21521  0 12:38 pts/0    00:00:00 grep --color=auto redis
root@db01:/data/db/redis/26379# 
root@db01:/data/db/redis/26379# netstat  -lntp|grep redis 
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      21555/redis-sentine 
tcp6       0      0 :::26379                :::*                    LISTEN      21555/redis-sentine 
root@db01:/data/db/redis/26379# 

3、查看哨兵模式的状态信息
root@db01:/data/db/redis/26379# redis-cli -h 172.21.209.40 -p 26379 info Sentinel
# 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=172.21.209.41:6379,slaves=2,sentinels=3
root@db01:/data/db/redis/26379# 

3.2、启用主库测试

1、启用主库
redis-server  /data/db/redis/6379/redis.conf

2、查看进程和端口
root@db01:/data/db/redis/26379# ps -ef|grep redis 
root       21555       1  0 12:27 ?        00:00:02 redis-sentinel *:26379 [sentinel]
root       21647       1  0 12:43 ?        00:00:00 redis-server 172.21.209.40:6379
root       21658   21521  0 12:44 pts/0    00:00:00 grep --color=auto redis
root@db01:/data/db/redis/26379# 
root@db01:/data/db/redis/26379# netstat  -lntp|grep redis 
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      21647/redis-server  
tcp        0      0 172.21.209.40:6379      0.0.0.0:*               LISTEN      21647/redis-server  
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      21555/redis-sentine 
tcp6       0      0 :::26379                :::*                    LISTEN      21555/redis-sentine 
root@db01:/data/db/redis/26379# 


3、查看redis的主从环境
此时,主从关系已经发生了变化。
root@db01:/data/db/redis/26379#  redis-cli -p 6379 -a 123 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:172.21.209.41
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:1596078
slave_repl_offset:1596078
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:42225a2458b036f2ac4354d2a19ce505765250f2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1596078
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1583705
repl_backlog_histlen:12374
root@db01:/data/db/redis/26379# 

4、查看哨兵的状态
root@db01:/data/db/redis/26379# redis-cli -h 172.21.209.40 -p 26379 info Sentinel
# 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=172.21.209.41:6379,slaves=2,sentinels=3
root@db01:/data/db/redis/26379# 

4、Sentinel管理命令

redis-cli -p 26380
PING :返回 PONG 。
SENTINEL masters :列出所有被监视的主服务器
SENTINEL slaves <master name> 

SENTINEL get-master-addr-by-name <master name> : 返回给定名字的主服务器的 IP 地址和端口号。 
SENTINEL reset <pattern> : 重置所有名字和给定模式 pattern 相匹配的主服务器。 
SENTINEL failover <master name> : 当主服务器失效时, 在不询问其他 Sentinel 意见的情况下, 强制开始一次自动故障迁移。


案例:
root@db01:/data/db/redis/26379# redis-cli -p 26379
127.0.0.1:26379> 
127.0.0.1:26379> ping
PONG
127.0.0.1:26379> SENTINEL masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "172.21.209.41"
    5) "port"
    6) "6379"
    7) "runid"
    8) "01381a1741eeef98302c485a3e88afd6aadd0b47"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "827"
   19) "last-ping-reply"
   20) "827"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "1695"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "496184"
   29) "config-epoch"
   30) "1"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "2"
   35) "quorum"
   36) "1"
   37) "failover-timeout"
   38) "180000"
   39) "parallel-syncs"
   40) "1"
127.0.0.1:26379> 

5、redis查看日志

查看哨兵日志:发现主从切换的过程
root@db01:/data/db/redis/26379# cat sentinel.log 

查看redis的日志:发现宕机过程
root@db01:/data/db/redis/6379# cat redis.log

相关文章

网友评论

      本文标题:【Redis】—05、Redis的哨兵模式sentinel部署—

      本文链接:https://www.haomeiwen.com/subject/thsnqdtx.html