LVS是Linux Virtual Server的簡寫,意即Linux虛擬服務器,是一個虛擬的服務器集羣系統。本項目在1998年5月由章文嵩博士成立,是中國國內最先出現的自由軟件項目之一。html
LVS是Linux Virtual Server的簡稱,也就是Linux虛擬服務器, 是一個由章文嵩博士發起的自由軟件項目,它的官方站點是www.linuxvirtualserver.org。如今LVS已是 Linux標準內核的一部分,在Linux2.4內核之前,使用LVS時必需要從新編譯內核以支持LVS功能模塊,可是從Linux2.4內核之後,已經徹底內置了LVS的各個功能模塊,無需給內核打任何補丁,能夠直接使用LVS提供的各類功能。
使用LVS技術要達到的目標是:經過LVS提供的負載均衡技術和Linux操做系統實現一個高性能、高可用的服務器羣集,它具備良好可靠性、可擴展性和可操做性。從而以低廉的成本實現最優的服務性能。前端
使用LVS架設的服務器集羣系統有三個部分組成:最前端的負載均衡層,用Load Balancer表示,中間的服務器羣組層,用Server Array表示,最底端的數據共享存儲層,用Shared Storage表示,在用戶看來,全部的內部應用都是透明的,用戶只是在使用一個虛擬服務器提供的高性能服務。linux
接下來開始部署配置LVS主從:web
免責聲明:小夥伴在上生產環境前先測試有沒有問題哈!!!centos
本次試驗共計使用4臺虛擬機,都是centos6.3.瀏覽器
192.168.146.164tomcat
192.168.146.160服務器
192.168.146.162cookie
192.168.146.163網絡
具體實驗環境配置以下:
192.168.146.101 LVS_VIP
192.168.146.164 LVS_MASTER
192.168.146.160 LVS_SALVE
192.168.146.162 WEB1
192.168.146.163 WEB2
192.168.146.164 LVS_MASTER:
安裝IPVSADM:
[root@localhost ~]# yum -y install ipvsadm
爲了測試方便,暫時關閉防火牆:
[root@localhost ~]# service iptables stop
安裝KeepAlived:
[root@localhost ~]# cd /usr/src
[root@localhost src]# yum -y install openssl-devel
root@localhost src]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
[root@localhost src]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm
[root@localhost src]# yum -y install popt-static-1.13-7.el6.x86_64.rpm
[root@localhost src]# yum -y install kernel-devel make gcc openssl-devel libnl* popt*
[root@localhost src]# ln -s /usr/src/kernels/2.6.32-220.13.1.el6.x86_64/ /usr/src/linux
[root@localhost src]# tar zxvf keepalived-1.2.7.tar.gz
[root@localhost src]# cd keepalived-1.2.7
[root@localhost keepalived-1.2.7]# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/
[root@localhost keepalived-1.2.7]# make && make install
[root@localhost keepalived-1.2.7]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@localhost keepalived-1.2.7]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.2.7]# mkdir /etc/keepalived
[root@localhost keepalived-1.2.7]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost keepalived-1.2.7]# cp /usr/local/sbin/keepalived /usr/sbin/
KeepAlived安裝完畢,而後進行配置。
打開IP Forward 功能(LVS現有三種負載均衡規則都須要打開此功能,若是不打開此功能,下面的配置配得再好都無濟於事。)
打開後修改裏面「net.ipv4.ip_forward = 1」
[root@localhost keepalived-1.2.7]# cat /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
執行以下命令使設置當即生效:
[root@localhost keepalived-1.2.7]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
配置keepalived:
配置文件在這個位置: /etc/keepalived/keepalived.conf
[root@localhost keepalived-1.2.7]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
test@sina.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.146.101
}
}
virtual_server 192.168.146.101 8080 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.146.162 8080 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.146.163 8080 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
至此,keepalived配置完成。
把keepalived加入開機自啓動,並開啓keepalived服務:
[root@localhost keepalived-1.2.7]# chkconfig keepalived on
[root@localhost keepalived-1.2.7]# service keepalived start
Starting keepalived: [ OK ]
查看keepalived進程:
[root@localhost keepalived-1.2.7]# ps aux | grep keepalived
root 1117 0.0 0.0 42172 1000 ? Ss 14:37 0:00 keepalived -D
root 1119 0.0 0.2 44400 2296 ? S 14:37 0:00 keepalived -D
root 1120 0.0 0.1 44276 1664 ? S 14:37 0:00 keepalived -D
root 1165 0.0 0.0 103268 884 pts/5 S+ 14:48 0:00 grep keepalived
查看虛擬IP是否加上:
[root@localhost keepalived-1.2.7]# ip a
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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b2:88:f7 brd ff:ff:ff:ff:ff:ff
inet 192.168.146.164/24 brd 192.168.146.255 scope global eth1
inet 192.168.146.101/32 scope global eth1
inet6 fe80::20c:29ff:feb2:88f7/64 scope link
valid_lft forever preferred_lft forever
紅色部分顯示已經自動加上虛擬IP了。
至此,LVS_MASTER服務器已經配置好並啓動了。
接下來開始配置部署LVS從服務器:
192.168.146.160 LVS_SALVE:
具體過程同192.168.146.164 LVS_MASTER!!
接下里配置部署兩臺web服務器:
配置部署192.168.146.162 web1:
配置虛擬IP啓動腳本:
[root@localhost ~]# cat /etc/init.d/realserver.sh
SNS_VIP=192.168.146.101
. /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
配置部署tomcat:
[root@localhost ~]# yum -y install tomcat6 tomcat6-webapps tomcat6-admin-webapps
[root@localhost ~]# chkconfig tomcat6 on
[root@localhost ~]# service tomcat6 start
Starting tomcat6: [ OK ]
爲了測試方便暫時關閉防火牆:
[root@localhost ~]# service iptables stop
打開瀏覽器測試:http://192.168.146.142:8080
會看到TOMCAT的熟悉頁面了。
爲了測試負載均衡,咱們將這個頁面改下,以更好的標識這個網頁是本服務器的
Tomcat6安裝目錄位於/usr/share/tomcat6,因此咱們要編輯tomcat下的webapps/ROOT/index.html這個文件。
[root@localhost ~]# cd /usr/share/tomcat6/webapps/ROOT/
[root@localhost ROOT]# cat /dev/null > index.html
[root@localhost ROOT]# cat index.html
web1 192.168.146.162
啓動虛擬IP的腳本:
[root@localhost ROOT]# sh /etc/init.d/realserver.sh start
RealServer Start OK
eth1 Link encap:Ethernet HWaddr 00:0C:29:1F:0F:F3
inet addr:192.168.146.162 Bcast:192.168.146.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe1f:ff3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:89543 errors:0 dropped:0 overruns:0 frame:0
TX packets:47914 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:112074876 (106.8 MiB) TX bytes:2870905 (2.7 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:22 errors:0 dropped:0 overruns:0 frame:0
TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1408 (1.3 KiB) TX bytes:1408 (1.3 KiB)
lo:0 Link encap:Local Loopback
inet addr:192.168.146.101 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
運行後會看到網絡有了一個虛擬IP,即紅色標註部分。
去LVS_MASTER服務器的終端查看下ipvsadm,查看已經鏈接上了WEB1服務器(即紅色標註部分):
[root@localhost keepalived-1.2.7]# 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.146.101:8080 rr persistent 50
-> 192.168.146.162:8080 Route 1 0 0
已經能夠看到有服務器加入進來了。
此時咱們訪問網頁http://192.168.146.101:8080,出現界面顯示web1 192.168.146.162;或者使用命令行測試轉發狀況均可以查看。
配置部署192.168.146.163 web2:
具體配置部署過程同web1!!