推薦學習:
*Sentinel 哨兵:這是一個分散式系統,該程序是用於監控Redis叢集中Master主伺服器的工作狀態,在Master主伺服器發生故障時,可以實現Master和Slave伺服器的秒級切換,保證系統有一個Master主伺服器,提供了Redis叢集的高可用,在Reids2.6.版本時被加入,到2.8版本之後得到了穩定
Redis哨兵和Redis主從的區別:
Redis哨兵:主伺服器出現故障後,會有一個從伺服器代替主伺服器
Redis主從:主伺服器出現故障後,從伺服器不會做任何事
哨兵只需要部署在master主伺服器上即可
工作程序:
監控(Monitoring):哨兵通過流言協定(gossip protocols)會不斷檢查叢集中每一臺伺服器是否運作正常
提醒(Notification):當哨兵監控的某個redis伺服器出現問題時,哨兵可以通過API(應用程式介面)向管理員或者其他應用程式傳送通知
自動故障轉移(Automatic failover):在叢集中如果有一個Master主伺服器出現故障時,哨兵會通過投票協定(Agreement Protocols)開始一次自動故障遷移操作,他會選擇一臺資料較完整的Slave從伺服器升級為主伺服器,當用戶端試圖連線失效的Master主伺服器時,叢集也會向用戶端返回新的Master主伺服器的地址,使得叢集可以使用現在的Master替換掉失效的Master。
Master和Slave切換後,Master的redis主組態檔、Slave的redis主組態檔和哨兵的組態檔的內容都會發生相應的改變,即原來的Master的redis主組態檔會多一行Slave伺服器的設定,之後哨兵的監控目標就會改變到現在的Master主伺服器上
每隔10秒,每個Sentinel節點會向主節點和從節點傳送info命令獲取Redis資料節點的資訊
作用:
通過向主節點執行info命令,獲取從節點的資訊,這也是為什麼Sentinel節點不需要顯式設定監控從節點。當有新的從節點加入時都可以立刻感知出來,當節點不可達或者故障轉移後,可以通過info命令實時更新節點拓撲資訊。
每隔1秒,每個Sentinel節點會向主節點、從節點、傳送一條ping命令做一次心跳檢測,來確認這些節點當前是否可達如果主節點掛掉,那麼sentinel,就會從剩餘的從節點選擇一個資料比較完整來做主節點
系統 | ip | 主機名 | Redis版本 | 埠 | 扮演角色 |
---|---|---|---|---|---|
Centos7.4 | 192.168.100.202 | master | Redis-5.0.4 | Redis:6379 Sentinel:26379 | Master |
Centos7.4 | 192.168.100.203 | slave1 | Redis-5.0.4 | Redis:6379 | Slave |
Centos7.4 | 192.168.100.204 | slave2 | Redis-5.0.4 | Redis:6379 | Slave |
安裝步驟相同,主機名、ip不同,下面只寫Master設定
[root@Centos7 ~]# hostnamectl set-hostname master [root@Centos7 ~]# su [root@master ~]# systemctl stop firewalld [root@master ~]# setenforce 0 setenforce: SELinux is disabled [root@master ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 防寫,將以唯讀方式掛載 mount: /dev/sr0 已經掛載或 /mnt 忙 /dev/sr0 已經掛載到 /mnt 上 [root@master ~]# ll 總用量 1928 -rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg -rw-r--r-- 1 root root 1966337 6月 9 01:16 redis-5.0.4.tar.gz [root@master ~]# tar xf redis-5.0.4.tar.gz [root@master ~]# cd redis-5.0.4 [root@master redis-5.0.4]# make [root@master redis-5.0.4]# mkdir -p /usr/local/redis [root@master redis-5.0.4]# cp /root/redis-5.0.4/src/redis-server /usr/local/redis/ [root@master redis-5.0.4]# cp /root/redis-5.0.4/src/redis-cli /usr/local/redis/ [root@master redis-5.0.4]# cp /root/redis-5.0.4/redis.conf /usr/local/redis/ [root@master redis-5.0.4]# vim /usr/local/redis/redis.conf #修改 。。。。。。 68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 69 bind 192.168.100.202 #修改為本機地址,如果為127.0.0.1就只能本機存取 70 。。。。。。 87 # are explicitly listed using the "bind" directive. 88 protected-mode no #關閉redis的保護模式,如果為yes的話其他使用者端就無法連線到此伺服器 89 。。。。。。 135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 136 daemonize yes #開啟redis的後臺守護程式,即在redis開啟之後是放在後臺執行的 137 。。。。。。 262 # Note that you must specify a directory here, not a file name. 263 dir /usr/local/redis/rdb 264 。。。。。。 506 # 507 requirepass 123123 #去掉註釋,修改redis的密碼為123123 508 #儲存退出 [root@slave2 redis-5.0.4]# mkdir /usr/local/redis/rdb [root@master redis-5.0.4]# vim /etc/init.d/redis #!/bin/sh # chkconfig: 2345 80 90 # description: Start and Stop redis #PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/redis/redis-server REDIS_CLI=/usr/local/redis/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/usr/local/redis/redis.conf" AUTH="123123" LISTEN_IP=$(netstat -utpln |grep redis-server |awk '{print $4}'|awk -F':' '{print $1}') case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -h $LISTEN_IP -p $REDISPORT -a $AUTH SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac [root@master redis-5.0.4]# chkconfig --add redis [root@master redis-5.0.4]# chmod 755 /etc/init.d/redis [root@master redis-5.0.4]# ln -s /usr/local/redis/* /usr/local/bin/ [root@master redis-5.0.4]# /etc/init.d/redis start Starting Redis server... 5233:C 09 Jun 2021 01:25:53.069 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5233:C 09 Jun 2021 01:25:53.069 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5233, just started 5233:C 09 Jun 2021 01:25:53.069 # Configuration loaded Redis is running... [root@master redis-5.0.4]# netstat -anpt | grep 6379 tcp 0 0 192.168.100.202:6379 0.0.0.0:* LISTEN 5234/redis-server 1
-做redis主從
******(1)Master設定 [root@master redis-5.0.4]# vim /usr/local/redis/redis.conf #修改 。。。。。。 292 # 293 masterauth 123123 #設定主伺服器密碼,哨兵有一個問題,就是當主伺服器壞掉,切換到從伺服器時,原來的主伺服器可以正常執行之後,再次加入叢集是加不進去的,因為哨兵沒有設定主伺服器的密碼,所以無法連線,所以在使用哨兵叢集時,要把每臺的主伺服器密碼都設定上,每臺redis的密碼最好都一樣 294 。。。。。。 456 # 457 min-replicas-to-write 1 #設定slave伺服器的數量,當slave伺服器少於這個數量時,Master主伺服器會停止接收使用者端的一切寫請求 458 min-replicas-max-lag 10 #設定主伺服器和從伺服器之間同步資料的超時時間,當超過此時間時,master主伺服器會停止使用者端的一切寫操作,單位為秒 459 # 。。。。。。 [root@master redis-5.0.4]# /etc/init.d/redis restart #重新啟動redis Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped Starting Redis server... 5291:C 09 Jun 2021 02:04:39.132 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5291:C 09 Jun 2021 02:04:39.132 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5291, just started 5291:C 09 Jun 2021 02:04:39.132 # Configuration loaded Redis is running... ******(2)Slave1設定 [root@slave1 redis-5.0.4]# vim /usr/local/redis/redis.conf 。。。。。。 285 # 286 replicaof 192.168.100.202 6379 #在從伺服器上指定主伺服器的ip和埠 287 。。。。。。 292 # 293 masterauth 123123 #指定主伺服器上redis的密碼 294 。。。。。。 #儲存退出 [root@slave redis-5.0.4]# /etc/init.d/redis restart #重新啟動服務 Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped Starting Redis server... 5304:C 09 Jun 2021 02:11:32.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5304:C 09 Jun 2021 02:11:32.241 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5304, just started 5304:C 09 Jun 2021 02:11:32.241 # Configuration loaded Redis is running... ******(3)Slave2設定 [root@slave2 redis-5.0.4]# vim /usr/local/redis/redis.conf 。。。。。。 286 replicaof 192.168.100.204 6379 287 288 # If the master is password protected (using the "requirepass" configuration 289 # directive below) it is possible to tell the replica to authenticate before 290 # starting the replication synchronization process, otherwise the master will 291 # refuse the replica request. 292 # 293 masterauth 123123 294 。。。。。。 #儲存退出 [root@slave2 redis-5.0.4]# /etc/init.d/redis restart Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped Starting Redis server... 5253:C 09 Jun 2021 17:50:25.680 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5253:C 09 Jun 2021 17:50:25.680 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5253, just started 5253:C 09 Jun 2021 17:50:25.680 # Configuration loaded Redis is running... ******(3)驗證主從是否成功 [root@master ~]# redis-cli -h 192.168.100.202 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.202:6379> set aaa bbb OK 192.168.100.202:6379> set bbb ccc OK 192.168.100.202:6379> keys * 1) "aaa" 2) "bbb" [root@slave1 ~]# redis-cli -h 192.168.100.203 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.203:6379> keys * 1) "bbb" 2) "aaa" 192.168.100.203:6379> set ttt fff (error) READONLY You can't write against a read only replica. #從伺服器無法寫入資料 [root@slave2 redis-5.0.4]# redis-cli -h 192.168.100.204 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.204:6379> keys * 1) "aaa" 2) "bbb" 192.168.100.204:6379> set ggg aaa (error) READONLY You can't write against a read only replica. #主從設定完成
-設定哨兵
******(1)在master上設定sentinel哨兵 [root@master ~]# cp redis-5.0.4/src/redis-sentinel /usr/local/redis/ #複製哨兵啟動指令碼 [root@master ~]# cp redis-5.0.4/sentinel.conf /usr/local/redis/ #複製哨兵組態檔 [root@master ~]# mkdir -p /var/redis/data #建立紀錄檔檔案存放位置 [root@master ~]# vim /usr/local/redis/sentinel.conf #修改哨兵組態檔 。。。。。。 21 port 26379 #指定埠預設為26379 22 23 # By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it. 24 # Note that Redis will write a pid file in /var/run/redis-sentinel.pid when 25 # daemonized. 26 daemonize yes #yes為放在後臺執行,使用no放在前臺執行可以看到主從切換時候的資訊 27 。。。。。。 64 # unmounting filesystems. 65 dir /var/redis/data #指定紀錄檔存放位置,就是剛才建立的路徑 66 。。。。。。 83 # The valid charset is A-z 0-9 and the three characters ".-_". 84 sentinel monitor mymaster 192.168.100.202 6379 1 #指定使用者為mymaster,ip為202,埠為6379,1表示當有一臺master出現故障時,就進行切換 85 86 # sentinel a 。。。。。。 112 # Default is 30 seconds. 113 sentinel down-after-milliseconds mymaster 3000 #指定master的失效時間,單位為毫秒3000為3秒,表示master超過3秒沒響應就判定為故障 114 。。。。。。 145 # Default is 3 minutes. 146 sentinel failover-timeout mymaster 180000 #切換操作完成的超時時間,單位為毫秒180000為180秒,在主從切換超過這個時間就判定為切換失敗 147 148 # SCRIPTS EXE 。。。。。。 102 # 103 sentinel auth-pass mymaster 123123 #連線master和slave的密碼 104 sentinel config-epoch mymaster 1 #切換後最多有多少節點可以於新的master進行同步資料 105 #儲存退出 [root@master ~]# /usr/local/redis/redis-sentinel /usr/local/redis/sentinel.conf #啟動哨兵 1118:X 09 Jun 2021 18:09:29.027 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1118:X 09 Jun 2021 18:09:29.027 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1118, just started 1118:X 09 Jun 2021 18:09:29.027 # Configuration loaded [root@master ~]# netstat -anpt | grep 26379 tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 1119/redis-sentinel tcp6 0 0 :::26379 :::* LISTEN 1119/redis-sentinel [root@master ~]# kill -9 1119 #先關閉哨兵 [root@master ~]# netstat -anpt | grep 26379 [root@master ~]# sed -i '26s/yes/no/g' /usr/local/redis/sentinel.conf #修改為前臺啟動 [root@master ~]# /usr/local/redis/redis-sentinel /usr/local/redis/sentinel.conf #再次開啟哨兵,稍等一段時間會有提示 1129:X 09 Jun 2021 18:11:02.585 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1129:X 09 Jun 2021 18:11:02.585 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1129, just started 1129:X 09 Jun 2021 18:11:02.585 # Configuration loaded 1129:X 09 Jun 2021 18:11:02.586 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.4 (00000000/0) 64 bit .-`` .-". "\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 1129 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 1129:X 09 Jun 2021 18:11:02.586 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1129:X 09 Jun 2021 18:11:02.586 # Sentinel ID is fce7776020cf12792fd239f6f9d34f2d3fdef98c 1129:X 09 Jun 2021 18:11:02.586 # +monitor master mymaster 192.168.100.202 6379 quorum 1 1129:X 09 Jun 2021 18:18:04.434 * +reboot slave 192.168.100.204:6379 192.168.100.204 6379 @ mymaster 192.168.100.202 6379 #看到新增兩條訊息,從伺服器增加了203和204主伺服器時202 1129:X 09 Jun 2021 18:18:14.478 * +reboot slave 192.168.100.203:6379 192.168.100.203 6379 @ mymaster 192.168.100.202 6379 #哨兵設定完成
-測試哨兵的故障切換
******(1)把master伺服器在開啟一個終端,在新開啟的終端中關閉redis,測試是否可以主從切換 [root@master ~]# /etc/init.d/redis stop Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped ******(2)切換到開啟哨兵的終端,檢視新彈出的資訊 1129:X 09 Jun 2021 18:20:36.588 # +failover-end master mymaster 192.168.100.202 6379 1129:X 09 Jun 2021 18:20:36.588 # +switch-master mymaster 192.168.100.202 6379 192.168.100.203 6379 1129:X 09 Jun 2021 18:20:36.588 * +slave slave 192.168.100.204:6379 192.168.100.204 6379 @ mymaster 192.168.100.203 6379 #發現主伺服器變成了203 1129:X 09 Jun 2021 18:20:36.588 * +slave slave 192.168.100.202:6379 192.168.100.202 6379 @ mymaster 192.168.100.203 6379 1129:X 09 Jun 2021 18:20:39.607 # +sdown slave 192.168.100.202:6379 192.168.100.202 6379 @ mymaster 192.168.100.203 6379‘ ******(3)在203上測試主從複製是否可以正常同步 [root@slave1 ~]# redis-cli -h 192.168.100.203 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.203:6379> keys * 1) "aaa" 2) "bbb" 192.168.100.203:6379> set yyy aaa OK 192.168.100.203:6379> keys * 1) "yyy" 2) "aaa" 3) "bbb" [root@slave2 redis-5.0.4]# redis-cli -h 192.168.100.204 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.204:6379> keys * #發現同步成功 1) "yyy" 2) "bbb" 3) "aaa" ******(4)此時重新開啟202的redis,並且檢視哨兵的提示訊息 [root@master ~]# /etc/init.d/redis start Starting Redis server... 1167:C 09 Jun 2021 18:23:39.756 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1167:C 09 Jun 2021 18:23:39.756 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1167, just started 1167:C 09 Jun 2021 18:23:39.756 # Configuration loaded Redis is running... 1129:X 09 Jun 2021 18:23:50.324 * +convert-to-slave slave 192.168.100.202:6379 192.168.100.202 6379 @ mymaster 192.168.100.203 6379 #提示增加了一臺slave ******(5)在202的新終端上檢視redis的資料是否成功同步 [root@master ~]# redis-cli -h 192.168.100.202 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.202:6379> keys * #發現已經成功同步 1) "bbb" 2) "aaa" 3) "yyy" #測試故障切換快取,發現在master主機出現故障然後重新連線到叢集後,master角色不會進行轉移
-哨兵紀錄檔分析
#把哨兵放在前臺執行時,紀錄檔資訊會直接輸出到終端上,放到後臺執行時,紀錄檔會寫到指定的路徑中 +reset-master <instance details> #當master被重置時. +slave <instance details> #當檢測到一個slave並新增進slave列表時. +failover-state-reconf-slaves <instance details> #Failover狀態變為reconf-slaves狀態時 +failover-detected <instance details> #當failover發生時 +slave-reconf-sent <instance details> #sentinel傳送SLAVEOF命令把它重新設定時 +slave-reconf-inprog <instance details> #slave被重新設定為另外一個master的slave,但資料複製還未發生時。 +slave-reconf-done <instance details> #slave被重新設定為另外一個master的slave並且資料複製已經與master同步時。 -dup-sentinel <instance details> #刪除指定master上的冗餘sentinel時,當一個sentinel重新啟動時,可能會發生這個事件 +sentinel <instance details> #當master增加了一個sentinel時。 +sdown <instance details> #進入SDOWN狀態時; -sdown <instance details> #離開SDOWN狀態時。 +odown <instance details> #進入ODOWN狀態時。 -odown <instance details> #離開ODOWN狀態時。 +new-epoch <instance details> #當前設定版本被更新時。 +try-failover <instance details> #達到failover條件,正等待其他sentinel的選舉。 +elected-leader <instance details> #被選舉為去執行failover的時候。 +failover-state-select-slave <instance details> #開始要選擇一個slave當選新master時。 no-good-slave <instance details> #沒有合適的slave來擔當新master selected-slave <instance details> #找到了一個適合的slave來擔當新master failover-state-send-slaveof-noone <instance details> #當把選擇為新master的slave的身份進行切換的時候。 failover-end-for-timeout <instance details> #failover由於超時而失敗時。 failover-end <instance details> #failover成功完成時。 switch-master <master name> <oldip> <oldport> <newip> <newport> #當master的地址發生變化時。通常這是使用者端最感興趣的訊息了。 +tilt #進入Tilt模式。 -tilt #退出Tilt模式。
推薦學習:
以上就是Redis步驟解析之sentinel哨兵叢集的詳細內容,更多請關注TW511.COM其它相關文章!