一、redis哨兵搭建
1.1、复制sentinel配置文件到conf
# 三台机器均这样操作
[root@masternode1 ~]# cd /app/redis-5.0.8 && cp sentinel.conf ../redis/conf/
[root@masternode1 redis-5.0.8]# cd /app/redis/logs && touch sentinel_26379.log
[root@masternode1 logs]# cd /app/redis/pid && touch sentinel_26379.pid
1.2、三台配置文件如下(bind修改为本机ip即可)
[root@masternode1 conf]# cat sentinel.conf | grep -v "#" | grep -v "^$"
bind 192.168.1.160 # bind参数修改为本机IP
port 26379 # 绑定端口
daemonize yes # 后台运行
pidfile "/app/redis/pid/sentinel_26379.pid" # pid目录
logfile "/app/redis/logs/sentinel_26379.log" # 日志目录
dir "/app/redis/data" # 数据目录
sentinel monitor mymaster 192.168.1.160 6379 2 # 设置 主名称 ip地址 端口号 参入选举的哨兵数
sentinel auth-pass mymaster a123456 # 在Redis实例中开启了requirepass,连接Redis实例的客户端都要提供密码
sentinel down-after-milliseconds mymaster 30000 # 定主节点应答哨兵sentinel的最大时间间隔,超过这个时间,哨兵主观上认为主节点下线,默认30秒
sentinel parallel-syncs mymaster 1 # 发生failover主备切换时最多可以有几个slave同时对新的master进行同步
sentinel failover-timeout mymaster 180000 # 故障转移时间,默认3分钟
sentinel deny-scripts-reconfig yes # 客户端在运行时重新配置脚本
[root@slavenode1 conf]# cat /app/redis/conf/sentinel.conf
bind 192.168.1.161
port 26379
daemonize yes
pidfile "/app/redis/pid/sentinel_26379.pid"
logfile "/app/redis/logs/sentinel_26379.log"
dir "/app/redis/data"
sentinel monitor mymaster 192.168.1.160 6379 2
sentinel auth-pass mymaster a123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
1.3、启动sentinel
[root@masternode1 conf]# cd /app/redis/conf
[root@masternode1 conf]# ../bin/redis-sentinel sentinel_26379.conf
1.4、端口检查
[root@masternode1 ~]# ss -ntl | grep 6379
LISTEN 0 128 192.168.1.160:26379 *:*
LISTEN 0 128 *:6379 *:*
[root@slavenode2 conf]# ss -ntl | grep 6379
LISTEN 0 128 192.168.1.162:26379 *:*
LISTEN 0 128 *:6379 *:*
[root@slavenode1 conf]# ss -ntl | grep 6379
LISTEN 0 128 192.168.1.161:26379 *:*
LISTEN 0 128 *:6379 *:*
1.5、验证
# 查看master信息(只贴出了部分)
[root@masternode1 bin]# ./redis-cli -h 192.168.1.160 -p 26379
192.168.1.160:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.1.160"
5) "port"
6) "6379"
7) "runid"
8) "da2519240392085df126aadd6a9034aa851b7966"
9) "flags"
10) "master
# 查看slaves信息(只贴出了部分)
192.168.1.160:26379> SENTINEL slaves mymaster
1) 1) "name"
2) "192.168.1.162:6379"
3) "ip"
4) "192.168.1.162"
5) "port"
6) "6379"
7) "runid"
8) "23e1cf0d0e5e54799e3a84b6527d46b2b7a704a7"
9) "flags"
10) "slave"
2) 1) "name"
2) "192.168.1.161:6379"
3) "ip"
4) "192.168.1.161"
5) "port"
6) "6379"
7) "runid"
8) "ab961c3c2113b71ef9922a07be3c44d337c8f07b"
9) "flags"
10) "slave"
# 查看master的IP地址和端口
192.168.1.160:26379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.1.160"
2) "6379"
# 查看sentinel信息
192.168.1.160: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=192.168.1.160:6379,slaves=2,sentinels=3
1.6、配置文件详解
# 绑定IP地址
bind 192.168.47.190
#此Sentinel实例运行的端口
port 26379
#后台运行yes
daemonize yes
#pid文件路径
pidfile "/app/redis/pid/sentinel_26379.pid"
#日志文件路径
logfile "/app/redis/logs/sentinel_26379.log"
#工作目录
dir "/app/redis/data"
#因为配置文件是复制的这个参数都是一样的,启动sentinel检查发现一个一sentinel(注释掉)
#sentinel myid d9730ea7211a7aa362e2ea9b8659f2ac83010473
sentinel deny-scripts-reconfig yes
##mymaster是集群的名称可自定义,IP地为集群中master的地址,注意与bind的区别 6379表示端口 2表示 需要多少哨兵同意才能执行故障转移操作
sentinel monitor mymaster 192.168.47.188 6379 2
#如果redis配置了密码,那这里必须配置认证,否则不能自动切换
sentinel auth-pass mymaster 123456
#哨兵程序自动添加的部分
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# 保护模式(是否禁止外部链接,除绑定的ip地址外)
protected-mode no
#指明了当前群集的从库的ip和端口,在主从切换时该值会改变
sentinel known-replica mymaster 192.168.47.189 6379
#除了当前的哨兵还有哪些监控的哨兵
sentinel current-epoch 0