一、说明
LVS 是Linux Virtual Server,主要用来实现后端服务的负载均衡,用源码包ipvsadm构建,本文使用基于tunneling模式的转发机制,调度算法使用 weighted Least-connected(wlc),即每个新的连接被分配到负担最小的服务器。
Heartbeat 是一种心跳检测机制,主要用来实现双机互备份,它的接管工作是通过ARP欺骗的手段来完成的。
Memcached memcached不是memcache,它是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。在本文主要用来实现session共享。
NFS 在本文用来实现web源程序目录挂载到web服务器的本地目录
1.拓扑图
2.服务器主机名与IP分配
VIP:192.168.1.10
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
二、安装与配置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
3、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
4、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
五、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
六、挂载 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”
七、监控页的设置
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
八、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;
转载于:https://blog.51cto.com/architect/1359823