负载均衡 LVS+Keepalived

系统  CentOS 5.6 X64

物理机 IP 10.10.10.104

Xen : 三台 CentOS 5.8

ip为: 10.10.10.106  
     10.10.10.107
     10.10.10.108
 
-----------------------------------
IP 分配 :  
LVS-vip  10.10.10.110
LVS-MASTER 10.10.10.104           是LVS 也是 WEB
LVS-BACKUP 10.10.10.106           是LVS 也是 WEB
WEB1-REALSERVER 10.10.10.107
WEB2-REALSERVER 10.10.10.108
WEB3-REALSERVER 10.10.10.106       是LVS 也是 WEB
WEB4-REALSERVER 10.10.10.104       既是LVS 也是 WEB
------------------------------------
 
安装开始:
 
首先在两台 LVS 上安装 LVS+Keppalived
 
下载
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
 
ln -s /usr/src/kernels/2.6.18-238.el5-x86_64/ /usr/src/linux
【PS: Xen 下面没有 2.6.18-238.el5-x86_64  . 将物理机上面的 复制到 虚拟机上面去,否则无法安装】
 
tar zxvf ipvsadm-1.24.tar.gz
 
cd ipvsadm-1.24
 
make
 
make install
 
安装完毕以后,下面安装 Keepalved
 
下载 wget http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
 
tar zxvf keepalived-1.2.1.tar.gz
 
cd keepalived-1.2.1
 
./configure
 
make
 
make install
 
将keepalived做成启动脚务:
 
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/  
 
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/  
 
mkdir /etc/keepalived  
 
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/  
 
cp /usr/local/sbin/keepalived /usr/sbin/  
 
【service keepalived start 可启动,这里暂时不启动,等修改完配置文件以后再启动】
 
 
Keealived的配置文件修改..
 
vi /etc/keepalived/keepalived.conf
 
主的配置文件为:
------------------------------------------------------------
! Configuration File for keepalived
global_defs {
  notification_email {
        jicki@qq.com
  }
  notification_email_from jicki@qq.com
  smtp_server 127.0.0.1
  router_id LVS_DEVEL
}
vrrp_instance VI_1 {
   state MASTER
   interface eth0
   virtual_router_id 51
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       10.10.10.110
   }
}
virtual_server 10.10.10.110 80 {
   delay_loop 6
   lb_algo wrr
   lb_kind DR
   persistence_timeout 60
   protocol TCP
   real_server 10.10.10.107 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }
   real_server 10.10.10.108 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
   real_server 10.10.10.106 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
   real_server 10.10.10.104 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
}
 
----------------------------------------------------
 
从的配置文件为:
 
----------------------------------------------------
! Configuration File for keepalived
global_defs {
  notification_email {
        jicki@qq.com
  }
  notification_email_from jicki@qq.com
  smtp_server 127.0.0.1
  router_id LVS_DEVEL
}
vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   virtual_router_id 51
   priority 99
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       10.10.10.110
   }
}
virtual_server 10.10.10.110 80 {
   delay_loop 6
   lb_algo wrr
   lb_kind DR
   persistence_timeout 60
   protocol TCP
   real_server 10.10.10.107 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }
   real_server 10.10.10.108 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
   real_server 10.10.10.106 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
   real_server 10.10.10.104 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
}
 
----------------------------------------------------
 
 
修改完配置文件以后启动   servcie keepalived start
 
 
配置WEB服务器....
 
在WEB主机上实行脚本realserver  每一台WEB主机都必须启动这个脚本.
 
----------------------------------------------------------------
#vim /usr/local/sbin/realserver  
#!/bin/bash  
SNS_VIP=10.10.10.110  
. /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
 
----------------------------------------------------------------
 
【FreeBSD 做realserver 的时候 只需要给网卡加 -arp 就可以了
 
ifconfig lo0 alias 10.10.10.110 netmask -arp up     】
 
----------------------------------------------------------------
 
chmod +x /usr/local/sbin/realserver   给权限
 
/usr/local/sbin/realserver start    启动它
 
然后 ifconfig 查看环回接口lo0:0 是否已经绑定VIP IP..
 
 
运行  ipvsadm -l  看看是否已经启动.
 
ipvsadm -l
 
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.10.10.110:http wrr persistent 60
 -> 10.10.10.106:http            Route   3      0          0        
 -> 10.10.10.108:http            Route   3      0          0        
 -> 10.10.10.107:http            Route   3      0          0        
 -> 10.10.10.104:http            Local   3      0          0
 
 
 
查看日志 cat /var/log/messages 文件查看
 
 
 
 
keepalived指定日志文件
 
 
vi /etc/sysconfig/keepalived
 
-------------------------------------------------------------------------
 
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#
 
KEEPALIVED_OPTIONS="-D -d -S 0"
 
-----------------------------------------------------------------------------------

 

设置syslog,修改/etc/syslog.conf

vi /etc/syslog.conf

 

------------------------------------------------------------------------

 

 

# Log all kernel messages to the console.

# Logging much else clutters up the screen.

#kern.*                         /dev/console

 

# Log anything (except mail) of level info or higher.

# Don't log private authentication messages!

*.info;mail.none;authpriv.none;cron.none        /var/log/messages

 

# The authpriv file has restricted access.

authpriv.*                      /var/log/secure

 

# Log all the mail messages in one place.

mail.*                          -/var/log/maillog

 

 

# Log cron stuff

cron.*                          /var/log/cron

 

# Everybody gets emergency messages

*.emerg                         *

 

# Save news errors of level crit and higher in a special file.

uucp,news.crit                      /var/log/spooler

 

# Save boot messages also to boot.log

local7.*                        /var/log/boot.log

 

# keepalived -S 0

local0.*                                                /var/log/keepalived.log

 

------------------------------------------------------------------------

 

重启syslog,设置完成。

posted @ 2016-05-31 18:11  丶小炒肉  阅读(265)  评论(0编辑  收藏  举报