1. 程式人生 > 資料庫 >redis哨兵實現

redis哨兵實現

原理:

 

 

 

 

 

 基於一主兩從架構實現哨兵

哨兵的前提是已經實現了一個redis的主從複製的執行環境,從而實現一個一主兩從基於哨兵的高可用
redis架構
注意: master 的配置檔案中masterauth 和slave 都必須相同

所有節點安裝redis:

tar xf redis-5.0.7.tar.gz -C /usr/local/src/
cd /usr/local/src/redis-5.0.7
make PREFIX=/apps/redis install
ll /apps/redis/bin/
echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
. /etc/profile.d/redis.sh
mkdir /apps/redis/{etc,log,data,run}
cp /usr/local/src/redis-5.0.7/redis.conf /apps/redis/etc/

建立 redis 使用者和資料目錄

useradd -r -s /sbin/nologin redis
chown -R redis.redis /apps/redis/

編輯 redis 服務啟動檔案

vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target

scp /usr/lib/systemd/system/redis.service 10.0.0.18:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/redis.service 10.0.0.28:/usr/lib/systemd/system/

驗證 redis 啟動

systemctl daemon-reload
systemctl start redis
systemctl status redis

所有主節點的redis.conf中關鍵配置

bind 0.0.0.0
masterauth "123456"
requirepass "123456"
logfile "/apps/redis/log/redis.log"
dir /apps/redis/data

scp /apps/redis/etc/redis.conf 10.0.0.18:/apps/redis/etc/
scp /apps/redis/etc/redis.conf 10.0.0.28:/apps/redis/etc/

 配置master

 配置slave1

REPLICAOF 10.0.0.8 6379  #啟用主從同步複製,並把ip指向master(10.0.0.8)

 配置slave2

 編輯哨兵的配置檔案

sentinel配置
Sentinel實際上是一個特殊的redis伺服器,有些redis指令支援,但很多指令並不支援.預設監聽在
26379/tcp埠.
哨兵可以不和Redis伺服器部署在一起,但一般部署在一起,所有redis節點使用相同的以下示例的配置
檔案

我這裡才用的是原始碼編譯安裝,在原始碼目錄有sentinel.conf,複製到安裝目錄即可

cp sentinel.conf /apps/redis/etc/

#如果是編譯安裝,在所有哨兵伺服器執行下面操作啟動哨兵
vi /apps/redis/etc/sentinel.conf #修改以下內容

  bind 0.0.0.0
  port 26379
  daemonize yes
  pidfile /apps/redis/run/redis-sentinel.pid
  logfile "/apps/redis/log/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

 

#修改sentinel.conf檔案許可權(3臺主機都執行)

chown redis.redis /apps/redis/etc/sentinel.conf

#建立sentinel.service啟動檔案

vi /usr/lib/systemd/system/redis-sentinel.service

[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf --supervised systemd
ExecStop=/apps/redis/bin/redis-shutdown redis-sentinel
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

 

scp /usr/lib/systemd/system/redis-sentinel.service 10.0.0.18:/usr/lib/systemd/system/

scp /usr/lib/systemd/system/redis-sentinel.service 10.0.0.28:/usr/lib/systemd/system/

#啟動哨兵

[root@redis-master ~]#systemctl daemon-reload
[root@redis-master ~]#systemctl start redis-sentinel.service

確保每個哨兵主機的myid不同

[root@redis-slave1 ~]#vi /apps/redis/etc/sentinel.conf
sentinel myid 782765adb0402fd686313ff99f49862f3c7f3d9c

[root@redis-slave2 ~]#vi /apps/redis/etc/sentinel.conf
sentinel myid 782765adb0402fd686313ff99f49862f3c7f3d9d

[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

當前sentinel狀態

 在sentinel狀態中尤其是最後一行,涉及到masterIP是多少,有幾個slave,有幾個sentinels,必須是

符合全部伺服器數量

[root@redis-master ~]#redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.8:6379,slaves=2,sentinels=3 #兩個
slave,三個sentinel伺服器

停止Redis Master測試故障轉移

[root@redis-master ~]#killall redis-server

檢視各節點上哨兵資訊:

[root@redis-master ~]#redis-cli -a 123456 -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.18:6379,slaves=2,sentinels=2