天天看點

redis哨兵模式(Sentinel)的搭建與配置

作者:資料庫幹貨鋪

Redis 哨兵模式(Sentinel)是一個自動監控處理 redis 間故障節點轉移工作的一個redis服務端執行個體,它不提供資料存儲服務,隻進行普通 redis 節點監控管理,使用redis哨兵模式可以實作redis服務端故障的自動化轉移。

1. 搭建redis主從叢集

1.1 建立3個redis執行個體

關于redis的搭建,可以參考曆史文章

https://mp.weixin.qq.com/s/RaWy0sqRxcAti1qbv-GbZQ

如果有編譯好的二進制檔案,則直接部署redis執行個體即可。

建立三個redis執行個體所需的目錄,生産環境需獨立部署在不同主機上,提高穩定性

mkdir   -p /data/redis
cd /data/redis/
mkdir  redis6379  redis6380  redis6381
cd redis6379
vim redis.conf


#  添加如下配置
bind  0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 30
tcp-keepalive 300
daemonize yes
supervised no
pidfile /data/redis/redis6379/redis_6379.pid
loglevel notice
logfile "/data/redis/redis6379/redis6379.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/redis6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes


# 将配置檔案拷貝到其他2個執行個體的目錄下




cp redis.conf  ../redis6380.conf
cp redis.conf ../redis6381.conf
sed -i  "s#6379#6380#g"  ../redis6380/redis.conf
sed -i  "s#6379#6381#g"  ../redis6381/redis.conf
# redis執行個體不建議使用root賬号啟動,單獨建立一個redis使用者,并修改redis相關目錄的權限
useradd redis
chown -R   redis:redis /data/redis
 su - redis
#  啟動三個redis執行個體
redis-server  /data/redis/redis6379/redis.conf
redis-server  /data/redis/redis6380/redis.conf
redis-server  /data/redis/redis6381/redis.conf

           
redis哨兵模式(Sentinel)的搭建與配置

1.2 配置主從同步

進入2個執行個體,配置同步,配置完成後去主節點檢查一下是否正常

[redis@test redis6379]$ redis-cli -p 6380 -a "123456"
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6380> slaveof 127.0.0.1 6379
OK
127.0.0.1:6380> exit
[redis@test redis6379]$ redis-cli -p 6381 -a "123456"
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6381> slaveof 127.0.0.1 6379
OK
127.0.0.1:6381> exit
[redis@test redis6379]$ redis-cli -p 6379 -a "123456"
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=42,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=42,lag=1
master_replid:b8a19f5afae13d3da38b359244dc0f560df03176
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
127.0.0.1:6379>            

可見 目前主從同步已建立。

2. 哨兵模式搭建

2.1 建立3個哨兵執行個體

mkdir -p   /data/redis/redis_sentinel/
cd /data/redis/redis_sentinel/
mkdir sentinel26379 sentinel26380 sentinel26381
cd sentinel26379
 # 初始化配置檔案如下 
vim  redis_sentinel_26379.conf
bind 0.0.0.0
port 26379
daemonize yes
dir "/data/redis/redis_sentinel/sentinel26379"
pidfile "/data/redis/redis_sentinel/sentinel26379/redis_sentinel26379.pid"
logfile "/data/redis/redis_sentinel/sentinel26379/redis_sentinel26379.log"


# Generated by CONFIG REWRITE
sentinel deny-scripts-reconfig yes
sentinel monitor testdb 127.0.0.1 6379 2
sentinel down-after-milliseconds testdb 5000
sentinel auth-pass testdb 123456


# 配置檔案拷貝至另2個執行個體
cp  redis_sentinel_26379.conf   ../sentinel26380/redis_sentinel_26380.conf 
sed -i  "s#26379#26380#g"  ../sentinel26380/redis_sentinel_26380.conf 
cp  redis_sentinel_26379.conf   ../sentinel26381/redis_sentinel_26381.conf 
sed -i "s#26379#26381#g"  ../sentinel26381/redis_sentinel_26381.conf           

配置檔案主要參數說明:

參數名 說明
bind 綁定的可以通路的主機IP,0.0.0.0 代表不限制
port 哨兵執行個體的端口
sentinel monitor testdb 127.0.0.1 6379 1 testdb任意定義,哨兵叢集名稱,127.0.0.1 6379 redis執行個體主節點 ;1 代表當1個哨兵執行個體判斷主庫不可用則進行轉移,生産環境節點數要配置多一點
sentinel down-after-milliseconds testdb 5000 testdb同上,down-after-milliseconds代表 master 最長響應時間,超過這個時間就主觀判斷它下線,5000 代表5000ms,即5s
sentinel auth-pass testdb 123456 123456 是redis執行個體的登入密碼

2.2 啟動哨兵執行個體

redis-sentinel  /data/redis/redis_sentinel/sentinel26379/redis_sentinel_26379.conf 
redis-sentinel  /data/redis/redis_sentinel/sentinel26380/redis_sentinel_26380.conf 
redis-sentinel  /data/redis/redis_sentinel/sentinel26381/redis_sentinel_26381.conf            

啟動後配置檔案會自動新增如下紅框中的内容

redis哨兵模式(Sentinel)的搭建與配置

登入哨兵執行個體檢視

redis-cli -p  26379           
redis哨兵模式(Sentinel)的搭建與配置

2.3 測試

測試将主節點down機

redis-cli -p 6379 -a 123456  shutdown           

再檢視哨兵找那個的master結果,如下:

redis哨兵模式(Sentinel)的搭建與配置

日志資訊如下:

1895:X 29 Apr 23:57:31.778 # +sdown master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.779 # +odown master testdb 127.0.0.1 6379 #quorum 1/1
1895:X 29 Apr 23:57:31.779 # +new-epoch 1
1895:X 29 Apr 23:57:31.779 # +try-failover master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.795 # +vote-for-leader 4928b4d4dfd762cd50fa540b7a0903d2be3b0f95 1
1895:X 29 Apr 23:57:31.796 # +elected-leader master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.796 # +failover-state-select-slave master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.862 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.862 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:31.991 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:32.223 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:32.223 # +failover-state-reconf-slaves master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:32.273 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.231 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.231 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.296 # +failover-end master testdb 127.0.0.1 6379
1895:X 29 Apr 23:57:33.296 # +switch-master testdb 127.0.0.1 6379 127.0.0.1 6380
1895:X 29 Apr 23:57:33.297 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ testdb 127.0.0.1 6380
1895:X 29 Apr 23:57:33.297 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ testdb 127.0.0.1 6380
1895:X 29 Apr 23:57:38.356 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ testdb 127.0.0.1 6380

           

再把6379端口啟動,可以看到節點自動加入叢集,且作為從節點

redis哨兵模式(Sentinel)的搭建與配置

繼續閱讀