redis哨兵模式配置
本文配置节点为一主两从,三哨兵
1、官网下载Redis源码安装包,解压安装
# 下载
wget https://download.redis.io/releases/redis-6.2.5.tar.gz
# 拷贝
cp redis-6.2.5.tar.gz /usr/local/;cd /usr/local/
# 解压
tar -zxvf redis-6.2.5.tar.gz
cd /usr/local/redis-6.2.5
# 编译环境准备
yum install -y gcc gcc-c++
# 安装
make && make install
2、修改conf配置文件
2.1、拷贝配置文件
cp /usr/local/redis-6.2.5/redis.conf /usr/local/redis/bin/
cp /usr/local/redis-6.2.5/sentinel.conf /usr/local/redis/bin/
cd /usr/local/redis/bin/
cp redis.conf master.conf
cp sentinel.conf sentinel1.conf
2.2、修改master.conf(标红部分)
bind 0.0.0.0 -::1
protected-mode no
daemonize yes
pidfile "/var/run/redis_6379.pid"
logfile "/var/log/app_redis/master.log"
# 密码视情况而定,可以不配置
requirepass "123456"
# 配置密码,防止主节点发生故障哨兵切换主节点之后,旧的主节点重启无法链接新的主节点问题
masterauth "123456"
拷贝并配置从节点
cp master.conf slave1.conf
cp master.conf slave2.conf
2.4、修改slave1.conf:
port 6380
pidfile "/var/run/redis_6380.pid"
logfile "/var/log/app_redis/slave1.log"
replicaof 127.0.0.1 6379
2.5、修改slave2.conf:
port 6381
pidfile "/var/run/redis_6381.pid"
logfile "/var/log/app_redis/slave2.log"
replicaof 127.0.0.1 6379
2.6、修改哨兵配置sentinel1.conf:
protected-mode no
daemonize yes
pidfile /var/run/sentinel1.pid
logfile "/var/log/app_redis/sentinel1.log"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster 123456
拷贝哨兵配置
cp sentinel1.conf sentinel2.conf
cp sentinel1.conf sentinel3.conf
2.6、修改哨兵配置sentinel2.conf:
port 26380
pidfile /var/run/sentinel2.pid
logfile "/var/log/app_redis/sentinel2.log"
2.7、修改哨兵配置sentinel3.conf:
port 26381
pidfile /var/run/sentinel3.pid
logfile "/var/log/app_redis/sentinel3.log"
3、启动集群
# 启动主节点
./redis-server master.conf
# 启动从节点
./redis-server slave1.conf
./redis-server slave2.conf
# 启动哨兵实例
./redis-sentinel sentinel1.conf
./redis-sentinel sentinel2.conf
./redis-sentinel sentinel3.conf
4、验证集群
# 验证集群信息
./redis-cli -a 123456 -h 127.0.0.1 -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=7750371,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=7750509,lag=1
master_failover_state:no-failover
master_replid:08bb6c62ccfe1d925ec0ede3c4ed42590496cef3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7750509
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:6701934
repl_backlog_histlen:1048576
# 验证哨兵信息
./redis-cli -h 127.0.0.1 -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=81.69.226.166:6379,slaves=2,sentinels=3
注、如果集群部署在阿里云、腾讯云这些云服务器上,且客户端应用部署在其他机器上,则节点、哨兵需修改一下配置
修改节点配置:master.conf、slave1.conf、slave2.conf
replicaof 云服务外网IP 6379
修改哨兵配置:master.conf、slave1.conf、slave2.conf
sentinel monitor mymaster 云服务外网IP 6379 2
bind 云服务内网IP