LVS负载均衡群集——keepalived+DR模式(实战!)
keepalived实现原理
keepalived采用vrrp热备份协议,实现Linux服务器的多机热备功能
vrrp,虚拟路由冗余协议,是针对路由器的一种备份解决方案
keepalivd案列讲解
keepalived可实现多机热备,每个热备组有多台服务器,最常用的就是双机热备
双机热备的故障切换是由虚拟IP地址的漂移来实现,适用于各种应用服务器
DR模式原理
实验环境
CentOS7系统: DR1 主: 192.168.100.2 DR2 备: 192.168.100.20 虚拟IP:192.168.100.10 web1:192.168.100.221 web2:192.168.100.222 win10系统: client:192.168.100.100
1、分别在四台Linux虚拟机上安装实验所需软件包
DR1主: [ ~]# yum install ipvsadm keepalived -y DR2从: [ ~]# yum install ipvsadm keepalived -y web1: [ ~]# yum install httpd -y web2: [ ~]# yum install httpd -y
2、分别将五台虚拟机的网络模式改为仅主机模式(五台操作相同)
3、分别修改四台Linux虚拟机的IP地址
DR1主: [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改为static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.201 #IP地址 NETMASK=255.255.255.0 #子网掩码 GATEWAY=192.168.100.1 #网关 [ ~]# systemctl restart network #重启网络服务 [ ~]# ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.201 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:95:9b:1b txqueuelen 1000 (Ethernet) RX packets 10373 bytes 13584878 (12.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4285 bytes 277473 (270.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
DR2从: [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改为static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.202 #IP地址 NETMASK=255.255.255.0 #子网掩码 GATEWAY=192.168.100.1 #网关 [ ~]# systemctl restart network #重启网络服务 [ ~]# ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.202 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20<link> inet6 fe80::2558:848e:ba08:4183 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:27:1c:3f txqueuelen 1000 (Ethernet) RX packets 10470 bytes 13593710 (12.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4240 bytes 273794 (267.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
web1: [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改为static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=8ecd53ce-afdb-46f8-b7ff-b2f428a3bc8f DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.221 #IP地址 NETMASK=255.255.255.0 #子网掩码 GATEWAY=192.168.100.1 #网关 [ ~]# systemctl restart network #重启网络服务 [ ~]# ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.221 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::c776:9d00:618:88f2 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:34:07 txqueuelen 1000 (Ethernet) RX packets 11508 bytes 14937643 (14.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3932 bytes 254623 (248.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
web2: [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改为static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=09073596-a7cf-404c-a098-28c3ff58c44b DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.222 #IP地址 NETMASK=255.255.255.0 #子网掩码 GATEWAY=192.168.100.1 #网关 [ ~]# systemctl restart network #重启网络服务 [ ~]# ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.222 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::5134:22f5:842b:5201 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:0d:f1:75 txqueuelen 1000 (Ethernet) RX packets 11425 bytes 14931323 (14.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4454 bytes 286016 (279.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4、修改win10系统的IP地址,并关闭防火墙
DR1主服务器配置
1、配置路由转发功能
[ ~]# vim /etc/sysctl.conf net.ipv4.ip_forward=1 #配置路由转发功能 #proc响应关闭重定向功能 net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.default.send_redirects=0 net.ipv4.conf.ens33.send_redirects=0 [ ~]# 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 [ ~]#
2、配置虚拟网卡
[ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0 #复制虚拟网卡的配置文件 [ network-scripts]# vim ifcfg-ens33:0 #添加配置文件信息,原有的信息删除 DEVICE=ens33:0 ONBOOT=yes IPADDR=192.168.100.10 NETMASK=255.255.255.0 [ network-scripts]# systemctl restart network #重启网络服务 [ network-scripts]# ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.201 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:95:9b:1b txqueuelen 1000 (Ethernet) RX packets 11455 bytes 13700437 (13.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4713 bytes 330899 (323.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 #虚拟网卡配置成功 ether 00:0c:29:95:9b:1b txqueuelen 1000 (Ethernet)
3、创建脚本便于service管理
[ network-scripts]# cd /etc/init.d/ [ init.d]# vim dr.sh #!/bin/bash GW=192.168.100.1 #网关 VIP=192.168.100.10 #虚拟ip RIP1=192.168.100.221 #真实web1服务器ip RIP2=192.168.100.222 #真实web2服务器ip 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 #指定真实服务器web1,dr模式 /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g #指定真实服务器web2,dr模式 echo "ipvsadm starting ---------------------[ok]" ;; stop) /sbin/ipvsadm -C #清空缓存 systemctl stop ipvsadm #关闭服务 ifconfig ens33:0 down #关闭接口 route del $VIP #删除路由信息 echo "ipvsadm stoped--------------------[ok]" ;; status) if [ ! -e /var/lock/subsys/ipvsadm ];then #判断文件存在与否决定状态 echo "ipvsadm stoped--------------" exit 1 else echo "ipvsadm Runing-----------[ok]" fi ;; *) echo "Usage: $0 {start|stop|status}" exit 1 esac exit 0 [ init.d]# chmod +x dr.sh #给执行权限 [ init.d]# service dr.sh start #开启服务 ipvsadm starting ---------------------[ok] [ init.d]# systemctl stop firewalld.service #关闭防火墙 [ init.d]# setenforce 0 #关闭增强型安全功能 [ init.d]#
4、配置DR1服务器keepalived部署
[ ~]# cd /etc/keepalived/ [ keepalived]# ls keepalived.conf [ keepalived]# vim keepalived.conf smtp_server 127.0.0.1 #指向本地 router_id LVS_01 #指定名称,备份服务器不同名称 vrrp_instance VI_1 { state MASTER #主服务器,备份服务器是BACKUP interface ens33 virtual_router_id 10 #组号相同 priority 100 #优先级,主服务器优先级应大于备份服务器 advert_int 1 authentication { auth_type PASS #验证类型 auth_pass 1111 #验证密码 } virtual_ipaddress { 192.168.100.10 #虚拟IP地址 } } virtual_server 192.168.100.10 80 { #虚拟服务器 delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.100.221 80 { #真实服务器 weight 1 TCP_CHECK { connect_port 80 #连接端口 connect_timeout 3 #连接超时 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.100.222 80 { #真实服务器 weight 1 TCP_CHECK { connect_port 80 #连接端口 connect_timeout 3 #连接超时 nb_get_retry 3 delay_before_retry 3 } } } [ keepalived]# systemctl start keepalived.service #开启服务 [ keepalived]#
DR2从服务器配置
1、配置路由转发功能
[ ~]# vim /etc/sysctl.conf net.ipv4.ip_forward=1 #配置路由转发功能 #proc响应关闭重定向功能 net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.default.send_redirects=0 net.ipv4.conf.ens33.send_redirects=0 [ ~]# 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 [ ~]#
2、配置虚拟网卡
[ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0 #复制虚拟网卡的配置文件 [ network-scripts]# vim ifcfg-ens33:0 #添加配置文件信息,原有的信息删除 DEVICE=ens33:0 ONBOOT=yes IPADDR=192.168.100.10 NETMASK=255.255.255.0 [ network-scripts]# systemctl restart network #重启网络服务 [ network-scripts]# ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.202 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20<link> inet6 fe80::2558:848e:ba08:4183 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:27:1c:3f txqueuelen 1000 (Ethernet) RX packets 15720 bytes 14706442 (14.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6289 bytes 1113172 (1.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 #虚拟网卡配置成功 ether 00:0c:29:27:1c:3f txqueuelen 1000 (Ethernet)
3、创建脚本便于service管理
[ network-scripts]# cd /etc/init.d/ [ init.d]# vim dr.sh #!/bin/bash GW=192.168.100.1 #网关 VIP=192.168.100.10 #虚拟ip RIP1=192.168.100.221 #真实web1服务器ip RIP2=192.168.100.222 #真实web2服务器ip 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 #指定真实服务器web1,dr模式 /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g #指定真实服务器web2,dr模式 echo "ipvsadm starting ---------------------[ok]" ;; stop) /sbin/ipvsadm -C #清空缓存 systemctl stop ipvsadm #关闭服务 ifconfig ens33:0 down #关闭接口 route del $VIP #删除路由信息 echo "ipvsadm stoped--------------------[ok]" ;; status) if [ ! -e /var/lock/subsys/ipvsadm ];then #判断文件存在与否决定状态 echo "ipvsadm stoped--------------" exit 1 else echo "ipvsadm Runing-----------[ok]" fi ;; *) echo "Usage: $0 {start|stop|status}" exit 1 esac exit 0 [ init.d]# chmod +x dr.sh #给执行权限 [ init.d]# service dr.sh start #开启服务 ipvsadm starting ---------------------[ok] [ init.d]# systemctl stop firewalld.service #关闭防火墙 [ init.d]# setenforce 0 #关闭增强型安全功能 [ init.d]#
4、配置DR1服务器keepalived部署
[ ~]# cd /etc/keepalived/ [ keepalived]# ls keepalived.conf [ keepalived]# vim keepalived.conf smtp_server 127.0.0.1 #指向本地 router_id LVS_02 #指定名称,备份服务器不同名称 vrrp_instance VI_1 { state BACKUP #备份服务器BACKUP,主服务器是MASTER interface ens33 virtual_router_id 10 #组号相同 priority 90 #优先级,备份服务器应小于主服务器优先级 advert_int 1 authentication { auth_type PASS #验证类型 auth_pass 1111 #验证密码 } virtual_ipaddress { 192.168.100.10 #虚拟IP地址 } } virtual_server 192.168.100.10 80 { #虚拟服务器 delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.100.221 80 { #真实服务器 weight 1 TCP_CHECK { connect_port 80 #连接端口 connect_timeout 3 #连接超时 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.100.222 80 { #真实服务器 weight 1 TCP_CHECK { connect_port 80 #连接端口 connect_timeout 3 #连接超时 nb_get_retry 3 delay_before_retry 3 } } } [ keepalived]# systemctl start keepalived.service #开启服务 [ keepalived]#
配置web1服务器
1、开启并配置httpd服务
[ ~]# systemctl stop firewalld.service #关闭防火墙 [ ~]# setenforce 0 #关闭增强型安全功能 [ ~]# systemctl start httpd.service #开启httpd服务 [ ~]# cd /var/www/html/ [ html]# echo "this is abc web" > index.html #创建网站页面 [ html]#
2、修改回环网卡的虚拟网卡
[ html]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp ifcfg-lo ifcfg-lo:0 #复制回环网卡配置文件 [ network-scripts]# vim ifcfg-lo:0 #添加配置文件信息,原有的信息删除 DEVICE=lo:0 IPADDR=192.168.100.10 NETMASK=255.255.255.0 ONBOOT=yes
3、添加脚本便于service管理
[ network-scripts]# cd /etc/init.d/ [ init.d]# vim web.sh #!/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 echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/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 Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 [ init.d]# chmod +x web.sh #添加执行权限 [ init.d]# service web.sh start #开启服务 RealServer Start OK [ init.d]# ifup lo:0 #开启网卡 [ init.d]#ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.221 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::c776:9d00:618:88f2 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:34:07 txqueuelen 1000 (Ethernet) RX packets 13293 bytes 15122368 (14.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4692 bytes 360484 (352.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.100.10 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback)
配置web2服务器
1、开启并配置httpd服务
[ ~]# systemctl stop firewalld.service #关闭防火墙 [ ~]# setenforce 0 #关闭增强型安全功能 [ ~]# systemctl start httpd.service #开启httpd服务 [ ~]# cd /var/www/html/ [ html]# echo "this is xyz web" > index.html #创建网站页面 [ html]#
2、修改回环网卡的虚拟网卡
[ html]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp ifcfg-lo ifcfg-lo:0 #复制回环网卡配置文件 [ network-scripts]# vim ifcfg-lo:0 #添加配置文件信息,原有的信息删除 DEVICE=lo:0 IPADDR=192.168.100.10 NETMASK=255.255.255.0 ONBOOT=yes
3、添加脚本便于service管理
[ network-scripts]# cd /etc/init.d/ [ init.d]# vim web.sh #!/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 echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/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 Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 [ init.d]# chmod +x web.sh #添加执行权限 [ init.d]# service web.sh start #开启服务 RealServer Start OK [ init.d]# ifup lo:0 #开启网卡 [ init.d]#ifconfig #查看网卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.222 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::5134:22f5:842b:5201 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:0d:f1:75 txqueuelen 1000 (Ethernet) RX packets 16771 bytes 15510870 (14.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7516 bytes 783039 (764.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.100.10 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback)
测试群集是否搭建成功
1、用win10测试主机访问虚拟IP地址:192.168.100.10
再次刷新页面,可以测试轮询机制
2、模拟DR1主服务器故障,然后再次用win10测试主机访问虚拟IP地址:192.168.100.10
[ keepalived]# ifdown ens33:0 #在DR1主服务器上,关闭虚拟网卡,模拟故障