文章目录
- 一、redis准备3节点
- 1. 创建目录
- 2. 节点1~配置
- 3. 节点2~配置
- 4. 节点3~配置
- 5. 启动redis
- 二、新增sentinel配置
- 1. sentinel_01.conf
- 2. sentinel_02.conf
- 3. sentinel_03.conf
- 4. sentinel 启动
- 5. sentinel 监控
- 6. 哨兵验证
一、redis准备3节点
1. 创建目录
mkdir /usr/local/redis/conf/redis01 -p
mkdir /usr/local/redis/conf/redis02 -p
mkdir /usr/local/redis/conf/redis03 -pmkdir /usr/local/redis/data/redis01 -p
mkdir /usr/local/redis/data/redis02 -p
mkdir /usr/local/redis/data/redis03 -pmkdir /usr/local/redis/log/redis01 -p
mkdir /usr/local/redis/log/redis02 -p
mkdir /usr/local/redis/log/redis03 -p
2. 节点1~配置
vim /usr/local/redis/conf/redis01/redis-6380.conf
# 放行访问IP的限制 多个用空格隔开
# 0.0.0.0 代表无限制
bind 0.0.0.0# 后台启动开启
daemonize yes# 日志存储目录以及日志文件名
logfile "/usr/local/redis/log/redis01/redis.log"# rdb数据文件名
dbfilename "dump.rdb"# aof模式开启
appendonly no# aof数据文件名
appendfilename "appendonly.aof"#rdb数据文件和aof数据文件存储目录
dir "/usr/local/redis/data/redis01"# 设置密码
requirepass "123456"# 从节点访问主节点的密码(必须和requirepass 一致)
masterauth "123456"#从节点只读模式
replica-read-only yes#关闭保护模式,可以外部访问
protected-mode no# 端口
port 6380
3. 节点2~配置
vim /usr/local/redis/conf/redis02/redis-6381.conf
# 放行访问IP的限制 多个用空格隔开
# 0.0.0.0 代表无限制
bind 0.0.0.0# 后台启动开启
daemonize yes# 日志存储目录以及日志文件名
logfile "/usr/local/redis/log/redis02/redis.log"# rdb数据文件名
dbfilename "dump.rdb"# aof模式开启
appendonly no# aof数据文件名
appendfilename "appendonly.aof"#rdb数据文件和aof数据文件存储目录
dir "/usr/local/redis/data/redis02"# 设置密码
requirepass "123456"# 从节点访问主节点的密码(必须和requirepass 一致)
masterauth "123456"#从节点只读模式
replica-read-only yes
port 6381#关闭保护模式,可以外部访问
protected-mode no# 从节点从属于那个主节点
replicaof 10.5.8.76 6380
4. 节点3~配置
vim /usr/local/redis/conf/redis03/redis-6382.conf
# 放行访问IP的限制 多个用空格隔开
# 0.0.0.0 代表无限制
bind 0.0.0.0# 后台启动开启
daemonize yes# 日志存储目录以及日志文件名
logfile "/usr/local/redis/log/redis03/redis.log"# rdb数据文件名
dbfilename "dump.rdb"# aof模式开启
appendonly no# aof数据文件名
appendfilename "appendonly.aof"#rdb数据文件和aof数据文件存储目录
dir "/usr/local/redis/data/redis03"# 设置密码
requirepass "123456"# 从节点访问主节点的密码(必须和requirepass 一致)
masterauth "123456"#从节点只读模式
replica-read-only yes
port 6382
#关闭保护模式,可以外部访问
protected-mode no# 从节点从属于那个主节点
replicaof 10.5.8.76 6380
5. 启动redis
/app/redis-6.2.6/src/redis-server /usr/local/redis/conf/redis01/redis-6380.conf
/app/redis-6.2.6/src/redis-server /usr/local/redis/conf/redis02/redis-6381.conf
/app/redis-6.2.6/src/redis-server /usr/local/redis/conf/redis03/redis-6382.conf
二、新增sentinel配置
1. sentinel_01.conf
vim /usr/local/redis/conf/sentiel_01.conf
# 放行所有ip限制
bind 0.0.0.0# 进程端口号
port 26380# 后台启动
daemonize yes# 日志记录文件
logfile "/usr/local/redis/log/sentinel_01.log"# 进程编号记录文件
pidfile "/var/run/redis-sentinel_01.pid"# 提示Sentinel 去监视一个名为 mymaster 的主服务器 仲裁 =环境3/2+1
sentinel monitor mymaster 192.168.45.80 6380 2# 访问主节点的密码
sentinel auth-pass mymaster 123456# Sentinel 认为服务器已经断线所需要的毫秒数 默认30秒 这里演示10秒
sentinel down-after-milliseconds mymaster 10000# 若Sentinel 在改配置值内未能完成 failover(故障转移) 操作,则认为本次 failover 失败180秒 3分钟
sentinel failover-timeout mymaster 180000
2. sentinel_02.conf
vim /usr/local/redis/conf/sentiel_02.conf
# 放行所有ip限制
bind 0.0.0.0# 进程端口号
port 26381# 后台启动
daemonize yes# 日志记录文件
logfile "/usr/local/redis/log/sentinel_02.log"# 进程编号记录文件
pidfile "/var/run/redis-sentinel_02.pid"# 提示Sentinel 去监视一个名为 mymaster 的主服务器 仲裁 =环境3/2+1
sentinel monitor mymaster 192.168.45.80 6380 2# 访问主节点的密码
sentinel auth-pass mymaster 123456# Sentinel 认为服务器已经断线所需要的毫秒数 默认30秒 这里演示10秒
sentinel down-after-milliseconds mymaster 10000# 若Sentinel 在改配置值内未能完成 failover(故障转移) 操作,则认为本次 failover 失败180秒 3分钟
sentinel failover-timeout mymaster 180000
3. sentinel_03.conf
vim /usr/local/redis/conf/sentiel_03.conf
# 放行所有ip限制
bind 0.0.0.0# 进程端口号
port 26382# 后台启动
daemonize yes# 日志记录文件
logfile "/usr/local/redis/log/sentinel_03.log"# 进程编号记录文件
pidfile "/var/run/redis-sentinel_03.pid"# 提示Sentinel 去监视一个名为 mymaster 的主服务器 仲裁 =环境3/2+1
sentinel monitor mymaster 192.168.45.80 6380 2# 访问主节点的密码
sentinel auth-pass mymaster 123456# Sentinel 认为服务器已经断线所需要的毫秒数 默认30秒 这里演示10秒
sentinel down-after-milliseconds mymaster 10000# 若Sentinel 在改配置值内未能完成 failover(故障转移) 操作,则认为本次 failover 失败180秒 3分钟
sentinel failover-timeout mymaster 180000
4. sentinel 启动
/app/redis-6.2.6/src/redis-sentinel /usr/local/redis/conf/sentinel_01.conf
/app/redis-6.2.6/src/redis-sentinel /usr/local/redis/conf/sentinel_02.conf
/app/redis-6.2.6/src/redis-sentinel /usr/local/redis/conf/sentinel_03.conf
5. sentinel 监控
- sentinel _01
[root@VM-24-10-centos ~]# tail -f /usr/local/redis/log/sentinel_01.log
22858:X 23 Oct 2022 14:54:00.271 # Configuration loaded
22858:X 23 Oct 2022 14:54:00.272 * monotonic clock: POSIX clock_gettime
22858:X 23 Oct 2022 14:54:00.272 * Running mode=sentinel, port=26380.
22858:X 23 Oct 2022 14:54:00.273 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/coris set to the lower value of 128.
22858:X 23 Oct 2022 14:54:00.283 # Sentinel ID is 24381e0b70d72fdbb5489ee58a5db7e521dd7a54
22858:X 23 Oct 2022 14:54:00.283 # +monitor master mymaster 192.168.45.80 6380 quorum 2
22858:X 23 Oct 2022 14:54:00.286 * +slave slave 192.168.45.80:6381 192.168.45.80 6381 @ mymaster 192.168.45.80 6380
22858:X 23 Oct 2022 14:54:00.298 * +slave slave 192.168.45.80:6382 192.168.45.80 6382 @ mymaster 192.168.45.80 6380
22858:X 23 Oct 2022 14:54:02.286 * +sentinel sentinel acd408cb9683b06e5786ecded9af3cb77b3bc33c 10.0.24.10 26381 @ mymas26.80 6380
22858:X 23 Oct 2022 14:54:03.994 * +sentinel sentinel 1da437262d0fd32e484a63d9b0e4155db857923b 10.0.24.10 26382 @ mymas26.80 6380
- sentinel _02
[root@VM-24-10-centos ~]# tail -f /usr/local/redis/log/sentinel_02.log
22864:X 23 Oct 2022 14:54:00.277 # Configuration loaded
22864:X 23 Oct 2022 14:54:00.278 * monotonic clock: POSIX clock_gettime
22864:X 23 Oct 2022 14:54:00.279 * Running mode=sentinel, port=26381.
22864:X 23 Oct 2022 14:54:00.279 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/coris set to the lower value of 128.
22864:X 23 Oct 2022 14:54:00.289 # Sentinel ID is acd408cb9683b06e5786ecded9af3cb77b3bc33c
22864:X 23 Oct 2022 14:54:00.289 # +monitor master mymaster 192.168.45.80 6380 quorum 2
22864:X 23 Oct 2022 14:54:00.292 * +slave slave 192.168.45.80:6381 192.168.45.80 6381 @ mymaster 192.168.45.80 6380
22864:X 23 Oct 2022 14:54:00.300 * +slave slave 192.168.45.80:6382 192.168.45.80 6382 @ mymaster 192.168.45.80 6380
22864:X 23 Oct 2022 14:54:02.309 * +sentinel sentinel 24381e0b70d72fdbb5489ee58a5db7e521dd7a54 10.0.24.10 26380 @ mymas26.80 6380
22864:X 23 Oct 2022 14:54:03.994 * +sentinel sentinel 1da437262d0fd32e484a63d9b0e4155db857923b 10.0.24.10 26382 @ mymas26.80 6380
- sentinel _03
[root@VM-24-10-centos src]# tail -f /usr/local/redis/log/sentinel_03.log
22937:X 23 Oct 2022 14:54:01.929 # Configuration loaded
22937:X 23 Oct 2022 14:54:01.929 * monotonic clock: POSIX clock_gettime
22937:X 23 Oct 2022 14:54:01.930 * Running mode=sentinel, port=26382.
22937:X 23 Oct 2022 14:54:01.930 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
22937:X 23 Oct 2022 14:54:01.940 # Sentinel ID is 1da437262d0fd32e484a63d9b0e4155db857923b
22937:X 23 Oct 2022 14:54:01.940 # +monitor master mymaster 192.168.45.80 6380 quorum 2
22937:X 23 Oct 2022 14:54:01.942 * +slave slave 192.168.45.80:6381 192.168.45.80 6381 @ mymaster 192.168.45.80 6380
22937:X 23 Oct 2022 14:54:01.950 * +slave slave 192.168.45.80:6382 192.168.45.80 6382 @ mymaster 192.168.45.80 6380
22937:X 23 Oct 2022 14:54:02.286 * +sentinel sentinel acd408cb9683b06e5786ecded9af3cb77b3bc33c 10.0.24.10 26381 @ mymaster 192.168.45.80 6380
22937:X 23 Oct 2022 14:54:02.309 * +sentinel sentinel 24381e0b70d72fdbb5489ee58a5db7e521dd7a54 10.0.24.10 26380 @ mymaster 192.168.45.80 6380
6. 哨兵验证
- 登录6380
/app/redis-6.2.6/src/redis-cli -p 6380 -a 123456
info replication
[root@localhost redis01]# /app/redis-6.2.6/src/redis-cli -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.45.76,port=6381,state=online,offset=2889706,lag=1
slave1:ip=192.168.45.76,port=6382,state=online,offset=2889706,lag=1
master_failover_state:no-failover
master_replid:0ee70287bead3e408a25ba421d918d7560f8fee3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2889972
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1841397
repl_backlog_histlen:1048576
127.0.0.1:6380>
登录6381
/app/redis-6.2.6/src/redis-cli -p 6380 -a 123456
info replication
[root@localhost redis01]# /app/redis-6.2.6/src/redis-cli -p 6381 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:192.168.45.76
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:2925735
slave_repl_offset:2925735
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0ee70287bead3e408a25ba421d918d7560f8fee3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2925735
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1877160
repl_backlog_histlen:1048576
127.0.0.1:6381> exit
登录6382
/app/redis-6.2.6/src/redis-cli -p 6380 -a 123456
info replication
[root@localhost redis01]# /app/redis-6.2.6/src/redis-cli -p 6382 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6382> info replication
# Replication
role:slave
master_host:192.168.45.76
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:2971130
slave_repl_offset:2971130
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0ee70287bead3e408a25ba421d918d7560f8fee3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2971130
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1922555
repl_backlog_histlen:1048576
127.0.0.1:6382>