redis-3.2.8 版本部署 哨兵模式
redis哨兵模式
master redis配置文件
[root@codis-server1 ~]# cat /etc/redis/6379.conf bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "/data/redis/log/redis_6379.log" databases 16 #always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis/6379" #replica-serve-stale-data yes #replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no #replica-priority 100 #lazyfree-lazy-eviction no #lazyfree-lazy-expire no #lazyfree-lazy-server-del no #replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes #aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 #stream-node-max-bytes 4096 #stream-node-max-entries 100 activerehashing yes #client-output-buffer-limit normal 0 0 0 #client-output-buffer-limit replica 64mb 32mb 60 #client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 #dynamic-hz yes aof-rewrite-incremental-fsync yes #rdb-save-incremental-fsync yes requirepass 123456 masterauth 123456
slave1 slave2 配置文件
[root@codis-server2 ~]# cat /etc/redis/6379.conf bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "/data/redis/log/redis_6379.log" databases 16 #always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis/6379" #replica-serve-stale-data yes #replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no #replica-priority 100 #lazyfree-lazy-eviction no #lazyfree-lazy-expire no #lazyfree-lazy-server-del no #replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes #aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 #stream-node-max-bytes 4096 #stream-node-max-entries 100 activerehashing yes #client-output-buffer-limit normal 0 0 0 #client-output-buffer-limit replica 64mb 32mb 60 #client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 #dynamic-hz yes aof-rewrite-incremental-fsync yes #rdb-save-incremental-fsync yes slaveof 192.168.96.161 6379 requirepass "123456" masterauth "123456
启动redis
[root@codis-server1 ~]# /etc/init.d/redis_6379 start
[root@mysql1 ~]# cat /usr/lib/systemd/system/redis.service [Unit] Description=Redis data structure server Documentation=https://redis.io/documentation After=network.target [Service] User=mysql Group=mysql Type=forking ExecStart=/data/redis/bin/redis-server /data/redis/etc/redis.conf LimitNOFILE=10032 #NoNewPrivileges=yes #Type=notify #TimeoutStartSec=infinity #TimeoutStopSec=infinity [Install] WantedBy=multi-user.target
配置 sentinel 配置文件(3个节点相同)
[root@codis-server1 ~]# cat /etc/redis/sentinel.conf bind 0.0.0.0 protected-mode no # 是否后台启动 daemonize yes protected-mode no # pid文件路径 pidfile "/var/run/redis-sentinel.pid" # 日志文件路径 logfile "/data/redis/log/sentinel.log" # 定义工作目录 dir "/data/redis/sentinel" sentinel monitor mymaster 192.168.96.161 6379 2 #设置密码 sentinel auth-pass mymaster 123456 # 如果mymaster 30秒内没有响应,则认为其主观失效 sentinel down-after-milliseconds mymaster 30000 # 如果master重新选出来后,其它slave节点能同时并行从新master同步数据的台数有多少个,显然该值越大,所有slave节点完成同步切换的整体速度越快,
但如果此时正好有人在访问这些slave,可能造成读取失败,影响面会更广。最保守的设置为1,同一时间,只能有一台干这件事,这样其它slave还能继续服务,但是所有slave全部完成缓存更新同步的进程将变慢。 sentinel parallel-syncs mymaster 1 # 该参数指定一个时间段,在该时间段内没有实现故障转移成功,则会再一次发起故障转移的操作,单位毫秒 sentinel failover-timeout mymaster 180000
sentinel 启动 顺序 master1 -- slave1 --- slave2
[root@codis-server1 ~]# redis-sentinel /etc/redis/sentinel.conf
使用 redis-cli 查询 sentinel 集群状态
查看master状态
[root@codis-server1 ~]# redis-cli -p 26379 127.0.0.1:26379> sentinel master mymaster 1) "name" 2) "mymaster" 3) "ip" 4) "192.168.96.161" 5) "port" 6) "6379" 7) "runid" 8) "59dae18716e06ee8504e8d7750105587300794a2" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "350" 19) "last-ping-reply" 20) "350" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "2074" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "62420" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1"
查看slaves 状态
127.0.0.1:26379> sentinel slaves mymaster 1) 1) "name" 2) "192.168.96.162:6379" 3) "ip" 4) "192.168.96.162" 5) "port" 6) "6379" 7) "runid" 8) "38c2151c22ed1744d493bd8a5e2af2ed53c6d89d" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "211" 19) "last-ping-reply" 20) "211" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "301" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "70664" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "192.168.96.161" 35) "master-port" 36) "6379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "13348" 2) 1) "name" 2) "192.168.96.163:6379" 3) "ip" 4) "192.168.96.163" 5) "port" 6) "6379" 7) "runid" 8) "1afdd22c3f6d91e3c4bf7276c6c9103c7922e1ec" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "211" 19) "last-ping-reply" 20) "211" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "301" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "70664" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "192.168.96.161" 35) "master-port" 36) "6379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "13348"
关闭master1 redis, 测试sentinel 是否切换
[root@codis-server1 ~]# /etc/init.d/redis_6379 stop
查看日志, +switch-master mymaster 192.168.96.161 6379 192.168.96.163 6379 ,已经切换到 192.168.96.163
[root@codis-server1 ~]# tail -f /data/redis/log/sentinel.log 1356:X 16 Mar 17:25:00.447 # Sentinel ID is a64d036e3a847295f69506b74b0441ab542f8535 1356:X 16 Mar 17:25:00.447 # +monitor master mymaster 192.168.96.161 6379 quorum 2 1356:X 16 Mar 17:27:20.386 # +sdown master mymaster 192.168.96.161 6379 1356:X 16 Mar 17:27:20.445 # +new-epoch 1 1356:X 16 Mar 17:27:20.446 # +vote-for-leader e4026a165372e633c4248801da7c03062b1671ba 1 1356:X 16 Mar 17:27:20.450 # +odown master mymaster 192.168.96.161 6379 #quorum 3/2 1356:X 16 Mar 17:27:20.450 # Next failover delay: I will not start a failover before Wed Mar 16 17:33:20 2022 1356:X 16 Mar 17:27:21.604 # +config-update-from sentinel e4026a165372e633c4248801da7c03062b1671ba 192.168.96.162 26379 @ mymaster 192.168.96.161 6379 1356:X 16 Mar 17:27:21.604 # +switch-master mymaster 192.168.96.161 6379 192.168.96.163 6379 1356:X 16 Mar 17:27:21.604 * +slave slave 192.168.96.162:6379 192.168.96.162 6379 @ mymaster 192.168.96.163 6379 1356:X 16 Mar 17:27:21.604 * +slave slave 192.168.96.161:6379 192.168.96.161 6379 @ mymaster 192.168.96.163 6379 1356:X 16 Mar 17:27:51.666 # +sdown slave 192.168.96.161:6379 192.168.96.161 6379 @ mymaster 192.168.96.163 6379
出现redis 不切换问题:
[root@master1]# telnet 192.168.96.162 6379
Escape character is '^]'. -DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication
password is requested to clients. In this mode connections are only accepted from the loopback interface.
If you want to connect from external computers to Redis you may adopt one of the following solutions:
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no'
from the loopback interface by connecting to Redis from the same host the server is running,
however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no',
and then restarting the server.
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside. Connection closed by foreign host.
建议处理方法如下:
每一台机子下的:redis.conf配置文件,还有哨兵的redis-sentinel.conf 配置文件修改成:
bind 0.0.0.0 protected-mode no