Redis哨兵模式實現主從故障互切換的方法

Redis哨兵模式實現主從故障互切換的方法

redis Sentinel 是一個分布式系統, 你可以在一個架構中運行多個 Sentinel 進程(progress), 這些進程使用流言協議(gossip protocols)來接收關于主服務器是否下線的信息, 并使用投票協議(agreement protocols)來決定是否執行自動故障遷移, 以及選擇哪個從服務器作為新的主服務器。

雖然 Redis Sentinel 釋出為一個單獨的可執行文件 redis-sentinel , 但實際上它只是一個運行在特殊模式下的 Redis 服務器, 你可以在啟動一個普通 Redis 服務器時通過給定 –sentinel 選項來啟動 Redis Sentinel 。

Sentinel 系統用于管理多個 Redis 服務器(instance), 該系統執行以下三個任務:

1、監控(Monitoring): Sentinel 會不斷地檢查你的主服務器和從服務器是否運作正常。

2、提醒(Notification): 當被監控的某個 Redis 服務器出現問題時, Sentinel 可以通過 API 向管理員或者其他應用程序發送通知。

3、自動故障遷移(Automatic failover): 當一個主服務器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將失效主服務器的其中一個從服務器升級為新的主服務器, 并讓失效主服務器的其他從服務器改為復制新的主服務器; 當客戶端試圖連接失效的主服務器時, 集群也會向客戶端返回新主服務器的地址, 使得集群可以使用新主服務器代替失效服務器。

配置

當主宕機了從接替主成為新的主,宕機的主啟動后自動變成了從,其實它和Mysql的雙主模式是一樣的互為主從;redis哨兵需要用到redis-sentinel程序和sentinel.conf配置文件。

mkdir?-p?/usr/local/redis mkdir?-p?/usr/local/redis/6379 mkdir?-p?/usr/local/redis/6380 mkdir?-p?/usr/local/redis/redis_cluster

主配置

?vim?redis_6379.conf

daemonize?yes pidfile?/usr/local/redis/6379/redis_6379.pid port?6379 tcp-backlog?128 timeout?0 tcp-keepalive?0 loglevel?notice logfile?"" databases?16 save?900?1????###save save?300?10 save?60?10000 stop-writes-on-bgsave-error?yes rdbcompression?yes rdbchecksum?yes dbfilename?dump.rdb???###dbfile dir?"/usr/local/redis/6379" masterauth?"123456" requirepass?"123456" slave-serve-stale-data?yes slave-read-only?yes repl-diskless-sync?no repl-diskless-sync-delay?5 repl-disable-tcp-nodelay?no slave-priority?100 appendonly?yes 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 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-entries?512 list-max-ziplist-value?64 set-max-intset-entries?512 zset-max-ziplist-entries?128 zset-max-ziplist-value?64 hll-sparse-max-bytes?3000 activerehashing?yes client-output-buffer-limit?normal?0?0?0 client-output-buffer-limit?slave?256mb?64mb?60 client-output-buffer-limit?pubsub?32mb?8mb?60 hz?10 aof-rewrite-incremental-fsync?yes

?vim sentinel_1.conf

?哨兵文件配置

port?6000 dir?"/usr/local/redis/sentinel" #?守護進程模式 daemonize?yes protected-mode?no logfile?"/usr/local/sentinel/sentinel.log"

從配置

?vim?redis_6380.conf

daemonize?yes pidfile?"/usr/local/redis/6380/redis_6380.pid" port?6380 tcp-backlog?128 timeout?0 tcp-keepalive?0 loglevel?notice logfile?"" databases?16 save?900?1 save?300?10 save?60?10000 stop-writes-on-bgsave-error?yes rdbcompression?yes rdbchecksum?yes dbfilename?"dump.rdb" dir?"/usr/local/redis/6380" masterauth?"123456" requirepass?"123456" slave-serve-stale-data?yes slave-read-only?yes repl-diskless-sync?no repl-diskless-sync-delay?5 repl-disable-tcp-nodelay?no slave-priority?100 appendonly?yes 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 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-entries?512 list-max-ziplist-value?64 set-max-intset-entries?512 zset-max-ziplist-entries?128 zset-max-ziplist-value?64 hll-sparse-max-bytes?3000 activerehashing?yes client-output-buffer-limit?normal?0?0?0 client-output-buffer-limit?slave?256mb?64mb?60 client-output-buffer-limit?pubsub?32mb?8mb?60 hz?10 aof-rewrite-incremental-fsync?yes

vim sentinel_2.conf

#sentinel端口 port?6000 #工作路徑,注意路徑不要和主重復 dir?"/usr/local/sentinel" #?守護進程模式 daemonize?yes protected-mode?no #?指明日志文件名 logfile?"/usr/local/sentinel/sentinel.log"

注意:1.應用程序連接到哨兵端口,通過指定不同的master名稱連接到具體的主副本。2.哨兵配置文件中只需要配置主從復制中的主副本ip和端口即可,當主從進行切換時哨兵會自動修改哨兵配置文件中的主副本ip為新在主副本ip。3.一個哨兵配置文件中可以同時配置監控多個主從復制。4.單個哨兵就可以用來進行主從故障監控,但是如果只有一個sentinel進程,如果這個進程運行出錯,或者是網絡堵塞,那么將無法實現redis集群的主備切換(單點問題);這個2代表投票數,當2個sentinel認為一個master已經不可用了以后,將會觸發failover,才能真正認為該master已經不可用了。(sentinel集群中各個sentinel也有互相通信,通過gossip協議);所以合理的配置應該是同時啟動多個哨兵進程,并且最好是在不同的服務器中啟動。5.注意mymaster的需要在整個網絡環境都是唯一的,哨兵之間會自動通過mastername去建立關聯關系只要網絡環境是相通的。

啟動redis

1.主從都要啟動

src/redis-server?redis.conf

2.登入到6380建立主從關系

redis-cli?-p?6380 slaveof?192.168.137.40?6379

配置哨兵

主從兩個哨兵都要啟動,還可以通過redis-server方式啟動,例如“redis-server sentinel.conf –sentinel”

1.啟動哨兵

src/redis-sentinel?sentinel.conf

2.登入哨兵(兩臺哨兵都需要登入執行),添加主從監控信息

redis-cli -p 6000

sentinel?monitor?mymaster?192.168.137.40?6379?2 sentinel?set?mymaster?down-after-milliseconds?5000 sentinel?set?mymaster?failover-timeout?15000 sentinel?set?mymaster?auth-pass?123456

啟動報錯處理

錯誤1:

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.

兩個解決方法(overcommit_memory)

1.? echo “vm.overcommit_memory=1” > /etc/sysctl.conf? 或 vi /etcsysctl.conf , 然后reboot重啟機器

2.? echo 1 > /proc/sys/vm/overcommit_memory? 不需要啟機器就生效

overcommit_memory參數說明:

設置內存分配策略(可選,根據服務器的實際情況進行設置)

/proc/sys/vm/overcommit_memory

可選值:0、1、2。

0, 表示內核將檢查是否有足夠的可用內存供應用進程使用;如果有足夠的可用內存,內存申請允許;否則,內存申請失敗,并把錯誤返回給應用進程。

1, 表示內核允許分配所有的物理內存,而不管當前的內存狀態如何。

2, 表示內核允許分配超過所有物理內存和交換空間總和的內存

注意:redis在dump數據的時候,會fork出一個子進程,理論上child進程所占用的內存和parent是一樣的,比如parent占用 的內存為8G,這個時候也要同樣分配8G的內存給child,如果內存無法負擔,往往會造成redis服務器的down機或者IO負載過高,效率下降。所 以這里比較優化的內存分配策略應該設置為 1(表示內核允許分配所有的物理內存,而不管當前的內存狀態如何)。

這里又涉及到Overcommit和OOM。

什么是Overcommit和OOM

在Unix中,當一個用戶進程使用malloc()函數申請內存時,假如返回值是NULL,則這個進程知道當前沒有可用內存空間,就會做相應的處理工作。許多進程會打印錯誤信息并退出。

Linux使用另外一種處理方式,它對大部分申請內存的請求都回復”yes”,以便能跑更多更大的程序。因為申請內存后,并不會馬上使用內存。這種技術叫做Overcommit。

當內存不足時,會發生OOM killer(OOM=out-of-memory)。它會選擇殺死一些進程(用戶態進程,不是內核線程),以便釋放內存。

Overcommit的策略

Linux下overcommit有三種策略(Documentation/vm/overcommit-accounting):

0. 啟發式策略。合理的overcommit會被接受,不合理的overcommit會被拒絕。

1. 任何overcommit都會被接受。

2. 當系統分配的內存超過swap+N%*物理RAM(N%由vm.overcommit_ratio決定)時,會拒絕commit。

overcommit的策略通過vm.overcommit_memory設置。

overcommit的百分比由vm.overcommit_ratio設置。

# echo 2 > /proc/sys/vm/overcommit_memory

# echo 80 > /proc/sys/vm/overcommit_ratio

當oom-killer發生時,linux會選擇殺死哪些進程

選擇進程的函數是oom_badness函數(在mm/oom_kill.c中),該函數會計算每個進程的點數(0~1000)。

點數越高,這個進程越有可能被殺死。

每個進程的點數跟oom_score_adj有關,而且oom_score_adj可以被設置(-1000最低,1000最高)。

錯誤2:
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

echo?511?>?/proc/sys/net/core/somaxconn

錯誤3:

16433:X 12 Jun 14:52:37.734 * Increased maximum number of open files to 10032 (it was originally set to 1024).

新裝的linux默認只有1024,當負載較大時,會經常出現error: too many open files

ulimit -a:使用可以查看當前系統的所有限制值

vim?/etc/security/limits.conf

在文件的末尾加上

*?soft?nofile?65535 *?hard?nofile?65535

執行su或者重新關閉連接用戶再執行ulimit -a就可以查看修改后的結果。

故障切換機制

1. 啟動群集后,群集程序默認會在從庫的redis文件中加入連接主的配置

#?Generated?by?CONFIG?REWRITE slaveof?192.168.137.40?6379

2.啟動群集之后,群集程序默認會在主從的sentinel.conf文件中加入群集信息

主:

port?26379 dir?"/usr/local/redis-6379" #?守護進程模式 daemonize?yes #?指明日志文件名 logfile?"./sentinel.log" sentinel?monitor?mymaster?192.168.137.40?6379?1 sentinel?down-after-milliseconds?mymaster?5000 sentinel?failover-timeout?mymaster?18000 sentinel?auth-pass?mymaster?123456 #?Generated?by?CONFIG?REWRITE sentinel?config-epoch?mymaster?0 sentinel?leader-epoch?mymaster?1 sentinel?known-slave?mymaster?192.168.137.40?6380 sentinel?known-sentinel?mymaster?192.168.137.40?26380?c77c5f64aaad0137a228875e531c7127ceeb5c3f sentinel?current-epoch?1

從:

#sentinel端口 port?26380 #工作路徑 dir?"/usr/local/redis-6380" #?守護進程模式 daemonize?yes #?指明日志文件名 logfile?"./sentinel.log" #哨兵監控的master,主從配置一樣,在進行主從切換時6379會變成當前的master端口, sentinel?monitor?mymaster?192.168.137.40?6379?1 #?master或slave多長時間(默認30秒)不能使用后標記為s_down狀態。 sentinel?down-after-milliseconds?mymaster?5000 #若sentinel在該配置值內未能完成failover操作(即故障時master/slave自動切換),則認為本次failover失敗。 sentinel?failover-timeout?mymaster?18000 #設置master和slaves驗證密碼 sentinel?auth-pass?mymaster?123456 #哨兵程序自動添加的部分 #?Generated?by?CONFIG?REWRITE sentinel?config-epoch?mymaster?0 sentinel?leader-epoch?mymaster?1 ###指明了當前群集的從庫的ip和端口,在主從切換時該值會改變 sentinel?known-slave?mymaster?192.168.137.40?6380 ###除了當前的哨兵還有哪些監控的哨兵 sentinel?known-sentinel?mymaster?192.168.137.40?26379?7a88891a6147e202a53601ca16a3d438e9d55c9d sentinel?current-epoch?1

模擬主故障

[root@monitor?redis-6380]#?ps?-ef|grep?redis root???????4171??????1??0?14:20??????????00:00:15?/usr/local/redis-6379/src/redis-server?*:6379?????????????????????????? root???????4175??????1??0?14:20??????????00:00:15?/usr/local/redis-6380/src/redis-server?*:6380?????????????????????????? root???????4305??????1??0?15:28??????????00:00:05?/usr/local/redis-6379/src/redis-sentinel?*:26379?[sentinel]???????????????????????????? root???????4306??????1??0?15:28??????????00:00:05?/usr/local/redis-6380/src/redis-sentinel?*:26380?[sentinel]???????????????????????????? root???????4337???4144??0?15:56?pts/1????00:00:00?grep?redis [root@monitor?redis-6380]#?kill?-9?4171 [root@monitor?redis-6380]#?ps?-ef|grep?redis root???????4175??????1??0?14:20??????????00:00:15?/usr/local/redis-6380/src/redis-server?*:6380?????????????????????????? root???????4305??????1??0?15:28??????????00:00:05?/usr/local/redis-6379/src/redis-sentinel?*:26379?[sentinel]???????????????????????????? root???????4306??????1??0?15:28??????????00:00:05?/usr/local/redis-6380/src/redis-sentinel?*:26380?[sentinel]???????????????????????????? root???????4339???4144??0?15:56?pts/1????00:00:00?grep?redis [root@monitor?redis-6380]#

Redis哨兵模式實現主從故障互切換的方法Redis哨兵模式實現主從故障互切換的方法從哨兵配置文件中可以看到當前的主庫的已經發生了改變

Redis哨兵模式實現主從故障互切換的方法

總結

redis的哨兵端口26379、26380使用客戶端軟件無法連接,使用程序可以連接,客戶端軟件只能直接連接6379和6380端口。使用哨兵監控當主故障后會自動切換從為主,當主啟動后就變成了從。有看到別人只配置單哨兵26379的這種情況,這種情況無法保證哨兵程序自身的高可用。

更多redis知識請關注redis數據庫教程欄目。

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享