redis sentinel哨兵 例項

igcllq發表於2020-10-25

哨兵前提是建立主從
檔案詳解

#如果是編譯安裝,在原始碼目錄有sentinel.conf,複製到安裝目錄即可,
如:/apps/redis/etc/sentinel.conf
[root@centos8 ~]#vim /etc/redis-sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "sentinel_26379.log"
dir "/tmp"  #工作目錄
sentinel myid 50547f34ed71fd48c197924969937e738a39975b
sentinel monitor mymaster 10.0.0.8 6379 2 #指定當前mymaster叢集中master伺服器的地址和埠
#2為法定人數限制(quorum),即有幾個sentinel認為master down了就進行故障轉移,一般此值是所有
sentinel節點(一般總數是>=3的 奇數,如:3,5,7等)的一半以上的整數值,比如,總數是3,即
3/2=1.5,取整為2,是master的ODOWN客觀下線的依據
sentinel auth-pass mymaster 123456 #mymaster叢集中master的密碼,注意此行要在上面行的下面
sentinel down-after-milliseconds mymaster 30000 #(SDOWN)判斷mymaster叢集中所有節點的主觀下線的時間,單位:毫秒,建議3000
sentinel parallel-syncs mymaster 1 #發生故障轉移後,同時向新master同步資料的slave數量,數字越小總同步時間越長,但可以減輕新master的負載壓力
sentinel failover-timeout mymaster 180000 #所有slaves指向新的master所需的超時時間,單位:毫秒
sentinel deny-scripts-reconfig yes #禁止修改指令碼
logfile /var/log/redis/sentinel.log

配置哨兵檔案
檢視檔案下的配置

grep -vE "^#|^$" /etc/redis-sentinel.conf


port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 10.0.0.8 6379 2   #修改此行
sentinel auth-pass mymaster 123456 #增加此行
sentinel down-after-milliseconds mymaster 3000   #修改此行
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

#自動生成不需要建立
sentinel myid 50547f34ed71fd48c197924969937e738a39975b  #此行每個哨兵主機自動生成,且必須唯一

把配置好的檔案發給其餘兩個slave主機

scp /etc/redis-sentinel.conf 10.0.0.18:/etc/
scp /etc/redis-sentinel.conf 10.0.0.28:/etc/

啟動哨兵

[root@redis-master ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave1 ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave2 ~]#systemctl enable --now redis-sentinel.service

編譯安裝可以在所有哨兵伺服器執行下面操作啟動哨兵

#vim /apps/redis/etc/sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
Logfile "sentinel_26379.log"
dir "/apps/redis/data"
sentinel monitor mymaster 10.0.0.8 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

#/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf

驗證哨兵埠

[root@master ~]# ss -ntl
State                          Recv-Q                          Send-Q                                                    Local Address:Port                                                     Peer Address:Port                          
LISTEN                         0                               128                                                             0.0.0.0:6379                                                          0.0.0.0:*                             
LISTEN                         0                               128                                                             0.0.0.0:26379                                                         0.0.0.0:*                             
LISTEN                         0                               128                                                             0.0.0.0:22                                                            0.0.0.0:*                             
LISTEN                         0                               128                                                                [::]:26379                                                            [::]:*                             
LISTEN                         0                               128                                                                [::]:22                                                               [::]:*                             

驗證時候成功
停止master節點,master自動轉移到其他slave節點

[root@master ~]# redis-cli -h 10.0.0.18
10.0.0.18:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.28,port=6379,state=online,offset=460072,lag=1
slave1:ip=10.0.0.8,port=6379,state=online,offset=460072,lag=1
master_replid:7d337f59b297cebee92d682a49911eddd6979f82
master_replid2:758cca9443d71103241bd51236d4066c7aa3c234
master_repl_offset:460205
second_repl_offset:236496
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:460205

[root@master ~]# killall redis-server

10.0.0.8:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.28,port=6379,state=online,offset=548703,lag=1
master_replid:252b4cde6c43a4c0a8967f3fd7510219696cee9b
master_replid2:7d337f59b297cebee92d682a49911eddd6979f82
master_repl_offset:548835
second_repl_offset:533558
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:297314
repl_backlog_histlen:251522
10.0.0.8:6379> 

相關文章