創(chuàng)建三個(gè)Redis實(shí)例所需的目錄,生產(chǎn)環(huán)境需獨(dú)立部署在不同主機(jī)上,提高穩(wěn)定性。

Redis 哨兵模式(Sentinel)是一個(gè)自動(dòng)監(jiān)控處理 redis 間故障節(jié)點(diǎn)轉(zhuǎn)移工作的一個(gè)redis服務(wù)端實(shí)例,它不提供數(shù)據(jù)存儲(chǔ)服務(wù),只進(jìn)行普通 redis 節(jié)點(diǎn)監(jiān)控管理,使用redis哨兵模式可以實(shí)現(xiàn)redis服務(wù)端故障的自動(dòng)化轉(zhuǎn)移。
一、搭建redis主從集群
1、創(chuàng)建3個(gè)redis實(shí)例
關(guān)于redis的搭建,可以參考?xì)v史文章。
?https://mp.weixin.qq.com/s/RaWy0sqRxcAti1qbv-GbZQ?
如果有編譯好的二進(jìn)制文件,則直接部署redis實(shí)例即可。
創(chuàng)建三個(gè)redis實(shí)例所需的目錄,生產(chǎn)環(huán)境需獨(dú)立部署在不同主機(jī)上,提高穩(wěn)定性。
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個(gè)實(shí)例的目錄下
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實(shí)例不建議使用root賬號(hào)啟動(dòng),單獨(dú)創(chuàng)建一個(gè)redis用戶,并修改redis相關(guān)目錄的權(quán)限
useradd redis
chown -R redis:redis /data/redis
su - redis
# 啟動(dòng)三個(gè)redis實(shí)例
redis-server /data/redis/redis6379/redis.conf
redis-server /data/redis/redis6380/redis.conf
redis-server /data/redis/redis6381/redis.conf

2、配置主從同步
進(jìn)入2個(gè)實(shí)例,配置同步,配置完成后去主節(jié)點(diǎn)檢查一下是否正常。
[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>
可見(jiàn) 當(dāng)前主從同步已建立。
二、哨兵模式搭建
1、創(chuàng)建3個(gè)哨兵實(shí)例
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個(gè)實(shí)例
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
配置文件主要參數(shù)說(shuō)明:
參數(shù)名 | 說(shuō)明 |
bind | 綁定的可以訪問(wèn)的主機(jī)IP,0.0.0.0 代表不限制 |
port | 哨兵實(shí)例的端口 |
sentinel monitor testdb 127.0.0.1 6379 1 | testdb任意定義,哨兵集群名稱,127.0.0.1 6379 redis實(shí)例主節(jié)點(diǎn) ;1 代表當(dāng)1個(gè)哨兵實(shí)例判斷主庫(kù)不可用則進(jìn)行轉(zhuǎn)移,生產(chǎn)環(huán)境節(jié)點(diǎn)數(shù)要配置多一點(diǎn) |
sentinel down-after-milliseconds testdb 5000 | testdb同上,down-after-milliseconds代表 master 最長(zhǎng)響應(yīng)時(shí)間,超過(guò)這個(gè)時(shí)間就主觀判斷它下線,5000 代表5000ms,即5s |
sentinel auth-pass testdb 123456 | 123456 是redis實(shí)例的登錄密碼 |
2、啟動(dòng)哨兵實(shí)例
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
啟動(dòng)后配置文件會(huì)自動(dòng)新增如下紅框中的內(nèi)容。

登錄哨兵實(shí)例查看。

3、測(cè)試
測(cè)試將主節(jié)點(diǎn)down機(jī)。
redis-cli -p 6379 -a 123456 shutdown
再查看哨兵找那個(gè)的master結(jié)果,如下:

日志信息如下:
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端口啟動(dòng),可以看到節(jié)點(diǎn)自動(dòng)加入集群,且作為從節(jié)點(diǎn)。
