1、說明php
LVS 是Linux Virtual Server,主要用來實現後端服務的負載均衡,用源碼包ipvsadm構建,本文使用基於tunneling模式的轉發機制,調度算法使用 weighted Least-connected(wlc),即每一個新的鏈接被分配到負擔最小的服務器。html
Heartbeat 是一種心跳檢測機制,主要用來實現雙機互備份,它的接管工做是經過ARP欺騙的手段來完成的。mysql
Memcached memcached不是memcache,它是高性能的,分佈式的內存對象緩存系統,用於在動態應用中減小數據庫負載,提高訪問速度。在本文主要用來實現session共享。linux
NFS 在本文用來實現web源程序目錄掛載到web服務器的本地目錄web
1.拓撲圖算法
2.服務器主機名與IP分配sql
VIP:192.168.1.10shell
Virtual Server數據庫
主機名:lvs1後端
Eth0_IP:192.168.1.2/24
Eth1_IP:192.168.10.9/29(heartbeat line)
Virtual Server_backup
主機名:lvs2
Eth0_IP:192.168.1.3/24
Eth1_IP:192.168.10.10/29(heartbeat line)
Real Server web1
主機名:web1
IP:192.168.1.4/24
Real Server web2
主機名:web2
IP:192.168.1.5/24
Memcached and web code
主機名:share
IP:192.168.1.6/24
Mysql server
主機名:data
IP:192.168.1.7/24
2、安裝與配置LVS
1.安裝 ipvsadm
# tar zxvf ipvsadm-1.24.tar
# cd ipvsadm-1.24
# ln –s /usr/src/kernels/2.6.18-53.el5-i686/ /usr/src/linux
# make
# make install
2. Configure shell of tunlvs on Vritual Server and Vritual server_backup
# vi /etc/rc.d/init.d/tunlvs
#!/bin/sh
VIP=192.168.1.10
WEB1=192.168.1.4
WEB2=192.168.1.5
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS directorserver"
/sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev tunl0
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t $VIP:80 -s wlc
/sbin/ipvsadm -a -t $VIP:80 -r $WEB1:80 -i
/sbin/ipvsadm -a -t $VIP:80 -r $WEB2:80 -i
/sbin/ipvsadm
;;
stop)
echo "close LVS directorserver"
/sbin/ipvsadm -C
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
esac
# chmod 755 /etc/rc.d/init.d/tunlvs
三、Configure shell of tunl on Real_web1 and Real_web2
# vi /etc/rc.d/init.d/tunl
#!/bin/sh
VIP=192.168.1.10
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "tunl port starting"
/sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/ifconfig add -host $VIP dev tunl0
echo "1">/proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/tunl0/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
sysctl –p
;;
stop)
echo "tunl port closing"
ifconfig tunl0 down
echo "1">/proc/sys/net/ipv4/ip_forward
echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usege:$0 {start|stop}"
exit 1
esac
四、test lvs
Virtual Server:
# /etc/rc.d/init.d/tunlvs start
Real_web1 and Real_web2:
# /etc/rc.d/init.d/tunl start
Real_web1:
# echo 「this is test for web1」>>/var/www/html/index.html
Real_web2:
# echo 「this is test for web2」>>/var/www/html/index.html
5、NFS與Memcached(Session共享)
1.Nfs
# rpm -ivh /mnt/CentOS/portmap-4.0-65.2.2.1.i386.rpm
# rpm -ivh /mnt/CentOS/nfs-utils-1.0.9-24.el5.i386.rpm
# vi /etc/exports
/webdata 192.168.1.4(rw,sync,no_wdelay) 192.168.1.5(rw,sync,no_wdelay)
# chkconfig –level 35 portmap on
# chkconfig –level 35 nfs on
# /etc/rc.d/init.d/portmap start
# /etc/rc.d/init.d/nfs start
2.Memcached
# rpm –ivh /mnt/Centos/ libevent-1.1a-3.2.1.i386.rpm
# rpm –ivh /mnt/Centos/ libevent-devel-1.1a-3.2.1.i386.rpm
# tar zxvf memcached-1.2.6.tar.tar
# ./configure
# make && make install
# memcached -d -m 512 –l 192.168.1.6 -p 11211 -u root
# killall –HUP memcached
6、掛載 web主目錄與配置php使用session共享
Real_web1 and Real_web2
1.mount
# showmount –e 192.168.1.6
# mount –t nfs 192.168.1.6:/webdata /var/www/html
# vi /etc/fstab
192.168.1.6:/webdata /var/www/html nfs defaults 0 0
2. 配置php使用session共享
# tar zxvf memcache-2.2.3.tgz
# phpize
# ./configure –enable-memcache
# make && make install
# vi /etc/php.ini
[Session]
extension=memcache.so
extension_dir = "/usr/lib/php/modules"
session.save_handler = memcache
session.save_path = 「tcp://192.168.1.6:11211」
7、監控頁的設置
Real_web1 and Real_web2:
這裏是配合ldirectord.cf的ldirectord守護進程
# mkdir /var/www/test
# vi /etc/httpd/conf/httpd.conf
Alias /test/ "/var/www/test/"
<Directory "/var/www/test/">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# echo "Test Page" > /var/www/test/test.html
8、Mysql Server
# /etc/rc.d/init.d/mysqld start
# mysqladmin -u root -p password test
# mysql –root –p
> grant all on *.* to mysql@’192.168.1.0/24’ identified by ‘your_password ’with grant option;
> flush privileges;
> quit;