一、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的实现
在DR 模式的集群中,LVS 负载调度器作为群集的访问入口,但不作为网关使用,服务池中所有节点都各自接入Internet, 发送给客户机的web响应数据包不需要经过lvs负载调度器。
二、实验环境与目标
网站负载均衡集群拓扑图
系统环境(配置好yum源):
[root@host1 ~]# cat /etc/RedHat-release
CentOS release 6.2 (Final)
[root@host1 ~]# uname -a
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
通过部署Apache应用构建Web服务器测试效果,通过部属iSCSI做为存储服务器。
实验目标:
实现Web服务器的备份和冗余,其中任意借点出现故障都会自动切换,保证应用正常运行。
三、LVS调度器的配置
[root@host1 ~]# modprobe ip_vs
[root@host1 ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@host1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.10.100
NETMASK=255.255.255.0
[root@host1 ~]# /etc/init.d/network restart
[root@host1 ~]# ifconfig eth0:0
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:46:D9:DE
inet addr:192.168.10.100 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@host1 ~]# vim /etc/sysctl.conf #避免网络内的ARP解析出现异常,应该关闭Linux内核的重定向参数响应
#在最后添加
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
[root@host1 ~]# sysctl -p
[root@host1 ~]# yum -y install ipvsadm #ipvsadm是负载调度器上使用LVS群集管理工具,通过调用ip_vs模块来添加、删除服务器节点,以及查看群集的运行状态
[root@host1 ~]# ipvsadm -v
ipvsadm v1.25 2008/5/15 (compiled with popt and IPVS v1.2.1)
[root@host1 ~]# mkdir /etc/sysconfig/ipvsadm
[root@host1 ~]# /etc/init.d/ipvsadm restart
[root@host1 ~]# chkconfig ipvsadm on
[root@host1 ~]# yum -y install gcc kernel-devel openssl-devel popt-devel #安装支持库
[root@host1 ~]# tar -zxf keepalived-1.2.2.tar.gz
[root@host1 ~]# cd keepalived-1.2.2
[root@host1 ~]# vi keepalived/libipvs-2.6/ip_vs.h
将#include <sys/types.h>放到#include <linux/types.h>的上面
[root@host1 keepalived-1.2.2]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686/
[root@host1 keepalived-1.2.2]# make
[root@host1 keepalived-1.2.2]# make install
[root@host1 keepalived-1.2.2]# chkconfig --add keepalived
[root@host1 keepalived-1.2.2]# chkconfig keepalived on
[root@host1 keepalived-1.2.2]# cd /etc/keepalived/
[root@host1 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@host1 keepalived]# vim keepalived.conf #文件里面所有内容
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER #从调度器上改为router_id LVS_SLAVE
}
vrrp_instance VI_1 {
state MASTER #从调度器上改为state SLAVE
interface eth0
virtual_router_id 51
priority 100 #从调度器上改为priority 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_make 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.10.3 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
}
}
real_server 192.168.10.4 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
}
}
}
配置负载分配策略
# /etc/init.d/ipvsadm stop
# ipvsadm –A –t 192.168.10.100:80 –s rr
# ipvsadm –a –t 192.168.10.100:80 –r 192.168.10.3 –g –w 1
# ipvsadm –a –t 192.168.10.100:80 –r 192.168.10.4 –g –w 1
# /etc/init.d/ipvsadm save
# chkconfig ipvsadm on
ipvsadm