一、keepalived 的安装
#版本自己选定
wget http://www.keepalived.org/software/keepalived-1.4.0.tar.gz
tar -zvxf keepalived-1.4.0.tar.gz
cd keepalived-1.4.0
#指定一下安装的目录
./configure --prefix=/usr/local/keepalived
#编译安装
make && make install
二、配置
1.抢占式 主机
linux-eauv:~ vim /usr/local/keepalived/etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs {
router_id LVS_1
}
vrrp_script notify_check {
script "/usr/local/keepalived/scripts/notify_check.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER #MASTER / BACKUP
interface eth0 #虚IP地址放置的网卡位置
virtual_router_id 51 #两机相同
priority 100 #优先级 主机必须大于备机
advert_int 1 #主备通讯时间间隔
authentication { #认证 两机相同
auth_type PASS
auth_pass 1111
}
track_script {
notify_check
}
virtual_ipaddress { #虚拟ip
198.168.0.222
}
#脚本
notify_master "/usr/local/keepalived/scripts/notify_master.sh 192.168.0.1 6379"
notify_backup "/usr/local/keepalived/scripts/notify_backup.sh 192.168.0.1 6379"
notify_fault "/usr/local/keepalived/scripts/notify_fault.sh 192.168.0.1 6379"
notify_stop "/usr/local/keepalived/scripts/notify_stop.sh"
}
2.抢占式备机
!Configuration File for keepalived
global_defs {
router_id LVS_1
}
vrrp_script notify_check {
script "/usr/local/keepalived/scripts/notify_check.sh"
interval 3
}
vrrp_instance VI_1 {
state BACKUP #MASTER / BACKUP
interface eth0 #虚IP地址放置的网卡位置
virtual_router_id 51 #两机相同
priority 99 #优先级 主机必须大于备机
advert_int 1 #主备通讯时间间隔
authentication { #认证 两机相同
auth_type PASS
auth_pass 1111
}
track_script {
notify_check
}
virtual_ipaddress { #虚拟ip
198.168.0.222
}
#脚本 IP 为另一台ip
notify_master "/usr/local/keepalived/scripts/notify_master.sh 192.168.0.2 6379"
notify_backup "/usr/local/keepalived/scripts/notify_backup.sh 192.168.0.2 6379"
notify_fault "/usr/local/keepalived/scripts/notify_fault.sh 192.168.0.2 6379"
notify_stop "/usr/local/keepalived/scripts/notify_stop.sh"
}
3.脚本 notify_master.sh(脚本方面看实际情况 可不配置 不配置则不执行)
#!/bin/bash
redis_dir=/usr/local/redis
redis_cli=${redis_dir}/src/redis-cli
logfile=/usr/local/keepalived/keepalived.log
echo "$(date +%T) redis run slaveof on one " >> $logfile
$redis_cli slaveof no one >> $logfile 2>&1
4.脚本 notify_check.sh
#!/bin/bash
redis_dir=/usr/local/redis
redis_cli=${redis_dir}/src/redis-cli
logfile=/usr/local/keepalived/keepalived.log
alive=$(/home/vdc/base/redis/src/redis-cli ping)
echo "$(date +%T)redis check " >> $logfile
if [ $alive == "PONG" ];then
echo "$(date +%T) redis is alive " >> $logfile
exit 0
else
echo "$(date +%T) redis is no alive" >> $logfile
exit 1
fi
5.脚本 notify_backup.sh
#!/bin/bash
redis_dir=/usr/local/redis
redis_cli=${redis_dir}/src/redis-cli
logfile=/usr/local/keepalived/keepalived.log
echo "$(date +%T )run slaveof from slave" >> $logfile
$redis_cli slaveof $1 $2 >> $logfile 2>&1
6.脚本 notify_fault.sh
#!/bin/bash
redis_dir=/usr/local/redis
logfile=/usr/local/keepalived/keepalived.log
redis_cli=${redis_dir}/src/redis-cli
echo "$(date +%T) redis or nginx fault " >> $logfile
7.脚本 notify_stop.sh
#!/bin/bash
redis_dir=/usr/local/redis
logfile=/usr/local/keepalived/keepalived.log
echo "$(date +%T) keepalived is stop " >> $logfile
三、启动
/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf
#启动的参数还有很多,可以用一下命令查看
/usr/local/keepalived/sbin/keepalived -help
四、非抢占式
keepalived的HA分为抢占模式和非抢占模式,抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来。
非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP。
主备的配置文件都如下, 其余不变:
!Configuration File for keepalived
global_defs {
router_id LVS_1
}
vrrp_script notify_check {
script "/usr/local/keepalived/scripts/notify_check.sh"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface eth0 #虚IP地址放置的网卡位置
virtual_router_id 51 #两机相同
priority 99 #优先级 主机必须大于备机
nopreempt #非抢占式 优先级无效 ,先启动的为主机
advert_int 1 #主备通讯时间间隔
authentication { #认证 两机相同
auth_type PASS
auth_pass 1111
}
track_script {
notify_check
}
virtual_ipaddress { #虚拟ip
198.168.0.222
}
#脚本 IP 为另一台ip
notify_master "/usr/local/keepalived/scripts/notify_master.sh 192.168.0.2 6379"
notify_backup "/usr/local/keepalived/scripts/notify_backup.sh 192.168.0.2 6379"
notify_fault "/usr/local/keepalived/scripts/notify_fault.sh 192.168.0.2 6379"
notify_stop "/usr/local/keepalived/scripts/notify_stop.sh"
}
五、防火墙会导致脑裂现象
firewalld 防火墙
#指定keepalived配置的网卡:enp0s3,固定的VRRP广播地址:224.0.0.18
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
# 查看配置的规则
firewall-cmd --direct --get-rules ipv4 filter INPUT
firewall-cmd --direct --get-rules ipv4 filter OUTPUT
suse12的防火墙
linux-eauv:~ vi /etc/sysconfig/SuSEfirewall2
将下面这行的注释去掉
FW_CUSTOMRULES="/etc/sysconfig/scripts/SuSEfirewall2-custom"
并将下面这行注释掉
FW_CUSTOMRULES=""
linux-eauv:~ vi /etc/sysconfig/scripts/SuSEfirewall2-custom
fw_custom_before_port_handling() {
# these rules will be loaded after the anti-spoofing and icmp handling
# and after the input has been redirected to the input_XXX and
# forward_XXX chains and some basic chain-specific anti-circumvention
# rules have been set,
# but before any IP protocol or TCP/UDP port allow/protection rules
# will be set.
# You can use this hook to allow/deny certain IP protocols or TCP/UDP
# ports before the SuSEfirewall2 generated rules are hit.
#添加下面这行
iptables -A INPUT -p vrrp -j ACCEPT -d 224.0.0.18
true
}
linux-eauv:~ rcSuSEfirewall2 restart