Bootstrap

Redis 哨兵搭建

Redis哨兵(sentinel)搭建 7.2.5

一、单节点哨兵

1. 环境介绍

操作系统Centos 7
内核版本Linux 3.10.0-957.el7.x86_64
主机名称master
IP192.168.1.100
端口master:16370 slaver1:16371 slaver2: 16372 sentinel:16379

2. 环境前准备工作

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 修改 hostname
hostnamectl set-hostname xxxx  # 修改后退出当前终端重新连接即可

# 更新下软件源
# 地址 https://developer.aliyun.com/mirror/

# 时区调整,时间校准
date -R
timedatectl set-timezone Asia/Shanghai
yum -y install ntp
ntpdate ntp1.aliyun.com

# 关闭 selinux: 
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

# 修改内存分配控制
vi /etc/sysctl.conf

vm.overcommit_memory = 1
# 设置TPC积压值
net.core.somaxconn= 1024

# 使其配置文件生效
sysctl -p /etc/sysctl.conf

3. 安装 Redis 7.2.5

# 下载地址
http://download.redis.io/releases/

# 下载
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解压
tar -xf redis-7.2.5.tar.gz -C /opt/redis

# 编译
make MALLOC=libc

# 安装 注意如果安装提示python3不存在 执行 yum install -y python3
make install PREFIX=/usr/local/redis

# 配置环境变量
vi /etc/profile.d/redis.sh

#!/bin/bash

export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin 

# 使其配置生效
source /etc/profile.d/redis.sh

4. redis 配置修改并且启动

4.1 修改配置文件

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/1637{0..2}
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/1637{0..2}
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/1637{0..2}

# 创建redis pid存储目录
mkdir /usr/local/redis/run


# redis配置文件 16370
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  



# redis配置文件 16371
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  



# redis配置文件 16372
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  




# 复制下面命令即可
cat > /usr/local/redis/conf/redis_16370.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF



cat > /usr/local/redis/conf/redis_16371.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

cat > /usr/local/redis/conf/redis_16372.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

4.2 编写启动脚本

# 启动脚本
cat > /usr/local/redis/bin/master-slave_start.sh << EOF
#!/bin/bash



redis-server /usr/local/redis/conf/redis_16370.conf
redis-server /usr/local/redis/conf/redis_16371.conf
redis-server /usr/local/redis/conf/redis_16372.conf
EOF


# 赋予可执行权限
chmod +x /usr/local/redis/bin/master-slave_start.sh


# 关闭脚本
vi /usr/local/redis/bin/master-slave_shutdown.sh

#!/bin/bash

kill -9 $(ps -ef | grep 1637 | grep -v grep | awk '{print $2}')

# 赋予可执行权限
chmod +x /usr/local/redis/bin/master-slave_shutdown.sh

5. 开启主从

5.1 开启

# 以"192.168.1.100:16370"实例为主库,以"192.168.1.100:16371"实例和"192.168.1.100:16372"实例为从库。
# -a 参数指定密码
# 从库实例开启主从
 redis-cli -h 192.168.1.100 -p 16371 -a dongdong slaveof 192.168.1.100 16370
 redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof 192.168.1.100 16370
 
 # Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 # 警告:在命令行界面上使用带有“-a”或“-u”选项的密码可能不安全。  可以忽略
 # 解决方式
 # redis-cli -h 192.168.1.100 -p 16371 -a dongdong --no-auth-warning  slaveof 192.168.1.100 16370 
 

5.2 主库实例查看主从信息

redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
 
 [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.100,port=16371,state=online,offset=8428,lag=0
slave1:ip=192.168.1.100,port=16372,state=online,offset=8428,lag=1
master_failover_state:no-failover
master_replid:a292603e2c96a6f451055e6d84a60795f81f2928
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:8428
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:8428

6. 创建sentinel的配置文件并启动

6.1 创建配置文件

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16379
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16379
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16379

# 创建redis pid存储目录
mkdir /usr/local/redis/run

cat > /usr/local/redis/conf/sentinel.conf <<EOF
bind 192.168.1.100
port 16379
dir /usr/local/redis/data/16379
sentinel monitor redis_master 192.168.1.100 16370 1
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF

配置文件参数说明如下:
    sentinel monitor redis_master 192.168.1.100 16370 1:
        指定Redis初始集群的master库节点并命名为"redis_master",注意这个"1"的含义,他表示sentinel需要最少的投票数。这在于多sentinel的场景下很有用,由于本案例只是用了1个sentinel,因此我就设置为1。
        在生产环境中,建议配置多sentinel的模式,建议最少设置为3(建议设置奇数)个sentinel物理节点,而后将此处的"1"改为"2",表示3个sentinel中最少有2个节点认为master库宕机时,才会真正意义上认为master库宕机。       

    sentinel down-after-milliseconds redis_master 5000:
        监控sentinel集群时,如果超过了5000毫秒(即5秒)仍然没有响应,则sentinel会判定master库宕机了。

    sentinel auth-pass redis_master dongdong:
        由于sentinel需要访问Redis集群,因此我们要设置访问整个集群的密码,我这里指定的密码为"dongdong",这意味着所有的节点都使用相同的密码。

6.2 启动

# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/sentinel.conf &> /usr/local/redis/log/16379/sentinel_16379.log &

root@master ~]# redis-sentin el /usr/local/redis/conf/sentinel.conf &> /usr/local/redis/log/16379/sentinel_16379.log &
[1] 29961

# 查看日志
tail -100f /usr/local/redis/log/16379/sentinel_16379.log
[root@master ~]# tail -100f /usr/local/redis/log/16379/sentinel_16379.log
30008:X 17 Jul 2024 19:51:15.356 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30008:X 17 Jul 2024 19:51:15.356 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30008, just started
30008:X 17 Jul 2024 19:51:15.356 * Configuration loaded
30008:X 17 Jul 2024 19:51:15.356 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30008:X 17 Jul 2024 19:51:15.356 * monotonic clock: POSIX clock_gettime
30008:X 17 Jul 2024 19:51:15.357 * Running mode=sentinel, port=16379.
30008:X 17 Jul 2024 19:51:15.357 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30008:X 17 Jul 2024 19:51:15.357 * Sentinel ID is a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b
30008:X 17 Jul 2024 19:51:15.357 # +monitor master redis_master 192.168.1.100 16370 quorum 1


# 通过redis-cli 进入 查看
redis-cli -h 192.168.1.100 -p 16379

[root@master ~]# redis-cli -h 192.168.1.100 -p 16379
192.168.1.100:16379> ping
PONG
192.168.1.100:16379> 

6.3 手动停止主库运行,模拟主库宕机

# 切换前主从状态正常
[root@master ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.100:16370              *:*                   users:(("redis-server",pid=29871,fd=6))
tcp    LISTEN     0      128    192.168.1.100:16371              *:*                   users:(("redis-server",pid=29873,fd=6))
tcp    LISTEN     0      128    192.168.1.100:16372              *:*                   users:(("redis-server",pid=29881,fd=6))
tcp    LISTEN     0      128    192.168.1.100:16379              *:*                   users:(("redis-sentinel",pid=30008,fd=6))

# 查看集群状态 master
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 

[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.100,port=16371,state=online,offset=111085,lag=0
slave1:ip=192.168.1.100,port=16372,state=online,offset=111085,lag=1
master_failover_state:no-failover
master_replid:208227bbd586d90841dcf753ba669dbde246bba7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:111085
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:111085

# 查看集群状态 slaver
redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication 

[root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  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.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:118087
slave_repl_offset:118087
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:208227bbd586d90841dcf753ba669dbde246bba7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:118087
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:118073


# 查看集群状态 slaver
redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 

[root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  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.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:126869
slave_repl_offset:126869
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:208227bbd586d90841dcf753ba669dbde246bba7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126869
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:126855


# 手动将主库宕机,观察从库被sentinel实现了自动切换:
redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown

[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# 再查看集群状态 master
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication  # 由于将主库服务停了,因此无法再次连接主库
[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused

# 再查看集群状态 slaves  发现变成了master节点
redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication 

[root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  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:1
slave0:ip=192.168.1.100,port=16372,state=online,offset=150234,lag=0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:150234
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:150220

# 查看集群状态 slaver
redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 

[root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  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.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:154406
slave_repl_offset:154406
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:154406
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:154392

# 查看sentinel的日志信息,不难发现有记录切换的过程
tail -100f /usr/local/redis/log/16379/sentinel_16379.log

[root@master ~]# tail -100f /usr/local/redis/log/16379/sentinel_16379.log
30008:X 17 Jul 2024 19:51:15.356 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30008:X 17 Jul 2024 19:51:15.356 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30008, just started
30008:X 17 Jul 2024 19:51:15.356 * Configuration loaded
30008:X 17 Jul 2024 19:51:15.356 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30008:X 17 Jul 2024 19:51:15.356 * monotonic clock: POSIX clock_gettime
30008:X 17 Jul 2024 19:51:15.357 * Running mode=sentinel, port=16379.
30008:X 17 Jul 2024 19:51:15.357 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30008:X 17 Jul 2024 19:51:15.357 * Sentinel ID is a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b
30008:X 17 Jul 2024 19:51:15.357 # +monitor master redis_master 192.168.1.100 16370 quorum 1
30008:X 17 Jul 2024 20:15:40.962 # +sdown master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.962 # +odown master redis_master 192.168.1.100 16370 #quorum 1/1
30008:X 17 Jul 2024 20:15:40.962 # +new-epoch 1
30008:X 17 Jul 2024 20:15:40.962 # +try-failover master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:40.968 # +vote-for-leader a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b 1
30008:X 17 Jul 2024 20:15:40.968 # +elected-leader master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 # +failover-state-select-slave master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.026 # +selected-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.027 * +failover-state-send-slaveof-noone slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.093 * +failover-state-wait-promotion slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:42.015 # +promoted-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 # +failover-state-reconf-slaves master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.096 * +slave-reconf-sent slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-inprog slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-done slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +failover-end master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +switch-master redis_master 192.168.1.100 16370 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.100 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:48.121 # +sdown slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371

6.4 手动修复宕机的主库,sentinel会自动发现

# 修复之前,16370端口是连接不上的
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication

[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused


[root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  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:1
slave0:ip=192.168.1.100,port=16372,state=online,offset=190264,lag=1
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:190410
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:190396



[root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  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.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:192204
slave_repl_offset:192204
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:192204
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:192190

# 修复之后,之前宕机的主库会自动加入集群并成为slaves节点
redis-server /usr/local/redis/conf/redis_16370.conf
[root@master ~]# redis-server /usr/local/redis/conf/redis_16370.conf
[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:199215
slave_repl_offset:199215
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:199215
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:199047
repl_backlog_histlen:169

# 查看 
redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication

redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 

[root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  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 # slaves 变成两个了
slave0:ip=192.168.1.100,port=16372,state=online,offset=203533,lag=1
slave1:ip=192.168.1.100,port=16370,state=online,offset=203533,lag=1
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:203533
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:203519


[root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  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.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:207851
slave_repl_offset:207851
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:207851
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:207837

# 注意观察sentinel日志哟,会有新日志记录
tail -100f /usr/local/redis/log/16379/sentinel_16379.log
[root@master ~]# tail -100f /usr/local/redis/log/16379/sentinel_16379.log
30008:X 17 Jul 2024 19:51:15.356 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30008:X 17 Jul 2024 19:51:15.356 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30008, just started
30008:X 17 Jul 2024 19:51:15.356 * Configuration loaded
30008:X 17 Jul 2024 19:51:15.356 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30008:X 17 Jul 2024 19:51:15.356 * monotonic clock: POSIX clock_gettime
30008:X 17 Jul 2024 19:51:15.357 * Running mode=sentinel, port=16379.
30008:X 17 Jul 2024 19:51:15.357 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30008:X 17 Jul 2024 19:51:15.357 * Sentinel ID is a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b
30008:X 17 Jul 2024 19:51:15.357 # +monitor master redis_master 192.168.1.100 16370 quorum 1
30008:X 17 Jul 2024 20:15:40.962 # +sdown master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.962 # +odown master redis_master 192.168.1.100 16370 #quorum 1/1
30008:X 17 Jul 2024 20:15:40.962 # +new-epoch 1
30008:X 17 Jul 2024 20:15:40.962 # +try-failover master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:40.968 # +vote-for-leader a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b 1
30008:X 17 Jul 2024 20:15:40.968 # +elected-leader master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 # +failover-state-select-slave master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.026 # +selected-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.027 * +failover-state-send-slaveof-noone slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.093 * +failover-state-wait-promotion slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:42.015 # +promoted-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 # +failover-state-reconf-slaves master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.096 * +slave-reconf-sent slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-inprog slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-done slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +failover-end master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +switch-master redis_master 192.168.1.100 16370 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.100 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:48.121 # +sdown slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:30:15.529 # -sdown slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:30:25.504 * +convert-to-slave slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371


# 除了提到上面的变化外,还有一点变化就是配置文件也修改了,每个"slave"节点都新增了 下面三行 本案例只有192.168.1.100:16370 和 192.168.1.100:16372是 slave
# Generated by CONFIG REWRITE
replicaof 192.168.1.100 16371
latency-tracking-info-percentiles 50 99 99.9
user default on sanitize-payload #162fffe9f429e1fa5d351c11f5a7f2f3031806c5125eeb84993f68738cbb6c84 ~* &* +@all


# Generated by CONFIG REWRITE
replicaof 192.168.1.100 16371
latency-tracking-info-percentiles 50 99 99.9
user default on sanitize-payload #162fffe9f429e1fa5d351c11f5a7f2f3031806c5125eeb84993f68738cbb6c84 ~* &* +@all

二、多节点哨兵

一主两从三哨兵

一共三台机器,每台机器部署一个哨兵

注意: 后面启动哨兵可能会出现两个警告

WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.

警告:必须启用内存超量使用!没有它,在内存不足的情况下,后台保存或复制可能会失败。要解决此问题,请将“vm.overcommit_memory=1”添加到/etc/sysctl.conf中,然后重新启动或运行命令“sysctl vm.overcommit_memory=1”以使其生效。

修改内存分配控制

vi /etc/sysctl.conf

vm.overcommit_memory = 1

使其配置文件生效

sysctl -p /etc/sysctl.conf

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

警告:无法强制执行TCP积压设置511,因为/proc/sys/net/core/somaxconn设置为较低的值128。

vi /etc/sysctl.conf

net.core.somaxconn= 1024

sysctl -p /etc/sysctl.conf

操作系统Centos 7Centos 7Centos 7
内核版本Linux 3.10.0-957.el7.x86_64Linux 3.10.0-957.el7.x86_64Linux 3.10.0-957.el7.x86_64
主机名称redis_masterredis_salve1redis_salve2
IP192.168.1.100192.168.1.200192.168.1.250
端口16370、2637016371、2637016372、26370

安装前准备工作已经Redis安装,请参照上面步骤进行

1.Redis配置文件修改并启动

1.1 redis_master 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16370

# 创建redis pid存储目录
mkdir /usr/local/redis/run/16370

# 修改配置文件
# 复制下面命令即可
cat > /usr/local/redis/conf/16370/redis_16370.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/16370/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

# 启动Redis
redis-server /usr/local/redis/conf/16370/redis_16370.conf

1.2 redis_slave1 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16371
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16371
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16371

# 创建redis pid存储目录
mkdir /usr/local/redis/run/16371

# 修改配置文件
# 复制下面命令即可
cat > /usr/local/redis/conf/16371/redis_16371.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.200   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/16371/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16371/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

# 启动Redis
redis-server /usr/local/redis/conf/16371/redis_16371.conf

1.3 redis_slave2 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16372
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16372
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16372

# 创建redis pid存储目录
mkdir /usr/local/redis/run/16372

# 修改配置文件
# 复制下面命令即可
cat > /usr/local/redis/conf/16372/redis_16372.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.250   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/16372/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16372/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

# 启动Redis
redis-server /usr/local/redis/conf/16372/redis_16372.conf

2. 开启主从

2.1 redis_slave1 节点

# 从库实例开启主从
redis-cli -h 192.168.1.200 -p 16371 -a dongdong slaveof 192.168.1.100 16370

[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong slaveof 192.168.1.100 16370
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

# 从库实例查看主从信息
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 

[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  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.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_read_repl_offset:210
slave_repl_offset:210
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:210
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:196

2.2 redis_slave2 节点

# 从库实例开启主从
redis-cli -h 192.168.1.250 -p 16372 -a dongdong slaveof 192.168.1.100 16370

[root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong slaveof 192.168.1.100 16370
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

# 从库实例查看主从信息
redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication 

[root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  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.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_read_repl_offset:266
slave_repl_offset:266
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:224

2.3 redis_master 节点

# 主库实例查看主从信息
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 

[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200,port=16371,state=online,offset=42,lag=0
slave1:ip=192.168.1.250,port=16372,state=online,offset=42,lag=1
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56

3. 编写sentinel配置文件并启动

3.1 redis_master 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/26370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/26370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/26370

# 创建redis pid存储目录
mkdir /usr/local/redis/run/26370

cat > /usr/local/redis/conf/26370/sentinel.conf <<EOF
bind 192.168.1.100
port 26370
dir /usr/local/redis/data/26370
sentinel monitor redis_master 192.168.1.100 16370 2
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF

# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &

[root@redis_master ~]# redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &
[1] 30569

# 查看redis-sentinel 进程
[root@redis_master ~]# ps -ef | grep redis
root      30552      1  0 10:17 ?        00:00:01 redis-server 192.168.1.100:16370
root      30569  30499  0 10:33 pts/1    00:00:00 redis-sentinel 192.168.1.100:26370 [sentinel]  # 已经成功启动了
root      30574  30499  0 10:34 pts/1    00:00:00 grep --color=auto redis

# 查看日志信息
tail -100f /usr/local/redis/log/26370/sentinel_26370.log

[root@redis_master ~]# tail -100f /usr/local/redis/log/26370/sentinel_26370.log
30569:X 18 Jul 2024 10:33:50.636 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30569:X 18 Jul 2024 10:33:50.636 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30569, just started
30569:X 18 Jul 2024 10:33:50.636 * Configuration loaded
30569:X 18 Jul 2024 10:33:50.636 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30569:X 18 Jul 2024 10:33:50.636 * monotonic clock: POSIX clock_gettime
30569:X 18 Jul 2024 10:33:50.637 * Running mode=sentinel, port=26370.
30569:X 18 Jul 2024 10:33:50.637 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30569:X 18 Jul 2024 10:33:50.638 * Sentinel new configuration saved on disk
30569:X 18 Jul 2024 10:33:50.639 * Sentinel ID is 088b671eb4ac4c8d8a57805c1be88e4f76cf7ff8
30569:X 18 Jul 2024 10:33:50.639 # +monitor master redis_master 192.168.1.100 26370 quorum 2
30569:X 18 Jul 2024 10:33:52.638 * +sentinel sentinel 088b671eb4ac4c8d8a57805c1be88e4f76cf7ff8 192.168.1.100 26370 @ redis_master 192.168.1.100 26370
30569:X 18 Jul 2024 10:33:52.644 * Sentinel new configuration saved on disk

3.2 redis_slave1 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/26370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/26370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/26370

# 创建redis pid存储目录
mkdir /usr/local/redis/run/26370

cat > /usr/local/redis/conf/26370/sentinel.conf <<EOF
bind 192.168.1.200
port 26370
dir /usr/local/redis/data/26370
sentinel monitor redis_master 192.168.1.100 16370 2
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF

# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &

[root@redis_slave1 ~]# redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &
[1] 38894

# 查看redis-sentinel 进程
[root@redis_slave1 ~]# ps -ef | grep redis
root      38879      1  0 10:19 ?        00:00:01 redis-server 192.168.1.200:16371
root      38894  38840  0 10:36 pts/0    00:00:00 redis-sentinel 192.168.1.200:26370 [sentinel]
root      38899  38840  0 10:37 pts/0    00:00:00 grep --color=auto redis

# 查看日志信息
tail -100f /usr/local/redis/log/26370/sentinel_26370.log

[root@redis_slave1 ~]# tail -100f /usr/local/redis/log/26370/sentinel_26370.log
38894:X 18 Jul 2024 10:36:54.115 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
38894:X 18 Jul 2024 10:36:54.116 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
38894:X 18 Jul 2024 10:36:54.116 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=38894, just started
38894:X 18 Jul 2024 10:36:54.116 * Configuration loaded
38894:X 18 Jul 2024 10:36:54.116 * Increased maximum number of open files to 10032 (it was originally set to 1024).
38894:X 18 Jul 2024 10:36:54.116 * monotonic clock: POSIX clock_gettime
38894:X 18 Jul 2024 10:36:54.116 * Running mode=sentinel, port=26370.
38894:X 18 Jul 2024 10:36:54.116 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
38894:X 18 Jul 2024 10:36:54.119 * Sentinel new configuration saved on disk
38894:X 18 Jul 2024 10:36:54.119 * Sentinel ID is 93a83bf4770f1f8eda48b96ead065382ab2b5fe6
38894:X 18 Jul 2024 10:36:54.119 # +monitor master redis_slave1 192.168.1.200 26370 quorum 2
38894:X 18 Jul 2024 10:36:56.164 * +sentinel sentinel 93a83bf4770f1f8eda48b96ead065382ab2b5fe6 192.168.1.200 26370 @ redis_slave1 192.168.1.200 26370
38894:X 18 Jul 2024 10:36:56.170 * Sentinel new configuration saved on disk

3.3 redis_slave2 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/26370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/26370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/26370

# 创建redis pid存储目录
mkdir /usr/local/redis/run/26370

cat > /usr/local/redis/conf/26370/sentinel.conf <<EOF
bind 192.168.1.250
port 26370
dir /usr/local/redis/data/26370
sentinel monitor redis_master 192.168.1.100 16370 2
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF

# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &

[root@redis_slave2 ~]# redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &
[1] 39310

# 查看redis-sentinel 进程
[root@redis_slave2 ~]# ps -ef | grep redis
root      39290      1  0 10:20 ?        00:00:03 redis-server 192.168.1.250:16372
root      39310  39251  0 10:50 pts/0    00:00:00 redis-sentinel 192.168.1.250:26370 [sentinel]
root      39316  39251  0 10:51 pts/0    00:00:00 grep --color=auto redis

# 查看日志信息
 tail -100f /usr/local/redis/log/26370/sentinel_26370.log
 
[root@redis_slave2 ~]# tail -100f /usr/local/redis/log/26370/sentinel_26370.log
39310:X 18 Jul 2024 10:50:57.403 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
39310:X 18 Jul 2024 10:50:57.403 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
39310:X 18 Jul 2024 10:50:57.403 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=39310, just started
39310:X 18 Jul 2024 10:50:57.403 * Configuration loaded
39310:X 18 Jul 2024 10:50:57.404 * Increased maximum number of open files to 10032 (it was originally set to 1024).
39310:X 18 Jul 2024 10:50:57.404 * monotonic clock: POSIX clock_gettime
39310:X 18 Jul 2024 10:50:57.404 * Running mode=sentinel, port=26370.
39310:X 18 Jul 2024 10:50:57.405 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
39310:X 18 Jul 2024 10:50:57.407 * Sentinel new configuration saved on disk
39310:X 18 Jul 2024 10:50:57.407 * Sentinel ID is 9227635399651de294a88fa16281232b9cbbccaf
39310:X 18 Jul 2024 10:50:57.407 # +monitor master redis_slave2 192.168.1.250 26370 quorum 2
39310:X 18 Jul 2024 10:50:59.427 * +sentinel sentinel 9227635399651de294a88fa16281232b9cbbccaf 192.168.1.250 26370 @ redis_slave2 192.168.1.250 26370
39310:X 18 Jul 2024 10:50:59.434 * Sentinel new configuration saved on disk

4. 验证sentinel能够自动切换

4.1 手动停止主库运行,模拟主库宕机

# 查看主库的状态
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200,port=16371,state=online,offset=2898,lag=0
slave1:ip=192.168.1.250,port=16372,state=online,offset=2898,lag=0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2898
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2898

# 查看从库状态
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication

[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  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.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:3066
slave_repl_offset:3066
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3066
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:3052

redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication

[root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  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.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:3094
slave_repl_offset:3094
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3094
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:3052

# 各节点运行redis进程情况
root@redis_master ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.100:16370              *:*                   users:(("redis-server",pid=30552,fd=6))
tcp    LISTEN     0      128    192.168.1.100:26370              *:*                   users:(("redis-sentinel",pid=30569,fd=6))


[root@redis_slave1 ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.200:16371              *:*                   users:(("redis-server",pid=38879,fd=6))
tcp    LISTEN     0      511    192.168.1.200:26370              *:*                   users:(("redis-sentinel",pid=38908,fd=6))


[root@redis_slave2 ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.250:16372              *:*                   users:(("redis-server",pid=39290,fd=6))
tcp    LISTEN     0      128    192.168.1.250:26370              *:*                   users:(("redis-sentinel",pid=39310,fd=6))

# 一切正常
# 手动模拟主库宕机 再来查看情况
redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown

[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# 再次执行命令
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused  # 发现连接不上 因为我们把主库宕了

4.2 当16370 实例宕机后,查看集群情况

# 查看集群状态 
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication  

[root@redis_master ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master # 此时的slave1主机变成了master 节点
connected_slaves:1   # slave 也变成了1个
slave0:ip=192.168.1.250,port=16372,state=online,offset=14837,lag=1
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:14837
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14837


# 查看集群状态
redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication  

[root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  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.1.200 
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:87953
slave_repl_offset:87953
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:87953
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:87953


# 现在集群里面的情况就是 一主一从

4.3 恢复16370 实例会成为slaves节点

# 修复之前,16370端口是连接不上的
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication

[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused

# 修复之后,之前宕机的主库会自动加入集群并成为slaves节点
# 启动Redis
redis-server /usr/local/redis/conf/16370/redis_16370.conf

# 可能需要过了几秒才会显示哦 因为信息还没有同步过来哦
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:165354
slave_repl_offset:165354
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:165354
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:164602
repl_backlog_histlen:753

5. 手动停掉一个sentinel实例

5.1 将redis_master主机中的sentinel实例给停止掉

# 查看redis进程
[root@redis_master ~]# ps -ef | grep redis
root      30646  30499  0 11:23 pts/1    00:00:06 redis-sentinel 192.168.1.100:26370 [sentinel]
root      30663      1  0 11:42 ?        00:00:01 redis-server 192.168.1.100:16370
root      30672  30499  0 11:50 pts/1    00:00:00 grep --color=auto redis


# 杀掉 进程id为30646服务
kill -9 30646

# 再次查看redis进程 sentinel 已经被杀掉了
[root@redis_master ~]# ps -ef | grep redis
root      30663      1  0 11:42 ?        00:00:01 redis-server 192.168.1.100:16370
root      30676  30499  0 11:51 pts/1    00:00:00 grep --color=auto redis

5.2 验证主从切换

# 查看各节点状况 此时slave1这台是主库
# 查询从库状态 redis_master主机
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:251306
slave_repl_offset:251306
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:251306
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:164602
repl_backlog_histlen:86705

# 查询从库状态 redis_slave2主机
redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  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.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:254136
slave_repl_offset:254136
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:254136
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:254136

# 查询主库状态
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  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.1.250,port=16372,state=online,offset=261722,lag=0
slave1:ip=192.168.1.100,port=16370,state=online,offset=261736,lag=0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:261882
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:261882

# 此时干掉主库
redis-cli -h 192.168.1.200 -p 16371 -a dongdong shutdown
[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# 再次查询
[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.200:16371: Connection refused


# 此时查询集群个节点状态 看看哪个节点成功master 此时集群中只有两个节点 分别是redis_master主机 和 redis_slave2主机
# 先查看redis_master
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.250
master_port:16372
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:56377
slave_repl_offset:56377
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:56377
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:30454
repl_backlog_histlen:25924

# 是正常的

5.3 恢复Redis 实例

# 查看各节点 redis 服务是否停用 如果停用 则重新启动
ps -ef | grep redis


# redis_master
redis-server /usr/local/redis/conf/16370/redis_16370.conf

# redis_slave1
redis-server /usr/local/redis/conf/16371/redis_16371.conf


# redis_slave2
redis-server /usr/local/redis/conf/16372/redis_16372.conf

6. 手动停掉两个sentinel 实例

6.1 将redis_slave1 主机中的sentinel 实例停止

[root@redis_slave1 ~]# ps -ef | grep redis
root      39196      1  0 13:43 ?        00:00:02 redis-server 192.168.1.200:16371
root      39207  38840  0 13:45 pts/0    00:00:03 redis-sentinel 192.168.1.200:26370 [sentinel]
root      39220  38840  0 13:57 pts/0    00:00:00 grep --color=auto redis


kill -9 39207

root@redis_slave1 ~]# ps -ef | grep redis
root      39196      1  0 13:43 ?        00:00:02 redis-server 192.168.1.200:16371
root      39224  38840  0 13:59 pts/0    00:00:00 grep --color=auto redis

6.2 验证主从切换

# 查询redis_master主机集群状态
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:140766
slave_repl_offset:140766
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:140766
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:30454
repl_backlog_histlen:110313


# 查询redis_slvae1主机集群状态
[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  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.1.100,port=16370,state=online,offset=143290,lag=0
slave1:ip=192.168.1.250,port=16372,state=online,offset=143290,lag=1
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:143436
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:143422


# 查询redis_slave2主机集群状态
[root@redis_slave2 conf]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  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.1.200
master_port:16371
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:1
slave_repl_offset:1
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:eb44c8dbaaf01ca3b091a4005ecb563cc467dc76
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


# 停掉redis_slvae1主机的redis 服务 看看是否可以进行主从切换
[root@redis_slave1 ~]# ps -ef | grep redis
root      39196      1  0 13:43 ?        00:00:03 redis-server 192.168.1.200:16371
root      39242  38840  0 14:03 pts/0    00:00:00 grep --color=auto redis


kill -9 39196

# 再次查看redis_master主机集群状态
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200
master_port:16371
master_link_status:down  # master 已经是 down 状态 说明没有进行主从切换
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:154925
slave_repl_offset:154925
master_link_down_since_seconds:49
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:154925
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:30454
repl_backlog_histlen:124472

# 再次查询redis_slvae2主机集群状态
[root@redis_slave2 conf]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  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.1.200
master_port:16371
master_link_status:down # master 已经是 down 状态 说明没有进行主从切换
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:154925
slave_repl_offset:154925
master_link_down_since_seconds:133
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154925
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:149549
repl_backlog_histlen:5377


# 是因为我们再配置文件中指定了 sentinel monitor redis_master 192.168.1.100 16370 2  这个2 表示3个sentinel中最少有2个节点认为master库宕机时,才会真正意义上认为master库宕机。    

# 把redis_slvae1主机的redis 服务恢复
redis-server /usr/local/redis/conf/16371/redis_16371.conf

# 查看状态
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  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.1.200
master_port:16371
master_link_status:up  # 恢复redis_slvae1主机的redis后 master服务up了
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:3474
slave_repl_offset:3474
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:527f1bfc1c67513d0813d0c0fddb07964640d907
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3474
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:476
repl_backlog_histlen:2999
;