Redis哨兵模式如何實現主從故障互切換-創(chuàng)新互聯(lián)

本篇文章為大家展示了Redis哨兵模式如何實現主從故障互切換,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

在龍華等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網站設計、成都網站制作 網站設計制作專業(yè)公司,公司網站建設,企業(yè)網站建設,成都品牌網站建設,成都全網營銷推廣,外貿營銷網站建設,龍華網站建設費用合理。

介紹

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

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

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

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

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

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

redis版本:3.0.7

主:6379  ,sentinel:26379

從:6380   ,sentinel:26380

配置

本章主要介紹怎樣搭建自動故障轉移的reids群集,當主宕機了從接替主成為新的主,宕機的主啟動后自動變成了從,其實它和Mysql的雙主模式是一樣的互為主從;redis群集需要用到redis-sentinel程序和sentinel.conf配置文件。

主配置

 vim redis.conf

daemonize yes
pidfile /usr/local/redis-6379/run/redis.pid
port 6379tcp-backlog 128timeout 0tcp-keepalive 0loglevel notice
logfile ""databases 16save 900 1    ###savesave 300 10save 60 10000stop-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 5repl-disable-tcp-nodelay no
slave-priority 100appendonly yes
appendfilename "appendonly.aof"appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yes
client-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes

 vim sentinel.conf

 群集文件配

port 26379dir "/usr/local/redis-6379"# 守護進程模式
daemonize yes
# 指明日志文件名
logfile "./sentinel.log"sentinel monitor mymaster 192.168.137.40 6379 1sentinel down-after-milliseconds mymaster 5000sentinel failover-timeout mymaster 18000sentinel auth-pass mymaster 123456

從配置

 vim redis.conf

daemonize yes
pidfile "/usr/local/redis-6380/run/redis.pid"port 6380tcp-backlog 128timeout 0tcp-keepalive 0loglevel notice
logfile ""databases 16save 900 1save 300 10save 60 10000stop-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 5repl-disable-tcp-nodelay no
slave-priority 100appendonly yes
appendfilename "appendonly.aof"appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yes
client-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes

vim sentinel.conf

#sentinel端口
port 26380#工作路徑,注意路徑不要和主重復
dir "/usr/local/redis-6380"# 守護進程模式
daemonize yes
# 指明日志文件名
logfile "./sentinel.log"#哨兵監(jiān)控的master,主從配置一樣,
sentinel monitor mymaster 192.168.137.40 6379 1# master或slave多長時間(默認30秒)不能使用后標記為s_down狀態(tài)。
sentinel down-after-milliseconds mymaster 5000#若sentinel在該配置值內未能完成failover操作(即故障時master/slave自動切換),則認為本次failover失敗。
sentinel failover-timeout mymaster 18000#設置master和slaves驗證密碼
sentinel auth-pass mymaster 123456

啟動redis

主從都要啟動

src/redis-server redis.conf

啟動群集監(jiān)控

主從都要啟動

src/redis-sentinel sentinel.conf --sentinel

Redis哨兵模式如何實現主從故障互切換

Redis哨兵模式如何實現主從故障互切換

啟動報錯處理

錯誤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, 表示內核允許分配所有的物理內存,而不管當前的內存狀態(tài)如何。2, 表示內核允許分配超過所有物理內存和交換空間總和的內存

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

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

Linux使用另外一種處理方式,它對大部分申請內存的請求都回復"yes",以便能跑更多更大的程序。因為申請內存后,并不會馬上使用內存。這種技術叫做Overcommit。
當內存不足時,會發(fā)生OOM killer(OOM=out-of-memory)。它會選擇殺死一些進程(用戶態(tài)進程,不是內核線程),以便釋放內存。Overcommit的策略Linux下overcommit有三種策略(Documentation/vm/overcommit-accounting):0. 啟發(fā)式策略。合理的overcommit會被接受,不合理的overcommit會被拒絕。1. 任何overcommit都會被接受。2. 當系統(tǒng)分配的內存超過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發(fā)生時,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:使用可以查看當前系統(tǒng)的所有限制值

vim /etc/security/limits.conf

在文件的末尾加上* soft nofile 65535* hard nofile 65535執(zhí)行su或者重新關閉連接用戶再執(zhí)行ulimit -a就可以查看修改后的結果。

故障切換機制

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

# Generated by CONFIG REWRITE
slaveof 192.168.137.40 6379

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

主:

port 26379dir "/usr/local/redis-6379"# 守護進程模式
daemonize yes
# 指明日志文件名
logfile "./sentinel.log"sentinel monitor mymaster 192.168.137.40 6379 1sentinel down-after-milliseconds mymaster 5000sentinel failover-timeout mymaster 18000sentinel auth-pass mymaster 123456# Generated by CONFIG REWRITE
sentinel config-epoch mymaster 0sentinel leader-epoch mymaster 1sentinel known-slave mymaster 192.168.137.40 6380sentinel 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"#哨兵監(jiān)控的master,主從配置一樣,在進行主從切換時6379會變成當前的master端口,sentinel monitor mymaster 192.168.137.40 6379 1# master或slave多長時間(默認30秒)不能使用后標記為s_down狀態(tài)。
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 0sentinel leader-epoch mymaster 1###指明了當前群集的從庫的ip和端口,在主從切換時該值會改變sentinel known-slave mymaster 192.168.137.40 6380###除了當前的哨兵還有哪些監(jiān)控的哨兵
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哨兵模式如何實現主從故障互切換

從哨兵配置文件中可以看到當前的主庫的已經發(fā)生了改變

Redis哨兵模式如何實現主從故障互切換

 從日志文件也可以看到當前的主已經從6379轉換成了6380

 redis配置文件官方說明:h t tp s:/ /r aw.g ithubusercon tent.com/antirez/redis/3.0/redis.conf

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

上述內容就是Redis哨兵模式如何實現主從故障互切換,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網站欄目:Redis哨兵模式如何實現主從故障互切換-創(chuàng)新互聯(lián)
URL分享:http://www.muchs.cn/article14/dhoide.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、品牌網站建設、App設計、網站導航、外貿建站面包屑導航

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網站建設網站維護公司