redis-3.2.8 版本部署 哨兵模式
阿新 • • 發佈:2022-03-16
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 save900 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 10000slowlog-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
配置 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