CentOS5.5 + LVS(Master+Slave)+ 2 WEB, Web提前配置好页面,以便区分是分配到哪台真机上, 实验前防火墙、selinux关闭。 1. 下载相关软件包 #wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz #wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz 2. 安装LVS和Keepalived #lsmod |grep ip_vs #uname -r 2.6.18-274.17.1.el5xen #tar zxvf ipvsadm-1.24.tar.gz #cd ipvsadm-1.24 #make && make install #tar zxvf keepalived-1.1.15.tar.gz #cd keepalived-1.1.15 #./configure –prefix=/usr/local/keepalived–with-kernel-dir=/usr/src/linux/ #make && make install #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/ #cp /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/ #mkdir /etc/keepalived #cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/ #cp /usr/local/keepalived/sbin/keepalived/usr/sbin/ # /etc/init.d/keepalived start Startingkeepalived: [ OK ] 3.配置keepalived.conf,如下: Master机器配置: #VIP1 vrrp_instance VI_1 { stateBACKUP # lvs_sync_daemon_interface eth1 nopreempt interfaceeth0 virtual_router_id 62 priority100 # 备份服务上将100改为99 advert_int3 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.228/16 brd 192.168.255.255 deveth0 label eth0:0 ### bt web 的vip } } virtual_server 192.168.2.228 80 { delay_loop6 #(每隔10秒查询realserver状态) lb_algowrr #(lvs 算法) lb_kindDR #(Direct Route) persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver) inhibit_on_failure protocolTCP #(用TCP协议检查realserver状态) real_server192.168.2.184 80 { weight10 notify_down “/root/bin/notify_mail.sh btweb01192.168.2.184″ TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server192.168.2.199 80 { weight 10 notify_down ”/root/bin/notify_mail.sh btweb02 192.168.2.199″ TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } Slave机器配置: # VIP1 vrrp_instance VI_1 { stateBACKUP #备份服务器上将MASTER改为BACKUP # lvs_sync_daemon_interface eth1 nopreempt interfaceeth0 virtual_router_id62 priority100 # 备份服务上将100改为99 advert_int3 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.228/16 brd 192.168.255.255 dev eth0 label eth0:0 ### btweb 的vip } } virtual_server 192.168.2.228 80 { delay_loop6 #(每隔10秒查询realserver状态) lb_algowrr #(lvs 算法) lb_kindDR #(Direct Route) persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver) inhibit_on_failure protocolTCP #(用TCP协议检查realserver状态) real_server192.168.2.184 80 { weight10 notify_down ”/root/bin/notify_mail.sh btweb01 192.168.2.184″ TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server192.168.2.199 80 { weight10 notify_down ”/root/bin/notify_mail.sh btweb02 192.168.2.199″ TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } 5.real机器配置 [root@vm02 ~]# more lvs_real.sh #!/bin/bash SNS_VIP=192.168.2.228 ./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 exit0 6.测试 [root@test1 ~]# 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.2.228:80 wrr persistent 60 -> 192.168.2.184:80 Route 10 0 0 -> 192.168.2.199:80 Route 10 0 5 禁用realserver机器: ipvsadm -d -t 192.168.2.228:80 -r 192.168.2.199:80 启用realserver机器: ipvsadm -a -t 192.168.2.228:80 -r 192.168.2.199:80 1.关掉任何一台web或nginx,网页能正常访问。 2.关闭任何一台lvs,网页能正常访问。 证明实验成功。 (责任编辑:IT) |