简介

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

实验环境

主机 操作系统 IP地址
DR1主调度服务器 CentOS 7.0 x86_64 192.168.100.156
DR2备调度服务器 CentOS 7.0 x86_64 192.168.100.157
web1节点服务器 CentOS 7.0 x86_64 192.168.100.133
web2节点服务器 CentOS 7.0 x86_64 192.168.100.134
VIP虚拟IP地址 -------- 192.168.100.10
Win 7 客户端 Windows 7 192.168.100.50

开始部署

DR1主调度服务器

1.安装keepalived、ipvsadm软件包
  yum install keepalived ipvsadm -y
2.修改/proc响应参数
  vi /etc/sysctl.conf
net.ipv4.ip_forward=1                                    #开启路由转发功能
net.ipv4.conf.all.send_redirects = 0              #proc响应关闭重定向功能
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

3.立即生效
  sysctl -p
4.创建虚拟VIP
    cd /etc/sysconfig/network-scripts/
    cp ifcfg-ens33 ifcfg-ens33:0
5.编辑虚拟网卡    
  vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
6.启用虚拟网卡
ifup ifcfg-ens33:0
7.制作dr.sh脚本文件
  vim /etc/init.d/dr.sh
#!/bin/bash
GW=192.168.100.1                          #网关地址
VIP=192.168.100.10                        #VIP虚拟地址
RIP1=192.168.100.133                   #web1节点服务器IP
RIP2=192.168.100.134                  #web2节点服务器IP
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm       #保存ipvsadm策略
systemctl start ipvsadm 
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/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
/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
8.给dr.sh脚本赋执行权限,启动脚本
chmod +x dr.sh
service dr.sh start
9.查看验证lvs节点状态
  ipvsadm -ln
10.修改keepalived配置文件如下
vim /etc/keepalived/keepalived.conf

global_defs {
...
smtp_server 127.0.0.1                             #指向本地
router_id LVS_02                                     #指定服务器名称
... 
}
vrrp_instance VI_1 {
state BACKUP                                       #主份服务器是Master
interface ens33
virtual_router_id 10                                   #主备组号相同
priority 99                                                 #优先级备服务器小于主服务器
... 
auth_pass abc123                                 #验证密码,主备相同
...
virtual_ipaddress {
192.168.100.10                                      #虚拟VIP地址
}
...
virtual_server 192.168.100.10 80 {         #虚拟服务器VIP、端口
...
lb_kind DR
...
real_server 192.168.100.133 80 {           #真实web1节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80 
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
} 
} 
real_server 192.168.100.134 80 { #真实web2节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
} 
} 
}

11.启动keepalived服务
 systemctl start keepalived

DR2从调度服务器

1.安装keepalived、ipvsadm软件包
  yum install keepalived ipvsadm -y
2.修改/proc响应参数
  vi /etc/sysctl.conf
net.ipv4.ip_forward=1                                    #开启路由转发功能
net.ipv4.conf.all.send_redirects = 0              #proc响应关闭重定向功能
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

3.立即生效
  sysctl -p
4.制作dr.sh脚本文件
  vim /etc/init.d/dr.sh
#!/bin/bash
GW=192.168.100.1                          #网关地址
VIP=192.168.100.10                        #VIP虚拟地址
RIP1=192.168.100.133                   #web1节点服务器IP
RIP2=192.168.100.134                  #web2节点服务器IP
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm       #保存ipvsadm策略
systemctl start ipvsadm 
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/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
/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
5.给dr.sh脚本赋执行权限,启动脚本
chmod +x dr.sh
service dr.sh start
6.查看验证lvs节点状态
  ipvsadm -ln
7.修改keepalived配置文件如下
vim /etc/keepalived/keepalived.conf

global_defs {
...
smtp_server 127.0.0.1                             #指向本地
router_id LVS_02                                     #指定服务器名称
... 
}
vrrp_instance VI_1 {
state MASTER                                        #备份服务器是BACKUP
interface ens33
virtual_router_id 10                                   #主备组号相同
priority 100                                              #优先级备服务器小于主服务器
... 
auth_pass abc123                                 #验证密码,主备相同
...
virtual_ipaddress {
192.168.100.10                                      #虚拟VIP地址
}
...
virtual_server 192.168.100.10 80 {         #虚拟服务器VIP、端口
...
lb_kind DR
...
real_server 192.168.100.133 80 {           #真实web1节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80 
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
} 
} 
real_server 192.168.100.134 80 { #真实web2节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
} 
} 
}

8.启动keepalived服务
 systemctl start keepalived

Web1节点服务器

1.安装httpd软件包
yum install -y httpd
2.启动httpd服务
systemctl start httpd.service 
3.新建网站首页文件
echo "

this is benet

" > /var/www/html/index.html 4.新建web.sh脚本至服务管理目录,便于service进行管理 vim /etc/init.d/web.sh #!/bin/bash VIP=192.168.100.10 #VIP虚拟IP地址 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 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 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 5.给web.sh脚本赋执行权限 chmod +x web.sh 6.启动web.sh服务 service web.sh start 7.关闭防火墙及selinux systemctl stop firewalld.service setenforce 0

8.测试
LVS+keepalived高可用群集_第1张图片

Web2节点服务器

1.安装httpd软件包
yum install -y httpd
2.启动httpd服务
systemctl start httpd.service 
3.新建网站首页文件
echo "

this is accp web

" > /var/www/html/index.html 4.新建web.sh脚本至服务管理目录,便于service进行管理 vim /etc/init.d/web.sh #!/bin/bash VIP=192.168.100.10 #VIP虚拟IP地址 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 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 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 5.给web.sh脚本赋执行权限 chmod +x web.sh 6.启动web.sh服务 service web.sh start 7.关闭防火墙及selinux systemctl stop firewalld.service setenforce 0

8.测试
LVS+keepalived高可用群集_第2张图片

客户端测试

LVS+keepalived高可用群集_第3张图片
LVS+keepalived高可用群集_第4张图片
等待几十秒后刷新,如果不行,清除浏览器缓存后再试
LVS+keepalived高可用群集_第5张图片