Centos 7 安裝redis5.08 -Sentinel(哨兵)搭建實現Redis高可用集羣

概述


Redis-Sentinel哨兵機制爲Redis提供了高可用性,這意味着你可以使用哨兵模式創建一個可以不用人爲干預而能夠自動地應對各種故障的Redis部署。與此同時,Redis-Sentinel哨兵機制還爲Redis提供瞭如下功能:

監控:哨兵不斷地檢查master和slave是否正常運行。

通知:當被監控的某臺Redis實例出現故障時,哨兵可以通過API通知系統管理員和其他的應用程序。

自動故障轉移:如果一個master不正常運行了,哨兵可以啓動一個故障轉移進程,將一個slave升級成爲master,其他的 slave被重新配置使用新的master,並且應用程序使用Redis服務端通知的新地址。

配置提供者:哨兵作爲Redis客戶端發現的權威來源:客戶端連接到哨兵請求當前可靠的master的地址。如果發生故障,哨兵將報告新地址。

哨兵的分佈式特性


Redis哨兵是一個分佈式系統,哨兵自身被設計成和多個哨兵進程一起合作運行,有多個哨兵進程合作的好處有:

a. 當多個哨兵對一個master不再可用達成一致時纔會執行故障轉移,這會降低錯誤判斷的概率。

b. 即使在不是所有的哨兵都工作時哨兵也會工作,使系統健壯的抵抗故障,畢竟在故障系統裏單點故障沒有什麼意義。

redis安裝

一,將壓縮包放入/tools/soft 目錄並解壓到、tools目錄下

 tar -zxvf redis-5.0.8.tar.gz -C /tools/

二、更改目錄名

mv redis-5.0.8/ redis

三、進入redis目錄編譯

make MALLOC=libc

主從Replication的配置

安裝Redis-Sentinel集羣
Redis官網地址:https://redis.io/

Redis安裝參考:Redis介紹與安裝

Redis-Sentinel集羣配置

hostname IP地址 節點角色&端口
master 10.150.200.19 Master:6379 / Sentinel : 26379
slave1 10.150.200.16 Slave :6379 / Sentinel : 26379
slave2 10.150.200.17 Slave :6379 / Sentinel : 26379

 

 

 

redis.conf配置


Master主節點(10.150.200.19)的 redis.conf 配置如下:

protected-mode no
port 6379
timeout 0
tcp-keepalive 300
daemonize no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/tools/redis/logs/redis.log"
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 "/tools/redis/rdbs"
masterauth "123456"
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
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync no
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 -5
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value -2
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 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes


Slave從節點(10.150.200.16,10.150.200.17)與Master主節點(10.150.200.19)的 redis.conf 配置內容基本相同,只是額外增加了一行主節點的配置信息。

replicaof 10.150.200.19 6379

5.0一下版本爲

slaveof 10.150.200.19 6379


sentinel.conf配置


Master主節點(10.150.200.19)和Slave從節點(10.150.200.16,10.150.200.17)的 sentinel.conf 配置相同,內容如下:

protected-mode no
port 26379
daemonize yes
dir "/tools/redis/sentinel-tmp"
sentinel monitor redis-master 172.16.250.234 6379 2
# sentinel auth-pass <master-name> <password>
# 當在Redis實例中開啓了requirepass foobared 授權密碼 這樣所有連接Redis實例的客戶端都要提供密碼
# 設置哨兵sentinel 連接主從的密碼 注意必須爲主從設置一樣的驗證密碼
sentinel auth-pass redis-master 123456
# sentinel down-after-milliseconds <master-name> <milliseconds>
# 指定多少毫秒之後 主節點沒有應答哨兵sentinel 此時哨兵主觀上認爲主節點下線 默認30秒
sentinel down-after-milliseconds redis-master 30000
# sentinel failover-timeout <master-name> <milliseconds>
# 故障轉移的超時時間 failover-timeout 可以用在以下這些方面:
# 1. 同一個sentinel對同一個master兩次failover之間的間隔時間。
# 2. 當一個slave從一個錯誤的master那裏同步數據開始計算時間。直到slave被糾正爲向正確的master那裏同步數據時。
# 3.當想要取消一個正在進行的failover所需要的時間。
# 4.當進行failover時,配置所有slaves指向新的master所需的最大時間。不過,即使過了這個超時,slaves依然會被正確配置爲指向master,但是就不按parallel-syncs所配置的規則來了
# 默認三分鐘
sentinel failover-timeout redis-master 180000
# sentinel parallel-syncs <master-name> <numslaves>
# 指定在發生failover主備切換時最多可以有多少個slave同時對新的master進行同步,
# 這個數字越小,完成failover所需的時間就越長,
# 但是如果這個數字越大,就意味着越多的slave因爲replication而不可用。
# 可以通過將這個值設爲 1 來保證每次只有一個slave 處於不能處理命令請求的狀態。
sentinel parallel-syncs redis-master 1
# 配置當某一事件發生時所需要執行的腳本,可以通過腳本來通知管理員,例如當系統運行不正常時發郵件通知相關人員。
# 對於腳本的運行結果有以下規則:
# 若腳本執行後返回1,那麼該腳本稍後將會被再次執行,重複次數目前默認爲10
# 若腳本執行後返回2,或者比2更高的一個返回值,腳本將不會重複執行。
# 如果腳本在執行過程中由於收到系統中斷信號被終止了,則同返回值爲1時的行爲相同。
# 一個腳本的最大執行時間爲60s,如果超過這個時間,腳本將會被一個SIGKILL信號終止,之後重新執行。
#
# 通知型腳本:當sentinel有任何警告級別的事件發生時(比如說redis實例的主觀失效和客觀失效等等),將會去調用這個腳本,
# 這時這個腳本應該通過郵件,SMS等方式去通知系統管理員關於系統不正常運行的信息。調用該腳本時,將傳給腳本兩個參數,
# 一個是事件的類型,
# 一個是事件的描述。
# 如果sentinel.conf配置文件中配置了這個腳本路徑,那麼必須保證這個腳本存在於這個路徑,並且是可執行的,否則sentinel無法正常啓動成功。
# #通知腳本
# sentinel notification-script <master-name> <script-path>
# sentinel notification-script redis-master /var/redis/notify.sh
#
# 客戶端重新配置主節點參數腳本
# 當一個master由於failover而發生改變時,這個腳本將會被調用,通知相關的客戶端關於master地址已經發生改變的信息。
# 以下參數將會在調用腳本時傳給腳本:
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
# 目前<state>總是“failover”,
# <role>是“leader”或者“observer”中的一個。
# 參數 from-ip, from-port, to-ip, to-port是用來和舊的master和新的master(即舊的slave)通信的
# 這個腳本應該是通用的,能被多次調用,不是針對性的。
# sentinel client-reconfig-script <master-name> <script-path>
# sentinel client-reconfig-script redis-master /var/redis/reconfig.sh


配置完成之後,先創建好相應目錄,並使用如下命令分別啓動Redis和Sentinel集羣:

[root@master src]# mkdir ../logs
[root@master src]# mkdir ../rdbs
[root@master src]# mkdir ../sentinel-tmp
[root@master src]# ./redis-server ../redis.conf
[root@master src]# ./redis-sentinel ../sentinel.conf


 停止服務使用如下命令:

[root@master src]# pkill ./redis-server
[root@master src]# pkill ./redis-sentinel


 集羣啓動成功之後,登錄 Redis 客戶端查看節點的複製信息。

使用如下命令:

[root@master src]# ./redis-cli -h 10.150.200.19 -p 6379 -a 123456
172.16.250.234:6379> info replication


 Master 主節點(10.150.200.19)節點如下。

[root@miguvideo-9 src]# ./redis-cli -h 10.150.200.19 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.150.200.19:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.150.200.16,port=6379,state=online,offset=1194654,lag=1
slave1:ip=10.150.200.17,port=6379,state=online,offset=1194654,lag=1
master_replid:ac29dc620c380ced10b6298a5c3d91f2b6d2fa29
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1194944
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:146369
repl_backlog_histlen:1048576

Slave從節點(172.16.250.239)節點如下。

[root@miguvideo-9 src]# ./redis-cli -h 10.150.200.16 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.150.200.16:6379> info replication
# Replication
role:slave
master_host:10.150.200.19
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1185870
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ac29dc620c380ced10b6298a5c3d91f2b6d2fa29
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1185870
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:137295
repl_backlog_histlen:1048576
10.150.200.16:6379> 

參考鏈接:

https://blog.csdn.net/pengjunlee/article/details/78777508

https://www.jianshu.com/p/85115435fd24

https://blog.csdn.net/pengjunlee/article/details/81429119

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章