Keepalived 實現VRRP(虛擬路由冗餘)協議,從路由級別實現VIP切換,可以完全避免類似heartbeat腦裂問題,可以很好的實現主從、主備、互備方案,尤其是無狀態業務,有狀態業務就需要額外花些功夫了。既然Mysql可以使用Keepalived很好的做到主從切換,那麼Redis自然可以使用這種方式實現高可用。
Redis主從實現完全沒有Mysql成熟,僅僅是可用而已,經過測試主從也不是那麼完全不靠譜,主要問題在於同步連線斷開之後需要重新全量同步,如果頻繁進行會對主服務帶來很大效能影響。 但現實中主從機器往往要求放在一個機櫃同一臺交換裝置下,網路閃斷情況極低;再者主從同步在同步數量量大情況下,需要將快取區調得足夠大,不然也容易造成連線斷開。
實現切換邏輯如下:A和B兩臺機器
1)A 、B機器依次啟動,A機作為主、B機為從。
2)主A機掛掉,B機接管業務並作為主。
3)A機起來,作為從SLAVEOF B。
4)B機掛掉,A機再切回主。
在Keepalived 有兩個角色:Master(一個)、Backup(多個),如果設定一個為Master,但Master掛了後再起來,必然再次業務又一次切換,這對於有狀態服務是不可接受的。解決方案就是兩臺機器都設定為Backup,而且優先順序高的Backup設定為nopreemt 不搶佔。
部署記錄:
0)伺服器資訊
192.168.10.205 redis-master 需要安裝redis(3.2.0版本)、keepalived(1.3.2版本) 192.168.10.206 redis-slave 需要安裝redis(3.2.0版本)、keepalived(1.3.2版本) 192.168.10.230 VIP 關閉兩個節點機器的iptables和selinux(兩個節點上都要操作) [root@redis-master ~]# /etc/init.d/iptables stop [root@redis-master ~]# vim /etc/sysconfig/selinux ...... SELINUX=disabled [root@redis-master ~]# setenforce 0 [root@redis-master ~]# getenforce Permissive
1)安裝redis服務及主從配置(兩個節點機都要操作)
[root@redis-master ~]# cd /usr/local/src/ [root@redis-master src]# wget http://download.redis.io/releases/redis-3.2.0.tar.gz [root@redis-master src]# tar -zvxf redis-3.2.0.tar.gz [root@redis-master src]# cd redis-3.2.0 [root@redis-master redis-3.2.0]# make 新增相關檔案及命令 [root@redis-master redis-3.2.0]# mkdir -p /usr/local/redis/bin/ [root@redis-master redis-3.2.0]# cd src [root@redis-master src]# cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel /usr/local/redis/bin/ [root@redis-master src]# cd ../ [root@redis-master redis-3.2.0]# cp redis.conf /etc/ 新增redis啟動指令碼 [root@redis-master redis-3.2.0]# vim /etc/init.d/redis #!/bin/bash #chkconfig: 2345 10 90 #description: Start and Stop redis REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server REDIS_CLI=/usr/local/redis/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/etc/redis.conf" 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 -p $REDISPORT 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@redis-master redis-3.2.0]# chmod 755 /etc/init.d/redis 設定開機自啟動 [root@redis-master redis-3.2.0]# chkconfig --add redis [root@redis-master redis-3.2.0]# chkconfig redis on 建立redis狀態日誌 [root@redis-master redis-3.2.0]# mkdir /var/log/redis/ [root@redis-master redis-3.2.0]# touch /var/log/redis/redis.log redis主從配置(先看下redis-master主節點的配置) [root@redis-master redis-3.2.0]# vim /etc/redis.conf ....... port 6379 ....... daemonize yes #這個修改為yes ....... bind 0.0.0.0 #繫結的主機地址。說明只能通過這個ip地址連線本機的redis。最好繫結0.0.0.0;注意這個不能配置成127.0.0.1,否則複製會失敗!用0.0.0.0或者本機ip地址都可以 ....... pidfile /var/run/redis.pid ....... logfile /var/log/redis/redis.log ....... dir /var/redis/redis #redis資料目錄 ....... appendonly yes #啟用AOF持久化方式 appendfilename "appendonly.aof" #AOF檔案的名稱,預設為appendonly.aof appendfsync everysec #每秒鐘強制寫入磁碟一次,在效能和持久化方面做了很好的折中,是受推薦的方式。 ..... save 900 1 #啟用RDB快照功能,預設就是啟用的 save 300 10 save 60 10000 #即在多少秒的時間內,有多少key被改變的資料新增到.rdb檔案裡 ....... slave-serve-stale-data yes #預設就會開啟 slave-read-only yes ...... dbfilename dump.rdb #快照檔名稱 ...... 另一個從節點redis-slave的redis.conf配置和上面基本差不多,只是多了下面一行配置: slaveof 192.168.10.205 6379 接著建立redis的資料目錄 [root@redis-master redis-3.2.0]# mkdir -p /var/redis/redis 然後啟動兩個節點的redis服務 [root@redis-master redis-3.2.0]# /etc/init.d/redis start Starting Redis server... Redis is running... [root@redis-master redis-3.2.0]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 17265 root 4u IPv4 59068 0t0 TCP *:6379 (LISTEN)
2)Keepalived安裝(兩個節點機都要操作)
[root@redis-master ~]# yum -y install openssl openssl-devel [root@redis-master ~]# cd /usr/local/src/ [root@redis-master src]# wget http://www.keepalived.org/software/keepalived-1.4.0.tar.gz [root@redis-master src]# tar -zvxf keepalived-1.4.0.tar.gz [root@redis-master src]# cd keepalived-1.4.0 [root@redis-master keepalived-1.4.0]# ./configure && make && make install 檔案配置 [root@redis-master keepalived-1.4.0]# mkdir /etc/keepalived [root@redis-master keepalived-1.4.0]# mkdir /usr/local/keepalived/scripts/ -p [root@redis-master keepalived-1.4.0]# cp /usr/local/src/keepalived-1.4.0/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@redis-master keepalived-1.4.0]# cp /usr/local/src/keepalived-1.4.0/keepalived/etc/init.d/keepalived /etc/init.d/ [root@redis-master keepalived-1.4.0]# cp /usr/local/sbin/keepalived /usr/sbin [root@redis-master keepalived-1.4.0]# cp /usr/local/src/keepalived-1.4.0/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 設定開機啟動 [root@redis-master keepalived-1.4.0]# chmod +x /etc/init.d/keepalived [root@redis-master keepalived-1.4.0]# chkconfig --add keepalived [root@redis-master keepalived-1.4.0]# chkconfig keepalived on
redis主從配置簡單說明
redis的主從複製實現簡單卻功能強大,其具有以下特點: 1)一個master支援多個slave連線,slave可以接受其他slave的連線 2)主從同步時,master和slave都是非阻塞的 redis主從複製可以用來: 1)data redundancy(資料冗餘) 2)slave作為master的擴充套件,提供一些read-only的服務 3)可以將資料持久化放在slave做,從而提升master效能 通過簡單的配置slave(master端無需配置),使用者就能使用redis的主從複製,即只需在slave端的redis.conf檔案中配置下面一行: slaveof <masterip> <masterport> 表示該redis服務作為slave,masterip和masterport分別為master 的ip和port 其他配置: masterauth <master-password> 如果master設定了安全密碼,則此處設定為相應的密碼 slave-serve-stale-data yes 當slave丟失master或者同步正在進行時,如果發生對slave的服務請求: slave-serve-stale-data設定為yes則slave依然正常提供服務 slave-serve-stale-data設定為no則slave返回client錯誤:"SYNC with master in progress" repl-ping-slave-period 10 slave傳送PINGS到master的時間間隔 repl-timeout 60 IO超時時間
3)redis+keepalived配置
a)先進行redis-master主節點的高可用配置 [root@redis-master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@redis-master ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id redis-master } vrrp_script chk_redis { script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6379" #監控指令碼 interval 2 #監控時間 timeout 2 #超時時間 fall 3 } vrrp_instance redis { state BACKUP interface eth0 lvs_sync_daemon_interface eth0 virtual_router_id 202 priority 150 #權重值 nopreempt #nopreempt:設定不搶佔,這裡只能設定在state為backup的節點上,而且這個節點的優先順序必須比另外節點的高 advert_int 1 authentication { #all node must same auth_type PASS #加密 auth_pass 1111 #密碼 } virtual_ipaddress { 192.168.10.230 #VIP地址 } track_script { chk_redis } notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.10.206 6379" notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.10.206 6379" notify_fault /usr/local/keepalived/scripts/redis_fault.sh notify_stop /usr/local/keepalived/scripts/redis_stop.sh } b)接著進行redis-slave從節點的高可用配置 [root@redis-slave ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@redis-slave ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id redis-slave } vrrp_script chk_redis{ script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6379" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP interface eth0 lvs_sync_daemon_interface eth0 virtual_router_id 202 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.230 } track_script { chk_redis } notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.10.205 6379" notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.10.205 6379" notify_fault /usr/local/keepalived/scripts/redis_fault.sh notify_stop /usr/local/keepalived/scripts/redis_stop.sh } c)在redis-master和redis-slave兩個節點機器上都要建立監控指令碼(下面幾個指令碼,在兩個節點上都要同樣配置) 首先配置監控指令碼 [root@redis-master ~]# vim /usr/local/keepalived/scripts/redis_check.sh #!/bin/bash ALIVE=`/usr/local/redis/bin/redis-cli -h $1 -p $2 PING` LOGFILE="/var/log/keepalived-redis-check.log" echo "[CHECK]" >> $LOGFILE date >> $LOGFILE if [ $ALIVE == "PONG" ]; then : echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1 exit 0 else echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1 exit 1 fi 需要注意的是: 以下負責運作的關鍵指令碼: notify_master /usr/local/keepalived/scripts/redis_master.sh notify_backup /usr/local/keepalived/scripts/redis_backup.sh notify_fault /usr/local/keepalived/scripts/redis_fault.sh notify_stop /usr/local/keepalived/scripts/redis_stop.sh 因為Keepalived在轉換狀態時會依照狀態來呼叫: 當進入Master狀態時會呼叫notify_master 當進入Backup狀態時會呼叫notify_backup 當發現異常情況時進入Fault狀態呼叫notify_fault 當Keepalived程式終止時則呼叫notify_stop
溫馨提示:
以上的keepalived.conf檔案中的切換模式設定為nopreempt,意思是:
不搶佔VIP資源,此種模式要是所有的節點都必須設定為state BACKUP模式!
需要注意無論主備伺服器都需要設定為BACKUP,與以往KeepAlived的配置不同,其目的就是防止主伺服器恢復後重新搶回VIP,導致Redis切換從而影響穩定。
接著在redis-master主節點上建立notity_master與notify_backup指令碼: [root@redis-master ~]# vim /usr/local/keepalived/scripts/redis_master.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli -h $1 -p $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ... " >> $LOGFILE $REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1 #echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 [root@redis-master ~]# vim /usr/local/keepalived/scripts/redis_backup.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[BACKUP]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1 $REDISCLI SLAVEOF $2 $3 >> $LOGFILE sleep 100 #延遲100秒以後待資料同步完成後再取消同步狀態 exit(0) [root@redis-master ~]# vim /usr/local/keepalived/scripts/redis_fault.sh #!/bin/bash LOGFILE="/var/log/keepalived-redis-state.log" echo "[fault]" >> $LOGFILE date >> $LOGFILE [root@redis-master ~]# vim /usr/local/keepalived/scripts/redis_stop.sh #!/bin/bash LOGFILE="/var/log/keepalived-redis-state.log" echo "[stop]" >> $LOGFILE date >> $LOGFILE [root@redis-master ~]# chmod 755 /usr/local/keepalived/scripts/*.sh [root@redis-master ~]# ll /usr/local/keepalived/scripts/ total 20 -rwxr-xr-x. 1 root root 283 May 7 07:20 redis_backup.sh -rwxr-xr-x. 1 root root 360 May 7 07:12 redis_check.sh -rwxr-xr-x. 1 root root 102 May 7 07:22 redis_fault.sh -rwxr-xr-x. 1 root root 445 May 7 07:16 redis_master.sh -rwxr-xr-x. 1 root root 101 May 7 07:23 redis_stop.sh 將redis-master主節點上的上面5個指令碼直接複製到redis-slave節點上即可。 [root@redis-master ~]# rsync -e "ssh -p22" -avpgolr /usr/local/keepalived/scripts/*.sh root@192.168.10.206:/usr/local/keepalived/scripts/ 到redis-slave從節點上檢視指令碼: [root@redis-slave ~]# ll /usr/local/keepalived/scripts/ total 20 -rwxr-xr-x. 1 root root 283 May 7 07:20 redis_backup.sh -rwxr-xr-x. 1 root root 360 May 7 07:12 redis_check.sh -rwxr-xr-x. 1 root root 102 May 7 07:22 redis_fault.sh -rwxr-xr-x. 1 root root 445 May 7 07:16 redis_master.sh -rwxr-xr-x. 1 root root 101 May 7 07:23 redis_stop.sh d)設定環境變數(兩個節點上都要設定) [root@redis-master ~]# vim /etc/profile ...... export PATH=$PATH:/usr/local/redis/bin [root@redis-master ~]# source /etc/profile e)啟動兩個節點上的keepalived服務 [root@redis-master ~]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@redis-master ~]# ps -ef|grep keepalived root 32509 1 0 07:29 ? 00:00:00 keepalived -D root 32510 32509 0 07:29 ? 00:00:00 keepalived -D root 32512 32509 0 07:29 ? 00:00:00 keepalived -D root 32515 32512 0 07:29 ? 00:00:00 keepalived -D root 32517 32515 0 07:29 ? 00:00:00 /bin/bash /usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.10.206 6379 root 32529 14122 0 07:29 pts/1 00:00:00 grep keepalived [root@redis-slave ~]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@redis-slave ~]# ps -ef|grep keepalived root 22277 1 0 07:29 ? 00:00:00 keepalived -D root 22278 22277 0 07:29 ? 00:00:00 keepalived -D root 22279 22277 0 07:29 ? 00:00:00 keepalived -D root 22283 22279 0 07:29 ? 00:00:00 keepalived -D root 22284 22283 0 07:29 ? 00:00:00 /bin/bash /usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.10.205 6379 root 22289 10868 0 07:29 pts/1 00:00:00 grep keepalived 檢視下redis-master主節點,發現vip資源已經有了 [root@redis-master ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:b1:9c:93 brd ff:ff:ff:ff:ff:ff inet 192.168.10.205/24 brd 192.168.10.255 scope global eth0 inet 192.168.10.230/32 scope global eth0 inet6 fe80::5054:ff:feb1:9c93/64 scope link valid_lft forever preferred_lft forever
4)redis+keepalived主從高可用故障切換測試
a)分別啟動redis-master和redis-slave兩個節點的redis和keepalived服務(如上已啟動) b)嘗試通過VIP連線Redis: [root@redis-master ~]# redis-cli -h 192.168.10.230 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.205 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.206 INFO|grep role role:slave 連線成功,Slave也連線上來了。 c)嘗試插入一些資料: [root@redis-master ~]# redis-cli -h 192.168.10.230 SET Hello Redis OK 從VIP讀取資料 [root@redis-master ~]# redis-cli -h 192.168.10.230 GET Hello "Redis" 從redis-master主節點讀取資料 [root@redis-master ~]# redis-cli -h 192.168.10.205 GET Hello "Redis" 從redis-slave從節點讀取資料 [root@redis-master ~]# redis-cli -h 192.168.10.206 GET Hello "Redis" e)然後開始模擬故障產生: 將redis-master主節點上的redis程式殺死: [root@redis-master ~]# ps -ef|grep redis root 4500 14122 0 08:04 pts/1 00:00:00 grep redis root 17265 1 0 04:00 ? 00:00:07 /usr/local/redis/bin/redis-server 0.0.0.0:6379 [root@redis-master ~]# kill -9 17265 [root@redis-master ~]# ps -ef|grep redis root 4514 14122 0 08:04 pts/1 00:00:00 grep redis 檢視redis-master主節點上的Keepalived日誌 [root@redis-master ~]# tail -f /var/log/keepalived-redis-state.log OK [master] Mon May 7 07:29:17 CST 2018 Being master.... Run SLAVEOF cmd ... OK Already connected to specified master Run SLAVEOF NO ONE cmd ... OK [fault] Mon May 7 08:05:00 CST 2018 同時redis-slave從節點上的日誌顯示: [root@redis-slave ~]# tail -f /var/log/keepalived-redis-state.log Being slave.... Run SLAVEOF cmd ... OK [master] Mon May 7 08:05:02 CST 2018 Being master.... Run SLAVEOF cmd ... OK Already connected to specified master Run SLAVEOF NO ONE cmd ... OK 然後我們可以發現,redis-slave從節點已經接管服務,並且擔任Master的角色了。 redis-slave從節點上已經接管過來VIP資源了(大概需要等待2秒左右的時間,vip資源就切過來了) [root@redis-slave ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:dd:84:6b brd ff:ff:ff:ff:ff:ff inet 192.168.10.206/24 brd 192.168.10.255 scope global eth0 inet 192.168.10.230/32 scope global eth0 inet6 fe80::5054:ff:fedd:846b/64 scope link valid_lft forever preferred_lft forever [root@redis-slave ~]# redis-cli -h 192.168.10.230 INFO|grep role role:master [root@redis-slave ~]# redis-cli -h 192.168.10.205 INFO|grep role Could not connect to Redis at 192.168.10.205:6379: Connection refused [root@redis-slave ~]# redis-cli -h 192.168.10.206 INFO|grep role role:master ======================================================================= 然後再恢復redis-master主節點的redis程式 [root@redis-master ~]# /etc/init.d/redis start /var/run/redis.pid exists, process is already running or crashed Redis is running... [root@redis-master ~]# rm -f /var/run/redis.pid [root@redis-master ~]# /etc/init.d/redis start Starting Redis server... Redis is running... [root@redis-master ~]# ps -ef|grep redis root 4969 1 0 08:08 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379 root 4977 4976 0 08:08 ? 00:00:00 /bin/bash /usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.10.206 6379 root 4987 14122 0 08:08 pts/1 00:00:00 grep redis [root@redis-master ~]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 4969 root 4u IPv4 93698 0t0 TCP *:6379 (LISTEN) redis-ser 4969 root 6u IPv4 93709 0t0 TCP 192.168.10.205:43299->192.168.10.206:6379 (ESTABLISHED) 檢視redis-master上的Keepalived日誌 [root@redis-master ~]# tail -f /var/log/keepalived-redis-state.log OK Already connected to specified master Run SLAVEOF NO ONE cmd ... OK [fault] Mon May 7 08:05:00 CST 2018 [BACKUP] Mon May 7 08:08:34 CST 2018 Being slave.... Run SLAVEOF cmd ... OK 檢視redis-slave上的Keepalived日誌 [root@redis-slave ~]# tail -f /var/log/keepalived-redis-state.log Being slave.... Run SLAVEOF cmd ... OK [master] Mon May 7 08:05:02 CST 2018 Being master.... Run SLAVEOF cmd ... OK Already connected to specified master Run SLAVEOF NO ONE cmd ... OK [root@redis-master ~]# redis-cli -h 192.168.10.230 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.205 INFO|grep role role:slave [root@redis-master ~]# redis-cli -h 192.168.10.206 INFO|grep role role:master 發現redis-master的redis服務再次啟動後,redis-master主節點成為salve角色了,redis-slave從節點還是master角色。 當redis-slave節點當機或redis服務關閉後,redis-master節點再次接管服務成為master角色,如此迴圈~~ 關閉redis-slave從節點的reids服務 [root@redis-slave ~]# ps -ef|grep redis root 15407 1 0 04:00 ? 00:00:10 /usr/local/redis/bin/redis-server 0.0.0.0:6379 root 22900 10868 0 08:11 pts/1 00:00:00 grep redis [root@redis-slave ~]# kill -9 15407 [root@redis-slave ~]# ps -ef|grep redis root 22902 10868 0 08:11 pts/1 00:00:00 grep redis 檢視redis-slave上的Keepalived日誌 [root@redis-slave ~]# tail -f /var/log/keepalived-redis-state.log ....... [stop] //測試時發現,當redis-slave的redis服務關閉後,還需要重啟或關閉keepalived,才能將vip資源漂到redis-master節點上,所以日誌裡也就會出現這個stop資訊 Mon May 7 09:25:03 CST 2018 [BACKUP] Mon May 7 09:25:04 CST 2018 Being slave.... Run SLAVEOF cmd ... OK 檢視redis-master上的Keepalived日誌 [root@redis-master ~]# tail -f /var/log/keepalived-redis-state.log ....... [master] Mon May 7 09:25:03 CST 2018 Being master.... Run SLAVEOF cmd ... OK Already connected to specified master Run SLAVEOF NO ONE cmd ... OK 檢視redis-master,發現VIP資源已經接管過來了(如果沒有按時切過來的話,只需重啟或關閉redis-slave節點那邊的keepalived服務即可) [root@redis-master ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:b1:9c:93 brd ff:ff:ff:ff:ff:ff inet 192.168.10.205/24 brd 192.168.10.255 scope global eth0 inet 192.168.10.230/32 scope global eth0 inet6 fe80::5054:ff:feb1:9c93/64 scope link valid_lft forever preferred_lft forever [root@redis-master ~]# redis-cli -h 192.168.10.230 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.205 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.206 INFO|grep role Could not connect to Redis at 192.168.10.206:6379: Connection refused 發現redis-maste節點已經轉變為master角色了。 同樣,當reids-slave節點的redis服務重新啟動後,它將成為slave角色。 [root@redis-slave ~]# /etc/init.d/redis start /var/run/redis.pid exists, process is already running or crashed Redis is running... [root@redis-slave ~]# rm -f /var/run/redis.pid [root@redis-slave ~]# /etc/init.d/redis start Starting Redis server... Redis is running... [root@redis-slave ~]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 23244 root 4u IPv4 3049509 0t0 TCP *:6379 (LISTEN) redis-ser 23244 root 6u IPv4 3049513 0t0 TCP dns.kevin.cn:44931->192.168.10.205:6379 (ESTABLISHED) [root@redis-master ~]# redis-cli -h 192.168.10.230 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.205 INFO|grep role role:master [root@redis-master ~]# redis-cli -h 192.168.10.206 INFO|grep role role:slave