CentOS 6.2 下搭建LVS(DR)+Keepalived實現高性能高可用負載均衡服務器
前言部分來源於網路
背景:
隨著你的網站業務量的增長你網站的服務器壓力越來越大?需要負載均衡方案!商業的硬件如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上面編寫腳本並啟動 (Windows 請參考另一篇)
[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 #啟動是抑制相關arp動作
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
~]# tail -f /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
~]# tail -f /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
一切訪問正常~!祝各位好運!~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
個人實作上遇到的問題....
1. 在打ipvsadm -lcn 看到 syn_recv一堆,怎麼樣就是不能連到 RIP 的網頁,錯就錯在我把 realserver.sh 的SNS_VIP一直寫成LVS主機的實體ip...如用以上範例來解釋,就是寫成192.168.10.1...就會發生這種情形...
2. 如把realserver.sh stop 停用,然後再start,在連LVS的VIP時,有可能完全沒連線,這時請把 網頁關閉或電腦的 arp 清掉,再開即可
3. syn_recv 問題還有可能就是 keepalived.conf 中的 interface介面設錯(eth1打成eth0等等),也會如此,跨不同的網段(10.1.1的想跨到192.168.1)也會如此,這都是要注意的
相關 LVS 解釋可參考
http://www.lslnet.com/linux/f/docs1/i38/big5277307.htm
~~