redis哨兵搭建
1、复制配置文件到conf
#单机安装以后
[root@t3 redis-5.0.8]# pwd
/app/redis-5.0.8
[root@t3 redis-5.0.8]# cp sentinel.conf ../redis/conf/
#复制三份
[root@t3 conf]# mv sentinel.conf sentinel_26379.conf
[root@t3 conf]# cp sentinel_26379.conf sentinel_26380.conf
[root@t3 conf]# cp sentinel_26379.conf sentinel_26381.conf
#日志文件创建三个
[root@t3 logs]# pwd
/app/redis/logs
[root@t3 logs]# touch {sentinel_26379.log,sentinel_26380.log,sentinel_26381.log}
#创建三个pid文件
[root@t3 pid]# pwd
/app/redis/pid
[root@t3 pid]# touch {sentinel_26379.pid,sentinel_26380.pid,sentinel_26381.pid}
2、更改配置文件
#更改26379的配置文件
bind 192.168.47.190
port 26379
daemonize yes
pidfile "/app/redis/pid/sentinel_26379.pid"
logfile "/app/redis/logs/sentinel_26379.log"
dir "/app/redis/data"
#sentinel myid d9730ea7211a7aa362e2ea9b8659f2ac83010473
sentinel deny-scripts-reconfig yes
#指向主服务器的IP端口
sentinel monitor mymaster 192.168.47.188 6379 2
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
protected-mode no
sentinel known-replica mymaster 192.168.47.189 6379
sentinel current-epoch 0
#26380的改端口,pid、log即可
bind 192.168.47.190
port 26380
daemonize yes
pidfile "/app/redis/pid/sentinel_26380.pid"
logfile "/app/redis/logs/sentinel_26380.log"
dir "/app/redis/data"
#sentinel myid d9730ea7211a7aa362e2ea9b8659f2ac83010473
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.47.188 6379 2
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
protected-mode no
sentinel known-replica mymaster 192.168.47.189 6379
sentinel current-epoch 0
#26381的改端口,pid、log即1
bind 192.168.47.190
port 26381
daemonize yes
pidfile "/app/redis/pid/sentinel_26381.pid"
logfile "/app/redis/logs/sentinel_26381.log"
dir "/app/redis/data"
#sentinel myid d9730ea7211a7aa362e2ea9b8659f2ac83010473
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.47.188 6379 2
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
protected-mode no
sentinel known-replica mymaster 192.168.47.189 6379
sentinel current-epoch 0
3、启动验证
3.1、启动
[root@t3 conf]# pwd
/app/redis/conf
[root@t3 conf]# ../bin/redis-sentinel sentinel_26379.conf
[root@t3 conf]# ../bin/redis-sentinel sentinel_26380.conf
[root@t3 conf]# ../bin/redis-sentinel sentinel_26381.conf
3.2、验证
#cli连接
[root@t3 bin]# ./redis-cli -h 192.168.47.190 -p 26381
#查看master信息
192.168.47.190:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.47.188"
5) "port"
6) "6379"
7) "runid"
8) "7ad0e4a8cede117bb1f7a2a7deb040a460e96a88"
9) "flags"
10) "master"
#查看slaves信息
192.168.47.190:26379> SENTINEL slaves mymaster
1) 1) "name"
2) "192.168.47.189:6379"
3) "ip"
4) "192.168.47.189"
5) "port"
6) "6379
#master的IP地址和端口
192.168.47.190:26379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.47.188"
2) "6379"
#查看sentinel信息看到有三个
192.168.47.190: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.47.188:6379,slaves=1,sentinels=3
#到此算是搭建完成拉,把master干掉看看能不能切换过去交给你们自己咯!
4、配置文件详解
# 绑定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
问题: -failover-abort-no-good-slave master mymaster 172.16.55.131 6379
已解决 所有的redis.conf 的bind 127.0.0.1 改为bind 0.0.0.0 从节点bind没有重新写 汗死 重启解决
日志:
23882:X 07 Jul 2020 17:26:27.285 # +new-epoch 3
23882:X 07 Jul 2020 17:26:27.285 # +try-failover master mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:27.286 # +vote-for-leader dc1a501112507435fe18cb8db2edbcb1b5117d10 3
23882:X 07 Jul 2020 17:26:27.289 # 7f5b2e6da8bede8dac1d5f970e9c81a64f63eff2 voted for dc1a501112507435fe18cb8db2edbcb1b5117d10 3
23882:X 07 Jul 2020 17:26:27.290 # 6e7620bb5aad69e54899dbc71838a841b4a63cf8 voted for dc1a501112507435fe18cb8db2edbcb1b5117d10 3
23882:X 07 Jul 2020 17:26:27.387 # +elected-leader master mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:27.387 # +failover-state-select-slave master mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:27.488 # +selected-slave slave 192.168.47.189:6379 192.168.47.189 6379 @ mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:27.488 * +failover-state-send-slaveof-noone slave 192.168.47.189:6379 192.168.47.189 6379 @ mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:27.572 * +failover-state-wait-promotion slave 192.168.47.189:6379 192.168.47.189 6379 @ mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:28.485 # +promoted-slave slave 192.168.47.189:6379 192.168.47.189 6379 @ mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:28.485 # +failover-state-reconf-slaves master mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:28.543 # +failover-end master mymaster 192.168.47.188 6379
23882:X 07 Jul 2020 17:26:28.543 # +switch-master mymaster 192.168.47.188 6379 192.168.47.189 6379
23882:X 07 Jul 2020 17:26:28.543 * +slave slave 192.168.47.188:6379 192.168.47.188 6379 @ mymaster 192.168.47.189 6379
23882:X 07 Jul 2020 17:26:58.559 # +sdown slave 192.168.47.188:6379 192.168.47.188 6379 @ mymaster 192.168.47.189 6379
###查看是否能切换
192.168.47.190:26379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.47.189"
2) "6379"
192.168.47.190:26379> SENTINEL get-master-addr-by-name mymaster1) "192.168.47.189"2) "6379"