Centos6.5搭建lvs(dr)+keepalived集群
一、原理
① client(192.168.6.58)向目标vip发出请求,Director接收。
② VS根据负载均衡算法选择一台active的realserver(假设是192.168.6.236),将此RIP所在网卡的mac地址作为目标mac地址,发送到局域网里。
③ realserver(192.168.6.236)在局域网中收到这个帧,拆开后发现目标IP(VIP)与本地匹配,于是处理这个报文。随后重新封装报文,发送到局域网。 如果client与VS同一网段,那么client(192.168.6.58)将收到这个回复报文。
在Director转发数据包过程中,srcIP与desIP均不变。用户收到的包中,srcIP为VIP,因此在用户看来,是和lvs进行通信,而不知道realserver的存在
二、环境
2.1、配置环境
系统平台:CentOS 6.5
Kernel:2.6.32-431.el6.x86_64
LVS版本:ipvsadm v1.26
keepalived版本:Keepalived v1.2.13
2.2、信息列表
名称 IP netmask gateway
LVS 192.168.6.239 24 192.168.6.254
LVS-BACKUP 192.168.6.240 24 192.168.6.254
LVS-VIP 192.168.6.6 32
Realserver1 192.168.6.236 24 192.168.6.254
Realserver2 192.168.6.237 24 192.168.6.254
所有机器均在同一网段。
三、安装
3.1、lvs与lvs_backup配置
关闭httpd:
[root@lvs Desktop]# service httpd stop
[root@lvs Desktop]# chkconfig httpd off
安装ipvsadm : 在线安装
[root@lvs Desktop]# yum -y install ip_vs
安装keepalived:在线安装
[root@lvs Desktop]# yum -y install keepalived
编辑keepalived.conf:
[root@lvs Desktop]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id KEEPALIVED_LVS
}
vrrp_sync_group KEEPALIVED_LVS {
group {
KEEPALIVED_LVS_WEB
}
}
vrrp_instance KEEPALIVED_LVS_WEB {
state MASTER #BACKUP上修改为BACKUP
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 51
priority 100 #BACKUP上修改为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.6.6
}
}
virtual_server 192.168.6.6 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 5
protocol TCP
real_server 192.168.6.236 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.6.237 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动keepalived:
[root@lvs Desktop]# service keepalived start
查看ipvsadm:
[root@lvs Desktop]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.6.6:80 rr
-> 192.168.6.236:80 Route 3 0 0
-> 192.168.6.237:80 Route 3 0 0
查看ip配置:
[root@lvs Desktop]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:31:53:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.6.239/24 brd 192.168.6.255 scope global eth0
inet 192.168.6.6/32 scope global eth0
inet6 fe80::20c:29ff:fe31:531b/64 scope link
valid_lft forever preferred_lft forever
可以看到vip被绑定到eth0上了。
3.2 realserver配置
开启httpd服务:
[root@slave1 Desktop]# service httpd start
[root@slave1 Desktop]# chkconfig httpd on
关闭防火墙:
[root@slave1 Desktop]# service iptables stop
[root@slave1 Desktop]# chkconfig iptables on
添加测试页面:
[root@slave1 Desktop]# cd /var/www/html
[root@slave1 html]# vi index.html
<h1>slave1</h1>(realserver2:<h1>slave2</h1>)
wq!保存退出。
编辑realserver脚本:
[root@slave1 Desktop]# vi /etc/init.d/realserver
# chkconfig: 2346 10 90
# description: realserver
SNS_VIP=192.168.6.6
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/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
wq!保存退出
改变用户权限:
[root@slave1 Desktop]# chmod 777 /etc/init.d/realserver
添加服务:
[root@slave1 Desktop]# chkconfig --add realserver
启动服务:
[root@slave1 Desktop]# service realserver start
查看ip配置:
[root@slave1 Desktop]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 192.168.6.6/32 brd 192.168.6.6 scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ea:3e:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.6.236/24 brd 192.168.6.255 scope global eth0
inet6 fe80::20c:29ff:feea:3e8f/64 scope link
valid_lft forever preferred_lft forever
可以看到vip添加到了本地回环地址。