主机:192.168.111.51备机:192.168.111.52LVS VIP: 192.168.111.50
yum -y install gcc gcc-c++ make popt popt-devel libnl libnl-devel popt-static openssl-devel kernel-devel
建立一个内核信息的符号链接,如下:
ln -s /usr/src/kernels/3.10.0-514.el7.x86_64 /usr/src/linux
以下是在安装keepalived时的依赖关系:
Installed: keepalived.x86_64 0:1.2.13-9.el7_3 Dependency Installed: lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-24.el7_3.2 net-snmp-libs.x86_64 1:5.7.2-24.el7_3.2
修改iptables配置,在主机上放开备机进来的流量,在备机也做相似设置:
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface enp0s3 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --reload注:在备机上,该配置文件仅有router_id和priority的值不同。其它值均一致。
以下为主机上的配置举例:
global_defs { notification_email { #system@hongshutech.com } notification_email_from lvs@baiwutong.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_51 } vrrp_instance VI_1 { state BACKUP nopreempt interface enp0s3 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1207 } virtual_ipaddress { 192.168.111.50 } } virtual_server 192.168.111.50 8888 { delay_loop 6 lb_algo wrr lb_kind DR #persistence_timeout 2 protocol TCP real_server 192.168.111.61 8888 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8888 } } real_server 192.168.111.62 8888 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8888 } } } virtual_server 192.168.111.50 8000 { delay_loop 6 lb_algo wrr lb_kind DR #persistence_timeout 2 protocol TCP real_server 192.168.111.61 8000 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8000 } } real_server 192.168.111.62 8000 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8000 } } }启动keepalived服务
# systemctl start keepalived查看keepalived服务状态
# systemctl status keepalived设置为随系统自启动
# systemctl enable keepalived查看系统网卡VIP是否已经生效
# ip a抓包观察是否有主机定时发出的VRRP包
# tcpdump -p vrrp -n tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on enp0s3, link-type EN10MB (Ethernet), capture size 65535 bytes 10:16:13.375399 IP 192.168.111.51 > 224.0.0.18: VRRPv2, Advertisement, vrid 50, prio 100, authtype simple, intvl 1s, length 20 10:16:14.376542 IP 192.168.111.51 > 224.0.0.18: VRRPv2, Advertisement, vrid 50, prio 100, authtype simple, intvl 1s, length 20 10:16:15.377596 IP 192.168.111.51 > 224.0.0.18: VRRPv2, Advertisement, vrid 50, prio 100, authtype simple, intvl 1s, length 20 10:16:16.378590 IP 192.168.111.51 > 224.0.0.18: VRRPv2, Advertisement, vrid 50, prio 100, authtype simple, intvl 1s, length 20对keepalived的主备切换、VIP地址漂移进行测试。
# systemctl stop keepalived # systemctl start keepalived注:为减少服务自动抢占对业务的影响,LVS服务是配置为运行在非抢占模式下。
以下为lvs_real_server.sh的内容,注意要更新VIP参配置:
vi lvs_real_server.sh #!/bin/bash VIP=192.168.111.50 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP 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 "Real Server 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 "Real Server Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0在完成LVS部署后,后端提供服务的应用可能还没有上线。这时需要预先测试LVS的负载均衡功能是否正常。方法是使用以下命令,在每个后端节点上临时启动一个http server,服务端口设置为与LVS负载均衡服务相对应的端口号即可。使用后,停掉该命令即可。