我這裏裝的是一主三從,其中有一個從一直不能切換到主,因此這臺機器上不須要配置keepalived,只須要在redis.conf文件配置上加上slaveof 20.200.45.95 6479,這個IP配置的是虛擬IP的地址,由於你不知道主機到時候會是哪一臺IP地址。c++
機器信息:20.200.45.72 主redis
20.200.45.73 從安全
20.200.45.74 從ruby
20.200.45.75 從(永遠不能升爲主)bash
申請的虛擬IP:20.200.45.95app
yum install gcc zib ruby rubygems tcl測試
解壓:tar -zxvf redis-4.0.2.tar.gzspa
> cd redis-4.0.2rest
> make日誌
> make test
> make install
安裝成功後,在/usr/local/bin/目錄下,會出現6個文件,以下圖
你能夠在redis-4.0.2目錄下,建立bin文件夾,把/usr/local/bin/下的文件拷貝到
redis-4.0.2/bin目錄下,在建立個logs文件夾,步驟以下:
> cd redis-4.0.2
> mkdir bin
> mkdir logs
> cp /usr/local/bin /home/user1/redis-4.0.2/bin
給其中三個文件賦予高一點的權限,最高是777
>chmod 777 redis-cli
>chmod 777 redis-server
>chmod 777 redis-sentinel
每一個redis配置文件都一致,注意:永遠不能升爲主的機器上,再加上slaveof 20.200.45.95 6479 這個配置就行了,別的機器不須要這個配置。
redis.conf基本配置:
bind 0.0.0.0
port 6479
protected-mode no
daemonize yes
pidfile "app/redis1z3c/redis/redis_6479.pid"
logfile "app/redis1z3c/redis/logs/redis.log"
stop-writes-on-bgsave-error no
dir "app/redis1z3c/redis/bin"
slave-serve-stale-data yes
slave-read-only no
slave-priority 80
appendonly yes
我redis安裝在/app/redis1z3c/redis目錄下
啓動72機器:後面73,74,75依次都是這個命令
> cd /app/redis1z3c/redis/bin
> ./redis-server ../redis.conf ##啓動redis
> /redis-cli -h 20.200.45.72 -p 6479
輸入info,能夠看到redis信息,
會發現除了75的機器,每一臺都是master
75機器顯示是slave,如圖:
由於keepalived沒有啓動,因此虛擬IP顯示的是down機狀態。
yum install kernel-devel openssl-devel popt-devel ipvsadm libnl libnl-devel
我是安裝在/app/redis1z3c/目錄下
解壓:tar -zxvf keepalived-2.0.10.tar.gz
> cd keepalived-2.0.10
> ./configure --prefix=/usr/local/keepalived
> make && make install
在/usr/local/keepalived相應的生成4個文件夾,如圖:
注意:keepalived啓動,讀取的配置文件必定是/etc/keepalived/keepalived.conf文件
> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
> cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
在/etc目錄下,建立keepalived文件夾
> mkdir keepalived
> cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived ##這是keepalived配置文件
> cp /app/redis1z3c/keepalived-2.0.10/keepalived/etc/init.d/keepalived /etc/init.d ##這是啓動腳本
默認是放在/var/log/message下面,可是這個文件有不少別的日誌信息,因此咱們單獨配置keepalived日誌文件
修改/etc/sysconfig/keepalived文件:
> vi /etc/sysconfig/keepalived
把KEEPALIVED_OPTIONS="-D" 修改爲KEEPALIVED_OPTIONS="-D -d -S 0",保存
修改 /etc/rsyslog.conf
在最後一行添加:
local0.* /etc/keepalived/keepalived.log
systemctl restart rsyslog ##重啓rsyslog
腳本文件我放在/etc/keepalived/scripts文件下,在/etc/keepalived文件下在建立log文件
如圖:
這是20.200.45.72的配置:
! Configuration File for keepalived
global_defs {
router_id ocs_keepalived
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
}
vrrp_instance VI_1 {
state MASTER ###主節點,從機器要把MASTER 換成BACKUP
interface eth0 ###網卡
virtual_router_id 25 ###設置VRID,決定多播的MAC地址
mcast_src_ip 20.200.45.72 ###多播地址,73的機器上配置20.200.45.73的IP,74的機器上配置20.200.45.74的IP地址
priority 100 ###優先級,73的機器上配置98,74的機器上配置95
advert_int 1 ###檢查間隔
authentication { ###設置的認證
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress { ###虛擬IP地址
20.200.45.95
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 20.200.45.95 6479"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 20.200.45.95 6479"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop "/etc/keepalived/scripts/redis_stop.sh 127.0.0.1 20.200.45.95 6479"
}
#!/bin/bash
ALIVE=`/app/redis1z3c/redis/bin/redis-cli -h $1 -p $2 PING`
LOGFILE="/etc/keepalived/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
#!/bin/bash
REDISCLI="/app/redis1z3c/redis/bin/redis-cli -h $1 -p $3"
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
echo "$2 $3" >>$LOGFILE
echo "關閉備份,轉換成主機模式" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF NO ONE cmd ... " >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "close slaver ,become master " >> $LOGFILE
sleep 5
echo "發送消息給監控" >> $LOGFILE
#!/bin/bash
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
echo "$2 $3" >> $LOGFILE
echo "keepalived 出現故障,redis變爲備機">> $LOGFILE
REDISCLI="/app/redis1z3c/redis/bin/redis-cli -h $1 -p $3"
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
#!/bin/bash
REDISCLI="/app/redis1z3c/redis/bin/redis-cli -h $1 -p $3"
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[BACKUP]" >> $LOGFILE
echo "$1 $3" >> $LOGFILE
echo "關閉主機模式,轉爲備機模式" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
echo "發送消息給監控" >> $LOGFILE
#/bin/bash
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
echo "redis 出現故障,沒法ping通,請檢查redis服務" >> $LOGFILE
記住:虛擬IP在哪臺機器上,哪臺機器就會是主機,虛擬ip是按照優先級去區分主從。
只須要啓動72,73,74機器,啓動命令以下:
進入/etc/init.d文件夾下
> ./keepalived start
> ps -ef|grep keepalived
> ip a 看虛擬IP是否出來,72會顯示虛擬IP
73和74的機器上, 是沒有虛擬IP 的,如圖:
72機器:
>./redis-cli -h 20.200.45.72 -p 6479
>info ##會看到72變成了主,73,74,75都是從
在任意看其中一臺從的機器,好比74
> ./redis-cli -h 20.200.45.74 -p 6479
> info ##會看到74是虛擬IP的從
再看75機器
> ./redis-cli -h 20.200.45.75 -p 6479
> info ##會發現虛擬IP變成up狀態
咱們也能夠直接鏈接虛擬ip
> ./redis-cli -h 20.200.45.95 -p 6479
> info ##會發現虛擬IP是主機,跟72顯示狀態同樣
這個時候我把72的機器停了,會發現73變成了主,74,75仍是從
若是再把72啓動起來,會發現72又變回了主
下面我把redis配置文件和keepalived配置文件放上去
1.系統防火牆必須關掉
2.VIP使用須要在同一個網段
3.實現主備模式,須要先所有啓動redis,以後按照優先級啓動keepalived
4.redis備庫掉了以後,從新加入機器須要先殺掉keepalived,以後重啓啓動redis和keepalived,
若是不這樣作,有可能會出現雙主。不過我這邊測試安裝沒有出現雙主的狀況,因此這個按照當時狀況再看。
5.結合項目,雲平臺可能會存在限制,主要是安全組件打開,keepalived沒法正常使用,形成VIP出現兩臺機器上。