介绍
Linux:keepalived 双热备份(基础备份web)_鲍海超-GNUBHCkalitarro的博客-CSDN博客https://blog.csdn.net/w14768855/article/details/132815057?spm=1001.2014.3001.5501
环境
一台 centos7 keepalived ipvsadm (主服务器) 192.168.254.1
一台 centos7 keepalived ipvsadm (备份服务器) 192.168.254.2
漂移ip : 192.168.254.66
一台 centos7 web (web1) 192.168.254.3
一台 centos7 web (web2) 192.168.254.3
在主服务器上的配置
yum -y install keepalived ipvsadm
systemctl enable keepalived.service
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
写入
bal_defs {
router_id tarro1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123123
}
virtual_ipaddress {
192.168.254.66
}
}
virtual_server 192.168.254.66 80 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.254.3 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.254.4 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
systemctl restart keepalived
modprobe ip_vs
echo "modprobe ip_vs" >>/etc/rc.local
备份服务器上的配置
yum -y install ipvsadm keepalived
systemctl enable keepalived.service
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
写入
bal_defs {
router_id tarro2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 1
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123123
}
virtual_ipaddress {
192.168.254.66
}
}
virtual_server 192.168.254.66 80 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.254.3 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.254.4 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
modprobe ip_vs
echo "modprobe ip_vs" >>/etc/rc.local
systemctl restart keepalived
web1服务器配置
yum -y install httpd
systemctl enable httpd
systemctl start httpd
echo "<h1>192.168.254.3" > /var/www/html/index.html
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
写入
DEVICE=lo:0
IPADDR=192.168.254.66
NETMASK=255.255.255.255
ONBOOT=yes
systemctl restart network
echo "route add -host 192.168.254.66 dev lo:0" >>/etc/rc.local
route add -host 192.168.254.66 dev lo:0
vim /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
sysctl -p
web2服务器配置
yum -y install httpd
systemctl enable httpd
systemctl start httpd
echo "<h1>192.168.254.4" > /var/www/html/index.html
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
写入
DEVICE=lo:0
IPADDR=192.168.254.66
NETMASK=255.255.255.255
ONBOOT=yes
systemctl restart network
echo "route add -host 192.168.254.66 dev lo:0" >>/etc/rc.local
route add -host 192.168.254.66 dev lo:0
vim /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
sysctl -p
测试效果
现在主服务器上有漂移地址和虚拟主机列表
备份主机上也有虚拟列表 但是没有66的漂移地址就对了,因为现在我们的主服务器还在正常工作
现在访问192.168.254.66 就可以被lvs服务器把流量分到两个web服务器上
我们把254.4关闭一下
一下就到3了 !!! 这里我只是在先效果,实际的环境lvs并不是备份,而是负载均衡,我们的lvs主服务器会把流量分到两个web站点上,然而两个web站点在实际中界面内容是一样的,这里只是为了看效果
现在可以看到上面lvs效果已经成功了
现在去测试keepalived效果
我们把主服务器关机
在主服务器关机的瞬间,我们的备份服务器立马会有漂移地址,我们再去访问254.66
依然不影响访问
我们再开启主服务器
漂移地址立马就回到主服务器了,测试成功