设为首页 收藏本站
查看: 1226|回复: 0

[经验分享] LVS+Keepalived 实现集群负载均衡

[复制链接]

尚未签到

发表于 2017-4-18 06:31:27 | 显示全部楼层 |阅读模式
DSC0000.png

IP配置信息:
LVS-DR-Master          192.168.2.166
LVS-DR-BACKUP          192.168.2.167
LVS-DR-VIP             192.168.2.170        
WEB1-Realserver        192.168.2.171
WEB2-Realserver        192.168.2.172
GateWay                192.168.2.253

一.keepalived安装
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance MASTER主机和BACKUP主机之间failover的实现。

参考CentOS 6.3 安装 Keepalived
http://maosheng.iteye.com/blog/2238747

参考Keepalived+Nginx 实现双机热备
http://maosheng.iteye.com/blog/2242594


二.配置LVS实现负载均衡

LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统

1.LVS-DR,配置LVS脚本实现负载均衡

vi /usr/local/sbin/lvs-dr.sh

#!/bin/bash
# description: start LVS of DirectorServer
#

GW=192.168.2.253

# website director vip.
WEB_VIP=192.168.2.170
WEB_RIP1=192.168.2.171
WEB_RIP2=192.168.2.172

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

logger $0 called with $1

case "$1" in

start)
       # Clear all iptables rules.
       /sbin/iptables -F
       # Reset iptables counters.
       /sbin/iptables -Z
       # Clear all ipvsadm rules/services.
       /sbin/ipvsadm -C

#set lvs vip for dr
       /sbin/ipvsadm --set 30 5 60
       /sbin/ifconfig eth0:0 $WEB_VIP broadcast $WEB_VIP netmask 255.255.255.255 up
       /sbin/route add -host $WEB_VIP dev eth0:0
       /sbin/ipvsadm -A -t $WEB_VIP:80 -s wrr -p 3
       /sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP1:80 -g -w 1
       /sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP2:80 -g -w 1
       touch /var/lock/subsys/ipvsadm >/dev/null 2>&1

       # set Arp
       /sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW >/dev/null 2>&1
      ;;
stop)
       /sbin/ipvsadm -C
       /sbin/ipvsadm -Z
       ifconfig eth0:0 down
       route del $WEB_VIP  >/dev/null 2>&1
       rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
       /sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW
       echo "ipvsadm stoped"
      ;;

status)

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

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

exit 0


2.配置Realserver脚本.

#!/bin/bash
#  
# description: Config realserver lo and apply noarp

WEB_VIP=192.168.2.170

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

case "$1" in
start)
      ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP
      /sbin/route add -host $WEB_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 $WEB_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"
      ;;
status)
       # Status of LVS-DR real server.
       islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP`
       isrothere=`netstat -rn | grep "lo:0" | grep $web_VIP`
       if [ ! "$islothere" -o ! "isrothere" ];then
           # Either the route or the lo:0 device
           # not found.
           echo "LVS-DR real server Stopped."
       else
           echo "LVS-DR Running."
       fi
;;
*)
       # Invalid entry.
       echo "$0: Usage: $0 {start|status|stop}"
       exit 1
;;
esac
exit 0

附上realserver机上的/etc/sysctl.conf:

# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
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
或者采用secondary ip address方式配置
# vi /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
#ip addr add 61.164.122.8/32 dev lo
#ip add list 查看是否绑定

3. 启动lvs-dr脚本和realserver启本,在DR上可以查看LVS当前状态:

#watch ipvsadm –ln

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

1.配置在主负载均衡服务器上配置keepalived.conf

#vi /etc/keepalived/keepalived.conf (主调度器)
! Configuration File for keepalived
global_defs {
  notification_email {

acassen@firewall.loc

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
}
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 {
       192.168.2.170
   }
}
virtual_server 192.168.2.170 80 {
   delay_loop 6
   lb_algo wrr
   lb_kind DR
   persistence_timeout 60
   protocol TCP

   real_server 192.168.2.171 80 {
       weight 3            
       TCP_CHECK {
       connect_timeout 10   
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
}
   }
   real_server 192.168.2.172 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
}

在备用调度器上:

#vi /etc/keepalived/keepalived.conf (备用调度器)
! Configuration File for keepalived
global_defs {
  notification_email {

acassen@firewall.loc

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
}
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 {
       192.168.2.170
   }
}
virtual_server 192.168.2.170 80 {
   delay_loop 6
   lb_algo wrr
   lb_kind DR
   persistence_timeout 60
   protocol TCP

   real_server 192.168.2.171 80 {
       weight 3            
       TCP_CHECK {
       connect_timeout 10   
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
}
   }
   real_server 192.168.2.172 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
}

2. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.

3. vi /etc/rc.local
  #/usr/local/sbin/lvs-dr.sh      将lvs-dr.sh这个脚本注释掉。
  #/usr/local/sbin/lvs-dr.sh stop 停止lvs-dr脚本
  #/etc/init.d/keepalived start  启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡和高可用.

4. 查看lvs服务是否正常

#watch ipvsadm –ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort           Forward Weight ActiveConn InActConn
TCP  61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80            Route   3      0          0
-> 61.164.122.9:80             Route   3      0          0
复制代码
#tail –f /var/log/message  监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。

5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-365664-1-1.html 上篇帖子: keepalived编译Use IPVS Framework No 下篇帖子: LVS+Keepalived搭建高可用的负载均衡集群系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表