前言部分来源于网络
背景:
随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案吗?答案是肯定的!有!我们利用LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。
1、LVS+Keepalived 介绍
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
2、网站负载均衡拓扑图
3、在2台Director Server(LVS_Master与)上分别配置LVS+Keepalived
实验环境是CentOS6.2,并且配置YUM
keepalived:http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
ipvsadm: http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz 注意:(for kernel 2.6.28-rc3 or later)
popt-static:http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm
首先下载popt-static:具体原因可以查看http://crazylinux.blog.51cto.com/259244/811591
[root@host1 ~]# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
[root@host1 ~]# yum -y install kernel-devel make gcc openssl-devel libnl* popt*
[root@host1 ~]# ln -s /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux
[root@host1 ~]# tar -zxvf ipvsadm-1.26.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/ipvsadm-1.26/
[root@host1 ipvsadm-1.26]# make
[root@host1 ipvsadm-1.26]# make install
[root@host1 ~]# tar -zxvf keepalived-1.2.2.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/keepalived-1.2.2/
[root@host1 keepalived-1.2.2]# ./configure
[root@host1 keepalived-1.2.2]# make
[root@host1 keepalived-1.2.2]# make install
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@host1 keepalived-1.2.2]# mkdir /etc/keepalived
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@host1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
4、在2台Director Server(LVS_Master与)上分别配置Keepalived
- [root@host1 ~]# less /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- router_id LVS_MASTER #网上资料说这个值也需要修改,具体不详,之前我们线上的主备就一直是一样的 ^ ^还是修改一下吧!
- }
- vrrp_instance VI_1 {
- state MASTER #LVS_Backup上修改成BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 #LVS_Backup上修改成80
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.10.100
- }
- }
- virtual_server 192.168.10.100 80 {
- delay_loop 6
- lb_algo rr
- lb_kind DR
- nat_mask 255.255.255.0
- persistence_timeout 50
- protocol TCP
- real_server 192.168.10.3 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- connect_port 80
- }
- }
- real_server 192.168.10.4 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- connect_port 80
- }
- }
- }
BACKUP服务器同上配置,先安装lvs再按装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可。
5、分别在2台Real Server上面编写脚本并启动
- [root@host3 init.d]# cat /etc/init.d/realserver.sh
- #!/bin/bash
- SNS_VIP=192.168.10.100
- . /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
6、在2台RealServer中分别配置HTTP并启动realserver.sh
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# cat index.html
<h1>RealServer 192.168.10.3</h1>
[root@host3 html]# /etc/init.d/httpd start
[root@host3 ~]# /etc/init.d/realserver.sh start
另一台机器配置一样,过程略、、、
7、测试:
分别启动Keepalived
[root@host1 ~]# chkconfig keepalived on
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.100:http rr persistent 50
-> 192.168.10.3:http Route 1 0 0
-> 192.168.10.4:http Route 1 0 0
先关闭LVS_Master的Keepalived,观察LVS_Backup的日志
[root@host2 ~]# tailf /var/log/messages
Mar 21 07:22:28 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 21 07:22:29 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.100 added
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.100
在测试机上访问:
启动LVS_Master,在查看LVS_Backup日志
[root@host2 ~]# tailf /var/log/messages
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 21 07:26:20 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.100 removed
一切访问正常~!祝各位好运!~