本文共 2705 字,大约阅读时间需要 9 分钟。
LVS可以实现负载均衡,但是不能够进行健康检查,比如一个rs出现故障,LVS 仍然会把请求转发给故障的rs服务器,这样就会导致请求的无效性。keepalive 软件可以进行健康检查,而且能同时实现 LVS 的高可用性,解决 LVS 单点故障的问题,其实 keepalive 就是为 LVS 而生的。
LVS-DR+Keepalived
一、实验环境:
Keepalived1 + lvs1(Director1):192.168.0.48Keepalived2 + lvs2(Director2):192.168.0.58Real server1:192.168.0.18Real server2:192.168.0.28VIP: 192.168.0.38
二、安装系统软件
LB+LVS+Keepalived的两个节点 yum install ipvsadm keepalived -yRS+Nginx的两个节点 yum install epel-release -y yum install nginx -y chkconfig nginx on service nginx start
三、设置配置脚本
两个RS节点# vim /usr/local/sbin/lvs_dr_rs.sh#! /bin/bashvip=192.168.0.38ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce2节点rs 上分别执行脚本:bash /usr/local/sbin/lvs_dr_rs.sh两个keepalived节点:Master节点:vim /etc/keepalived/keepalived.confvrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.38 }}virtual_server 192.168.0.38 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 0 protocol TCPreal_server 192.168.0.18 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }real_server 192.168.0.28 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }}backup节点:vim /etc/keepalived/keepalived.confvrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.38 }}virtual_server 192.168.0.38 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 0 protocol TCPreal_server 192.168.0.18 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }real_server 192.168.0.28 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }}keepalived的2个节点执行如下命令,开启转发功能:# echo 1 > /proc/sys/net/ipv4/ip_forward先主后从分别启动keepaliveservice keepalived start
三、验证结果
1、访问VIP,轮询到两个后端web服务器。2、LVS主节点挂了,不影响服务的正常运行。转载于:https://blog.51cto.com/gdutcxh/2109249