lvs+keepalived配置高可用的负载均衡

发布时间:2020-07-13编辑:脚本学堂
本文为大家介绍lvs+keepalived配置高可用的负载均衡,供大家学习参考。文章写的不错,很有实用价值。

昨天在chinaunix的论坛上看到一提问:为什么ipvsadm -L看不到后端的

本文为大家介绍lvs+keepalived配置高可用的负载均衡,供大家学习参考。文章写的不错,很有实用价值。

昨天在chinaunix的论坛上看到一提问:为什么ipvsadm -L看不到后端的realsrver,我想应该是他的realserver上没有启动脚本吧!也或许是他realserver上的80端口没有开启,这里我也写下我的环境的lvs+keepalived架构的实现!目的就是用虚拟IP转发80端口和1099端口请求到后端的真实服务器
四台机器环境:
IP地址            lvs版本      keepalived版本     系统版本            用途
192.168.15.67      1.2.4        1.1.15            redhat4.8(64位)     MASTER
192.168.15.91      1.2.4        1.1.15            redhat4.8(64位)     BACKUP
192.168.15.93      无需安装     无需安装          redhat4.8(64位)     Realserver
192.168.15.94      无需安装     无需安装          redhat4.8(64位)     Realserver
192.168.15.120                                                        VIP

拓扑图:
lvs
1、lvs软件ipvsadm下载安装(主机和备机上面都必须安装)
 

复制代码 代码如下:
wget  http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
tar zxvf ipvsadm-1.24.tar.gz
ln -s /usr/src/kernels/2.6.9-89.EL-smp-x86_64 /usr/src/linux //注释:这一步针对自己的系统做,否则一会安装ipvsadm会报错。
cd ipvsadm-1.24
make
make install

2、keepalived下载安装(主机和备机上面都必须安装)
 

复制代码 代码如下:
wget  http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15

//注释:如果系统rhel版本低于5.0以下安装报错,这里要修改系统配置
//vi /usr/src/linux/include/linux/types.h 将下面2行红色字体用/* */注释掉
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;

*/
./configure
make
make install

3、将keepalived做成服务启动(主机和备机上面都必须安装)
 

复制代码 代码如下:
cp /usr/local/etc/rc.d/init.d/keepalived /etc/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     注释:测试服务启动与关闭
service keepalived stop

4、主机上配置keeplived(配置前备份好keepalived.conf文件然后再清空即可)
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
echo “” > /etc/keepalived/keepalived.conf

增加如下内容:
 

复制代码 代码如下:

! Configuration File for keepalived
global_defs {
   notification_email {
       zengzhunzhun@ninetowns.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
   state MASTER          状态是MASTER
   interface eth0        监听网卡切换
   virtual_router_id 51
   priority 100          优先级100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }

virtual_ipaddress {
      192.168.15.120   虚拟IP地址,即VIP
   }
}

virtual_server 192.168.15.120 80{
   delay_loop 6
   lb_algo wlc
   lb_kind DR    DR模式

   persistence_timeout 1
   protocol TCP

   real_server 192.168.15.94 80{
       weight 1         权重,权重越高处理的请求越多
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }

   real_server 192.168.15.93 80{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }
}

virtual_server 192.168.15.120 1099{
   delay_loop 6
   lb_algo wlc
   lb_kind DR
   persistence_timeout 1
   protocol TCP
   real_server 192.168.15.94 1099{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 1099
       }
   }

   real_server 192.168.15.93 1099{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 1099
       }
   }
}

5、备机上配置keeplived(配置前备份好keepalived.conf文件然后再清空即可)
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
echo “” > /etc/keepalived/keepalived.conf
增加如下内容:(只有两处和主机上的不一样,下面已用红色字体标出)
 

复制代码 代码如下:

! Configuration File for keepalived
global_defs {
   notification_email {
       zengzhunzhun@ninetowns.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
   state BACKUP           注意状态是BACKUP
   interface eth0
   virtual_router_id 51
   priority 99            优先级是99,比主机低
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }

virtual_ipaddress {
      192.168.15.120
   }
}

virtual_server 192.168.15.120 80{
   delay_loop 6
   lb_algo wlc
   lb_kind DR
   persistence_timeout 1
   protocol TCP

   real_server 192.168.15.94 80{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }

   real_server 192.168.15.93 80{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }
}

virtual_server 192.168.15.120 1099{
   delay_loop 6
   lb_algo wlc
   lb_kind DR
   persistence_timeout 1
   protocol TCP

   real_server 192.168.15.94 1099{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 1099
       }
   }

   real_server 192.168.15.93 1099{
       weight 1
       TCP_CHECK {
       connect_timeout 1
       nb_get_retry 3
       delay_before_retry 3
       connect_port 1099
       }
   }
}

6、两台realserver上启动脚本,脚本内容如下:
 

复制代码 代码如下:
#!/bin/bash
VIP=192.168.15.120
. /etc/rc.d/init.d/functions
case "$1" in
start)
        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
        ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev lo:0
        sysctl -p > /dev/null 2>&1
        echo "realserver start OK"
        ;;
stop)
        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
        ifconfig lo:0 down
        /sbin/route del $VIP > /dev/null 2>&1        
        echo "realserver stoped"
        ;;
*)
        echo "Usage:$0 {start|stop}"
        exit 1
esac
exit 0

7、测试
1)启动主机和备机的keepalived服务,ip a确认是否有vip地址?

2)输入ipvsadm -Ln查看是否有如下内容输出?
ipvsadm -Ln
IP Virtual Server version 1.2.0 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  124.42.15.120:80 wlc persistent 1
  -> 192.168.15.94:80             Route   1      122        4       
  -> 192.168.15.93:80             Route   1      124        6       
TCP  124.42.15.120:1099 wlc persistent 1
  -> 192.168.15.94:1099           Route   1      0          0       
  -> 192.168.15.93:1099           Route   1      0          0   

3)停止主机的keepalived服务,备机上确认是否接管vip地址?

4)备机上输入ipvsadm -Ln查看ActiveConn和InActConn对应的值不为0?主机上反而为0了。

5)启动主机的keepalived服务,确认主机再次接管vip地址继续服务?
这个请大家在配置时自行测试吧。有问题可以进脚本学堂QQ群:161228069 进行交流。