哨兵+redis主從部署架構-docker部署

架構圖

  1. 哨兵的介紹
    sentinel , 中文是哨兵。
    哨兵是redis 集群架構中非常重要的一個組件,主要功能如下:
    (1)集群監控:負責監控reidis master 和slave 進程是否正常工作;
    (2)消息通知:如果某個redis實例有故障,那麼哨兵負責發送消息作為報警通知給管理員;
    (3)故障轉移:如果master node掛掉了,會自動轉移到slave node上;
    (4)配置中心:如果故障轉移發生了,通知client 客戶端新的master 地址;
    哨兵本身也是分佈式的作為一個哨兵集群去運行,互相協同工作;
    (1)故障轉移時, 判斷一個master node宕機了,需要大部分的哨兵都同意才行,涉及到了分佈式選舉的問題;
    (2)即使部分哨兵節點掛了,哨兵集群還是能正常工作的,因為如果 一個作為高可用機制重要組成部分的故障轉移系統本身是單點的,那就坑爹了;目前採用的是sentinel 2 版本,sentinel2 相對於 sentinel 1 來說,重寫了很多代碼,主要是讓故障轉移的機制和算法變得更加健壯和簡單。
  2. 哨兵的核心知識
    (1)哨兵至少需要3個實例,來保證自己的健壯性;
    (2)哨兵+redis 主從的部署架構,是不會保證數據0丟失的,只能保證redis 集群的高可用性;
    (3)對於哨兵+redis主從這種負責的部署架構,盡量在測試環境和生產環境,都進行充足的測試和演練

部署

準備docker環境

hostnamectl set-hostname  l-gz-txy-lyy-uat-redis-master-001

sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    //download.docker.com/linux/centos/docker-ce.repo
	
sudo yum install docker-ce docker-ce-cli containerd.io -y
systemctl start docker
systemctl enable docker

三台虛擬機

10.180.3.125:7000 主節點
10.180.3.181:7000 從節點
10.180.3.90:7000 從節點

主節點創建redis data和conf目錄

mkdir -p /data/redis/7000
mkdir /data/redis/7000/conf
mkdir /data/redis/7000/data
cd /data/redis/7000/conf

修改redis.conf主要配置

cat > /data/redis/7000/conf/redis.conf <<EOF
port 7000
masterauth "YACEheyuan@123"
requirepass "YACEheyuan@123"
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
#logfile "/var/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
 repl-timeout 1
repl-disable-tcp-nodelay no
replica-priority 100
maxmemory 1024000kb
maxmemory-policy volatile-lru
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled no
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 -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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
EOF

配置sentinel


cat >/data/redis/7000/conf/sentinel.conf <<EOF
port 27000
daemonize yes
sentinel monitor mymaster 10.180.3.125 7000 1
sentinel auth-pass mymaster YACEheyuan@123
EOF

拷貝整個/data/redis目錄到備節點

scp -r /data/redis [email protected]:/data/

主從節點都啟動redis容器

cat >start.sh<<EOF
docker run -d -ti -p 7000:7000 -p 27000:27000 \
-v /data/redis/7000/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /data/redis/7000/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
-v /data/redis/7000/data:/data \
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--privileged=true --restart always --name redis_7000 \
uhub.service.ucloud.cn/jackhe/redis-sentinel:latest redis-server /usr/local/etc/redis/redis.conf
EOF
chmod +x start.sh
sh start.sh

從節點進去容器,設置自己為從節點

docker exec -it  redis_7000  bash
redis-cli -h 127.0.0.1 -c -p 7000 -a YACEheyuan@123
SLAVEOF 10.180.3.125 7000

分別進去主從節點的容器,啟動sentinel
redis-sentinel /usr/local/etc/redis/sentinel.conf

輸入info查看是否設置成功

image-20220107144302245

啟動完成後,關閉主節點redis容器,等待30秒查看備節點是否切換為主節點
啟動回主節點redis容器,查看主節點是否已經作為備節點連接上,同時啟動sentinel

部署邏輯

docker 啟動redis——》從節點聲明為slave——》啟動sentinel

驗證可用性

  • 在master上set key
# Keyspace
127.0.0.1:7000> set a 1
OK
127.0.0.1:7000> get a
"1"
  • 在任意slave上get
# Keyspace
127.0.0.1:7000> get a
"1"
  • 腳本測試
from redis.sentinel import Sentinel
sentinel = Sentinel([('10.180.3.198', 27000)], 
                    password='YACEheyuan@123',socket_timeout=0.5)
master = sentinel.discover_master("mymaster")
print(master)

壓測

redis-benchmark -h 10.180.3.125 -p 27000 -a YACEheyuan@123 -c 100 -n 100000 -q

Tags: