码迷,mamicode.com
首页 > 其他好文 > 详细

借助LVS+Keepalived实现负载均衡

时间:2016-06-30 16:07:30      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

1.测试环境4台server,全部初始化一下,该关的关了

# vim /etc/hosts
192.168.1.101   lvs-master
192.168.1.102   lvs-slave
192.168.1.161   lvs-web1
192.168.1.162   lvs-web2

yum clean all
systemctl stop firewalld.service
systemctl disable firewalld.service 
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

2. 在两台web服务器上配置httpd和realserver脚本

[root@host161 ~]# yum -y install httpd
[root@host161 ~]# systemctl start httpd
[root@host161 ~]# systemctl enable httpd
ln -s ‘/usr/lib/systemd/system/httpd.service‘ ‘/etc/systemd/system/multiuser.target.wants/httpd.service‘
[root@host161 ~]# cat /var/www/html/index.html 
hello this lvs-web1


[root@host161 ~]# vim /etc/init.d/realserver
SNS_VIP=192.168.1.200
/etc/rc.d/init.d/functions
case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_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 $SNS_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
[root@host161 ~]# chmod 755 /etc/init.d/realserver
[root@host161 ~]# /etc/init.d/realserver start

3.在两台负载均衡server上配置keepalived,back节点只需要改动state和priority

[root@host101 keepalived]# yum install -y keepalived	

[root@host101 keepalived]# vim /etc/keepalived/keepalived.conf
global_defs {  
   notification_email {  
         mail@126.com  
   }  
   notification_email_from sns-lvs@gmail.com  
   smtp_server 192.168.80.1  
   smtp_connection_timeout 30
   router_id LVS_DEVEL  # 设置lvs的id,在一个网络内应该是唯一的
}  
vrrp_instance VI_1 {  
    state MASTER   #指定Keepalived的角色,MASTER为主,BACKUP为备          
    interface eno16777736  #指定Keepalived的角色,MASTER为主,BACKUP为备
    virtual_router_id 51  #虚拟路由编号,主备要一致
    priority 100  #定义优先级,数字越大,优先级越高,主DR必须大于备用DR    
    advert_int 1  #检查间隔,默认为1s
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    virtual_ipaddress {  
        192.168.1.200  #定义虚拟IP(VIP)为192.168.2.33,可多设,每行一个
    }  
}  
# 定义对外提供服务的LVS的VIP以及port
virtual_server 192.168.1.200 80 {  
    delay_loop 6 # 设置健康检查时间,单位是秒                    
    lb_algo wrr # 设置负载调度的算法为wlc                   
    lb_kind DR # 设置LVS实现负载的机制,有NAT、TUN、DR三个模式   
    nat_mask 255.255.255.0                
    persistence_timeout 0          
    protocol TCP                  
    real_server 192.168.1.161 80 {  # 指定real server1的IP地址
        weight 3   # 配置节点权值,数字越大权重越高              
        TCP_CHECK {  
        connect_timeout 10         
        nb_get_retry 3  
        delay_before_retry 3  
        connect_port 80  
        }  
    }  
    real_server 192.168.1.162 80 {  # 指定real server2的IP地址
        weight 3  # 配置节点权值,数字越大权重越高  
        TCP_CHECK {  
        connect_timeout 10  
        nb_get_retry 3  
        delay_before_retry 3  
        connect_port 80  
        }  
     }  
}
[root@host101 keepalived]# systemctl start keepalived

4. 通过浏览器访问http://192.168.1.200进行测试,包括可以分别关闭主/备节点上的keepalived和web服务器的httpd进行测试。结果发现web流量可以在两台web上来回漂移

参考文章,写的很全的  http://www.cnblogs.com/edisonchou/p/4281978.html

借助LVS+Keepalived实现负载均衡

标签:

原文地址:http://www.cnblogs.com/oskb/p/5630232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!