Keepalived是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健康检查功能–判断LVS负载调度器、节点服务器的可用性,及时隔离并替换新的服务器,当故障主机恢复后将其重新加入集群。

实验环境

客户端:192.168.136.130
虚拟IP:192.168.136.100
LVS1:192.168.136.133
LVS2: 192.168.136.137
RS1: 192.168.136.134
RS2: 192.168.136.135

#关闭防火墙
systemctl stop firewalld
#临时关闭selinux
setenforce 0

LVS配置

#下载lvs和keepalived
yum install -y keepalived -y ipvsadm
#修改LVS1的/etc/keepalived下的keepalived.conf
global_defs {
router_id LVS-1
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.136.100
}
}

virtual_server 192.168.136.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP

real_server 192.168.136.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.136.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#修改LVS2的/etc/keepalived下的keepalived.conf
global_defs {
router_id LVS-2
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.136.100
}
}

virtual_server 192.168.136.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP

real_server 192.168.136.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.136.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

#开机重启
systemctl enable keepalived && systemctl enable ipvsadm
#重启
reboot

RS1服务器配置

#安装httpd服务
yum install -y httpd
#写页面
echo "web1" > /var/www/html/index.html
#启动服务,开机自启
systemctl start httpd && systemctl enable httpd
#配置lo:0回管口
cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0
#修改ifcfg-lo:0文件
DEVICE=lo:0
IPADDR=192.168.136.100
NETMASK=255.255.255.255
ONBOOT=yes
#在/etc/rc.local里面添加
/sbin/route add -host 192.168.136.100 dev lo:0
#在/etc/sysctl.conf添加
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
#重启
reboot

RS2配置

#安装httpd服务
yum install -y httpd
#写页面
echo "web2" > /var/www/html/index.html
#启动服务,开机自启
systemctl start httpd && systemctl enable httpd
#配置lo:0回管口
cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0
#修改ifcfg-lo:0文件
DEVICE=lo:0
IPADDR=192.168.136.100
NETMASK=255.255.255.255
ONBOOT=yes
#在/etc/rc.local里面添加
/sbin/route add -host 192.168.136.100 dev lo:0
#在/etc/sysctl.conf添加
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
#重启
reboot