KeepAlived最初是为LVS设计,用来管理并监控LVS集群系统中各个服务节点的状态,后来加入了VRRP功能,发展一个多功能、通用的轻量级高可用组件,可以为ipvs、nginx、haproxy等诸多服务提供高可用功能。
专为LVS和HA设计的一款健康检查工具
支持故障自动切换(Failover)
支持节点健康状态检查(Health Checking)
官方网站:http://www.keepalived.org
实验环境:
【1】DR调度服务器 192.168.100.129 keepalived ipvsadm
【3】DR调度服务器 192.168.100.128 keepalived ipvsadm
【5】节点服务器 192.168.100.120 httpd
【6】节点服务器 192.168.100.130 httpd
【2】win 10客户端 192.168.100.50
漂移地址:192.168.100.10
//keepalived:双机热备要用到的,ipvsadm:调度管理要用
[root@lvs01 ~]# yum install keepalived ipvsadm -y
[root@lvs02 ~]# yum install keepalived ipvsadm -y
'//尾行插入下段配置'
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
#重载配置,使之生效
[root@lvs01 ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
配置VIP网卡跟真实网卡信息**【仅主机模式】**
虚拟网卡:ens33:0 真实网卡ens33
[root@lvs01 ~]# cd /etc/sysconfig/network-scripts/
[root@lvs01 network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0
[root@lvs01 network-scripts]# vim ifcfg-ens33:0 //编辑虚拟IP地址
//删除原来内容 添加以下内容
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
#配置真实IP
BOOTPROTO=static //修改为static
---省略信息
IPADDR=192.168.100.129 //真实地址为192.168.100.129
GATEWAY=192.168.100.1
NETMASK=255.255.255.0
...................lvs02配置网卡----------------------------
[root@lvs02 network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@lvs02 network-scripts]# vim ifcfg-ens33:0
//一样的操作
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
//配置ens33
IPADDR=192.168.100.128
GATEWAY=20.0.0.2
NETMAKE=255.255.255.0
//使用无法重启网络或ping不通
systemctl start NetworkManager
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10 ##虚拟ip
RIP1=192.168.100.120 ##真实web服务器ip
RIP2=192.168.100.130
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm ##保存配置
systemctl start ipvsadm ##启动服务
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
##设置ens33:0地址,广播地址,子网掩码,并开启
/sbin/route add -host $VIP dev ens33:0 ##添加路由网段信息
/sbin/ipvsadm -A -t $VIP:80 -s rr ##指定虚拟服务访问入口,指定轮询算法
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g ##指定真实服务器,dr模式
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
/sbin/ipvsadm -C ##清空缓存
systemctl stop ipvsadm ##关闭服务
ifconfig ens33:0 down ##关闭接口
route del $VIP ##删除路由信息
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then ##判断文件存在与否决定状态
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
#增加执行权限
[root@lvs01 network-scripts]# chmod +x /etc/init.d/dr.sh
//开启dr服务配置
[root@lvs01 network-scripts]# service dr.sh start
ipvsadm starting --------------------[ok]
//关闭或关闭防护功能
[root@lvs01 network-scripts]# setenforce 0
[root@lvs01 network-scripts]# systemctl restart network
[root@lvs01 network-scripts]# systemctl stop
[root@lvs02 network-scripts]# systemctl restart network
[root@shuai ~]# yum -y install httpd
[root@shuai02 ~]# yum -y install httpd
[root@shuai02 network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@shuai02 network-scripts]# vim ifcfg-lo:0
//编写以下内容
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes
IPADDR=192.168.100.120
GATEWAY=192.168.100.1
NETMASK=255.255.255.0
--------------配置shuai02节点服务器-------------------
[root@shuai ~]# cd /etc/sysconfig/network-scripts/
[root@shuai network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@shuai network-scripts]# vim ifcfg-lo:0
//编写lo服务器
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes
//编辑ens33网卡
IPADDR=192.168.100.130
GATEWAY=192.168.100.1
NETMASK=255.255.255.0
#重启两台接节点服务器的网卡
[root@shuai network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
节点服务器配置抑制ARP响应
两台通用的配置
#!/bin/bash
VIP=192.168.100.10
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore '//arp忽略'
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore '//arp开启'
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
//增加执行权限
[root@shuai network-scripts]# chmod +x /etc/init.d/wed.sh
#复制脚本文件到shuai01节点服务器
[root@shuai init.d]# scp wed.sh root@192.168.100.120:/etc/init.d/
root@192.168.100.120's password:
wed.sh 100% 1440 2.0MB/s 00:00
[root@shuai network-scripts]# systemctl stop firewalld.service
[root@shuai network-scripts]# setenforce 0
[root@shuai network-scripts]# service web.sh start
[root@shuai network-scripts]# service wed.sh start
RealServer Start OK
#编写shui首页信息
[root@shuai init.d]# cd /var/www/html/
[root@shuai html]# vim index.html
//编写
<h1>this is shuai web</h1>
#编写shuai02
<h1>this is shuai02</h1>
#重启两台服务
systemctl start httpd
netstat -ntap | grep httpd
tcp6 0 0 :::80 :::* LISTEN 16174/httpd
[root@lvs1 ~]# vim /etc/keepalived/keepalived.conf
...省略内容
router_id LVS_02 '//router_id不能相同'
...省略内容
vrrp_instance VI_1 {
state BACKUP '//此处选择为BACKUP备服务器'
interface ens33
virtual_router_id 10
priority 90 '//优先级需要低于主服务器'
...省略内容
'//其他配置都相同'
[root@lvs01 keepalived]# systemctl start keepalived
[root@lvs02 keepalived]# systemctl start keepalived