四台centos5.5
VIP 192.168.0.82lvs 192.168.0.81 192.168.0.74web 192.168.0.72 192.168.0.73LVS服务器(DR模式,所有IP都在内网)ln -s /usr/src/kernels/2.6.18-53.e15-i686/ /usr/src/linux#如果/usr/src/kernels/目录空先安装kernel-devel #yum install kernel-devel yum install ipvsadmipvsadmlsmod |grep ip_vsecho "1" > /proc/sys/net/ipv4/ip_forward#/bin/bashVIP=192.168.0.82WEB1=192.168.0.72WEB2=192.168.0.73case "$1" instart)/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up/sbin/route add -host $VIP dev eth0:0/sbin/ipvsadm -C/sbin/ipvsadm -A -t $VIP:80 -s rr/sbin/ipvsadm -a -t $VIP:80 -r $WEB1 -g/sbin/ipvsadm -a -t $VIP:80 -r $WEB2 -gtouch /var/lock/subsys/ipvsadm >/dev/null 2>&1echo "lvs status-------------------[OK]";;stop)/sbin/ipvsadm -C/sbin/ifconfig eth0:0 downroute del $VIPrm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1echo "lvs stop";;status)if [ ! -e /var/lock/subsys/ipvsadm ];then echo "lvs stop" exit 1else echo "lvs status-------------------[OK]"fi;;*)echo "Usage: $0 {start|stop|status}"exit 1;;esacWEB服务器(两台一致)#!/bin/bash#description:start realserver#chkconfig VIP=192.168.0.82/etc/rc.d/init.d/functionscase "$1" instart)echo " start LVS of REALServer"/sbin/ifconfig lo:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up/sbin/route add -host $VIP1 dev lo:0echo "1" >/proc/sys/net/ipv4/ip_forwardecho "1" >/proc/sys/net/ipv4/conf/eth0/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/eth0/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce;;stop)/sbin/ifconfig lo:0 downecho "close LVS Directorserver"echo "0" >/proc/sys/net/ipv4/ip_forwardecho "0" >/proc/sys/net/ipv4/conf/eth0/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/eth0/arp_announceecho "0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/all/arp_announce;;*)echo "Usage: $0 {start|stop}"exit 1esac编译 keepalived-1.1.17(版本不可过高)yum install libnl-devel./configuremakemake installcp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ #设置为service方式启动cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/mkdir /etc/keepalivedcp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/cp /usr/local/sbin/keepalived /usr/sbin//etc/init.d/keepalived start #启动keepalivedvi /etc/rc.local #添加为开机自启动/usr/local/sbin/keepalived -D -f /etc/keepalived/keepalived.conf# sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#' /etc/sysctl.conf #开启内核转发功能# sysctl -p #查看是否开启内核转发mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak #备份原始配置文件vi /etc/keepalived/keepalived.conf========================================================!Configuration File for keepalivedglobal_defs { #notification_email { # quanzhou.li@pearlinpalm.com #填写自己的邮箱出现故障接收报警邮件用 #} #notification_email_from Alexandre.Cassen@firewall.loc #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_01 #备份服务器改为LVS_02}vrrp_instance VI_1 { state MASTER #备份服务器上MASTER为BACKUP interface eth0 #lvs_sync_daemon_inteface eth1 virtual_router_id 51 priority 100 #备份服务上优先级要低于100,如改为90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.82 #这里填写VIP地址,也可添加多个VIP }}virtual_server 192.168.0.82 80 { #这里填写VIP地址 delay_loop 6 lb_algo rr lb_kind DR #nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.0.72 80 { #这里是真实服务器的IP weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.0.73 80 { #这里是第二台真实服务器的IP weight 2 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }}========================================================keepalived.conf 文件详解安装完成之后,生成的配置文件放置于:/usr/local/keepalived/etc/keepalived/keepalived.conf官方提供了不少模板性的配置文件:/usr/local/keepalived/etc/keepalived/samples建议将配置文件放置于: /etc/keepalived/keepalived.conf如果运用不指定配置文件,他可以直接调用/etc/keepalived/keepalived.conf中的配置文件接下来,详细说明配置文件中的各项含义:#全局定义块,以下模块不可省略,必须存在。global_defs { notification_email { email #如有故障,发邮件报警的地址,一般不采用,可以随意填写}notification_email_from emailsmtp_server hostsmtp_connect_timeout num #邮件服务链接超时的最长时间lvs_id string #Lvs负载均衡器标识,在一个网络里面,请保持他是唯一性。}#VRRP实例定义块vrrp_sync_group string { #确定失败切换(FailOver)包含的路由实例个数。即在有2个负载均衡器的场景,一旦某个负载均衡器失效group { string #备用的负载均衡的服务器名string}vrrp_instance string { #前面定义的后备的负载均衡的服务器名state MASTER|BACKUP #只有MASTER和BACKUP两种状态,都必须保持大写。interface string #进行通信的端口,如eth0,eth1mcast_src_ip @IP #真实的IP地址lvs_sync_daemon_interface string #负载均衡器之间的监控接口。如果采用DR模式,可以保持和通信端口一致。virtual_router_id num #这个标识是同一个vrrp实例使用唯一的标识。即同一个vrrp_stance,MASTER和BACKUP的virtual_router_id是一致的,同时在整个vrrp内是唯一的。priority num #权重,数值越大,权重越大,Master大于Slave。advert_int num #Master和Slave负载均衡器之间同步检查的时间间隔,单位:秒smtp_alertauthentication { #Master和Slave之间认证的方式auth_type PASS|AHauth_pass string #认证的秘密}virtual_ipaddress { # Block limited to 20 IP addressesIPIPIP}virtual_ipaddress_excluded { # Unlimited IP addresses numberIPIPIP}#虚拟服务器定义块virtual_server (@IP PORT)|(fwmark num) { #上面定义的virtual_ipaddress,需要添加端口delay_loop num #服务健康检查周期,单位:秒lb_algo rr|wrr|lc|wlc|sh|dh|lblc #负载均衡的调度算法方式,一般使用rr或者wlc。lb_kind NAT|DR|TUN #负载均衡转发规则,一般采用DR(nat_mask @IP) #地址掩码,可不填persistence_timeout num #会话保持时间,单位:秒。如果是动态服务,建议开启。persistence_granularity @IPvirtualhost stringprotocol TCP|UDP #通信协议,有sorry_server @IP PORTreal_server @IP PORT { #真实IP地址weight num #权重值,数值越大,权重越高,分发的可能越大。TCP_CHECK { connect_port num #检查端口connect_timeout num #检查超时时间}}real_server @IP PORT { weight numMISC_CHECK { misc_path /path_to_script/script.sh(or misc_path “/path_to_script/script.sh ”)}}real_server @IP PORT { weight numHTTP_GET|SSL_GET { url { # You can add multiple url blockpath alphanumdigest alphanum}connect_port numconnect_timeout numnb_get_retry numdelay_before_retry num}}}keepalived 实例Master 服务器#guration File for keepalived (Master Server)#writed by eric.w.t 2011/04/12#################################### global define###################################global_defs { notification_email { mesopodamia@gmail.com}notification_email_from sns-lvs@gmail.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}##################################### vrrp define####################################vrrp_sync_group VGM { group { VI_1}}vrrp_instance VI_1 { state MASTERinterface eth0virtual_router_id 110priority 100advert_int 1virtual_ipaddress { 10.249.0.208}}###################################### virtual machine setting#####################################virtual_server 10.249.0.208 80 { delay_loop 6lb_algo rrlb_kind NATnat_mask 255.255.255.0protocol TCPpersistence_timeout 20real_server 10.249.0.254 80 { weight 10TCP_CHECK { connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}}Slave 服务器配置#guration File for keepalived (Slave Server)#writed by eric.w.t 2011/04/12#################################### global define###################################global_defs { notification_email { mesopodamia@gmail.com}notification_email_from sns-lvs@gmail.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}##################################### vrrp define####################################vrrp_sync_group VGM { group { VI_1}}vrrp_instance VI_1 { state SLAVEinterface eth0virtual_router_id 110priority 99advert_int 1virtual_ipaddress { 10.249.0.208}}###################################### virtual machine setting#####################################virtual_server 10.249.0.208 80 { delay_loop 6lb_algo rrlb_kind NATnat_mask 255.255.255.0protocol TCPpersistence_timeout 20real_server 10.249.0.254 80 { weight 10TCP_CHECK { connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}}参考文档 :http://www.keepalived.org/documentation.html#调用检测脚本vrrp_script check_run { script "/root/keepalived_check_mysql.sh" interval 5}