安装keepalived的依赖
yum install -y openssl ipvsadm.x86_64 popt* libnl*
yum install openssl* -y
yum -y install libnfnetlink.x86_64 libnfnetlink-devel.x86_64
官网下载keepalived 解压安装
tar zxvf keepalived-1.3.4.tar.gz
./configure
make && make install
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
vi /etc/keepalived/keepalived.conf
global_defs {
router_id haweb_1
}
vrrp_sync_group VGM {
group {
VI_HA
}
}
vrrp_instance VI_HA {
state MASTER #slave
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 145
priority 100 #90
advert_int 5
authentication {
auth_type PASS
auth_pass zhangbin
}
virtual_ipaddress {
192.168.1.145/24 dev eth0
}
}
在master和slave都同时配置以上步骤。。只是配置文件稍有不同
测试在master死机后slave会马上衔接145继续工作。。
如果不是keepalived死掉而是httpd死掉则需要把陪文写为
vi /etc/keepalived/keepalived.conf
global_defs {
router_id haweb_1
}
vrrp_sync_group VGM {
group {
VI_HA
}
}
vrrp_script chk_http_port {
script "/etc/keepalived/httpd.sh"
interval 1
weight 1
}
vrrp_instance VI_HA {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass zhangbin
}
virtual_ipaddress {
192.168.1.145/24 dev eth0
}
track_script {
chk_http_port
}
}
vi httpd.sh
#!/bin/bash
counter=$(ps -C httpd --no-heading|wc -l)
if [ "${counter}"="0" ]; then
/usr/sbin/httpd
sleep 2
counter=$(ps -C httpd --no-heading|wc -l)
if [ "${counter}"="0" ]; then
/usr/bin/systemctl stop keepalived.service
fi
fi