lanying56123 发表于 2015-12-24 11:00:30

LVS+keepalived简单配置实例

一、基本信息

系统:
CentOS 6.5 (2.6.32-220.el6.x86_64)

软件:
ipvsadm-1.24.tar.gz
keepalived-1.2.2.tar.gz

二、拓扑结构图

虚拟I P地址: 172.16.32.200
DR1_MASTER: 172.16.32.203
DR2_BACKUP: 172.16.32.204
Web1: 172.16.32.205
Web2: 172.16.32.206


三、安装配置

1.在DR1和DR2上面操作(172.16.32.203/172.16.32.204)

查看是否支持IPVS
#lsmod |grep ip_vs    或#modprobe -l |grep ipvs
创建软连接(不创建链接会出现:libipvs.h:14:23: net/ip_vs.h: No such file or directory等编译错误。)
#ln -s /usr/src/kernels/2.6.32-220.el6.x86_64/ /usr/src/linux

2.DR服务器安装ipvsadm及keepalived,软件存放在/data/software/目录中

#cd /data/software
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24/
#make && make install
#cd ..

#tar zxvfkeepalived-1.2.2.tar.gz
#cd keepalived-1.2.2/
#./configure
#make && make install

#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/

3.编写LVS脚本实现负载均衡

#vim /usr/local/sbin/lvs-dr.sh   内容如下:


#!/bin/bash
# description: start LVS of DirectorServer
#Written by :NetSeek http://www.linuxtone.org


GW=172.16.32.1
# website director vip.
SNS_VIP=172.16.32.200
SNS_RIP1=172.16.32.205
SNS_RIP2=172.16.32.206


. /etc/rc.d/init.d/functions

logger $0 called with $1

case "$1" in

start)
         # set squid vip
         /sbin/ipvsadm --set 30 5 60
         /sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
         /sbin/route add -host $SNS_VIP dev eth0:0
         /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
         /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
         /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
         touch /var/lock/subsys/ipvsadm >/dev/null 2>&1

       ;;
stop)
         /sbin/ipvsadm -C
         /sbin/ipvsadm -Z
         ifconfig eth0:0 down
         ifconfig eth0:1 down
         route del $SNS_VIP
         route del $SS_VIP
         rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
         echo "ipvsadm stoped"
         ;;

status)

         if [ ! -e /var/lock/subsys/ipvsadm ];then
                echo "ipvsadm stoped"
                exit 1
         else
                echo "ipvsadm OK"
         fi
         ;;

*)
          echo "Usage: $0 {start|stop|status}"
         exit 1
esac

exit 0

4.利用Keepalvied实现负载均衡和和高可用性

#vim /etc/keepalived/keepalived.conf         #配置keepalived.conf文件具体内容如下:


! Configuration File for keepalived

global_defs {
   notification_email {
#    cnseek@gmail.com
      lixuan@miaodai.cn
   wanghaichao@miaodai.cn
#   failover@firewall.loc
#   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
# smtp_connect_timeout 30
   router_id LVS_DEVEL
}

# 20150110 written by :netseek
# VIP1
vrrp_instance VI_1 {
    state MASTER       #DR2上面将此处的 MASTER 改为 BACKUP
    interface eth0
    virtual_router_id 51
    priority 100         #DR2上面将此处的 100 改为 90
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      172.16.32.200   
    }
}

virtual_server 172.16.32.200 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP

    real_server 172.16.32.205 80 {
      weight 3
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 80
      }
    }
    real_server 172.16.32.206 80 {
      weight 3
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 80
      }
   }
}

5.在Web1和Web2上面编写realserver.sh脚本

#vim /usr/local/sbin/realserver.sh#内容如下:

#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org


SNS_VIP=172.16.32.200

. /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
6.在Web1和Web2服务器/etc/sysctl.conf 配置文件末尾添加如下内容:

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

#sysctl -p    #示在/proc/sys目录中指定的内核加载参数

7.启动lvs-dr.sh 和 realserver.sh 脚本

在DR1和DR2上面启动 lvs-dr.sh 脚本

#cd /usr/local/sbin/
#sh lvs-dr.sh start
#watch ipvsadm -ln    #查看LVS状态如下:
Every 2.0s: ipvsadm -ln                                                                                       Thu Mar 12 19:04:41 2015
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP172.16.32.200:80 wrr persistent 60
-> 172.16.32.206:80             Route   30          0
-> 172.16.32.205:80             Route   30          0
在Web1和Web2上面启动realserver.sh脚本

#cd /usr/local/sbin/
#sh realserver.sh start
#ip a


1: lo:mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet 172.16.32.200/32 brd 172.16.32.200 scope global lo:0
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:88:b6:73 brd ff:ff:ff:ff:ff:ff
    inet 172.16.32.205/24 brd 172.16.32.255 scope global eth0
    inet6 fe80::20c:29ff:fe88:b673/64 scope link
       valid_lft forever preferred_lft forever

#tailf /var/log/message    #监听日志,查看服务运行状态

8.Web1和Web2安装httpd 服务页面更改,以方便测试。

#yum install httpd -y
#service httpd start

四、测试

1.打开浏览器输入虚拟IP 测试   http://172.16.32.200
2.停止DR1的keepalived服务或者网卡查看DR2是否能正常接管
3.停掉Web1或者Web2任意一台服务器的web服务,测试访问页面是否正常
页: [1]
查看完整版本: LVS+keepalived简单配置实例