LinuxSky:让我们一起学习Linux。

LVS基于SOCKET流量调度管理之NAT负载均衡

本文主要介绍了如何使用LVS软件搭建NAT负载均衡及在搭建NAT负载均衡的过程中遇到的一些问题,在此分享出来以供大家参考学习,希望对广大博友有一定的参考意义。其中正文内容也附上了keepalived主备服务器的配置信息,以下内容均已经通过测试功能正常,可放心拷贝使用。
第一步:NAT负载模式网络规划
#############################NAT网络拓扑###########################################################
LVS Director机器主:
主机名:ESBLVS01
公网VIP:36.0.0.70
连接外网IP:36.0.0.71
连接内网IP:192.168.0.2
私有VIP:192.168.0.200(该ip地址作为realServer的高可用网关)
子网掩码:255.255.255.0
网关:36.0.0.254
内网网卡不设网关
--------------------------------------------------------------------
LVS Director机器备:
主机名:ESBLVS02
连接外网IP:36.0.0.72
连接内网IP:192.168.0.3
私有VIP:192.168.0.200(该ip地址作为realServer的高可用网关)
子网掩码:255.255.255.0
网关:36.0.0.254
内网网卡不设网关
--------------------------------------------------------------------
RealServer01机器:
主机名:APP1
私网RIP1地址:192.168.0.3 
子网掩码:255.255.255.0   
网关设置为调度服务器上的私有VIP:192.168.0.200 
RealServer02机器:
主机名:APP3
私网RIP1地址:192.168.0.4 
子网掩码:255.255.255.0   
网关设置为调度服务器上的私有VIP:192.168.0.200 
RealServer03机器:
主机名:APP3
私网RIP1地址:192.168.0.5 
子网掩码:255.255.255.0   
网关设置为调度服务器上的私有VIP:192.168.0.200 
########################################################################################
第二步:调整LB调度服务器系统参数
1、开启路由转发功能
#echo 1 > /proc/sys/net/ipv4/ip_forward 
2、确保selinux服务和防火墙iptables已经关闭
#getenforce 为disable
#service iptables status 为not running状态
3、手动配置测试lvs调度策略
#!/bin/bash
#It was created by fengzhanhai at 20150325
#The scripts used by manaul control nat loading blancer
VIP=36.0.0.70
RIP1=192.168.0.3
RIP2=192.168.0.4
RIP3=192.168.0.5
DVIP=192.168.0.200
case "$1" in
start)           
/sbin/ifconfig eth1:0 $VIP netmask 255.255.255.0 up
/sbin/ifconfig eth2:0 $DVIP netmask 255.255.255.0 up
#refresh dynamic routing
/sbin/arping -I eth1 -c 3 -s ${VIP} 36.0.0.254 > /dev/null 2>&1
#The Director must be able to forward packets
  echo 1 > /proc/sys/net/ipv4/ip_forward
# Clear ipvsadm rules/services.
  /sbin/ipvsadm -C
# In this recipe, we will use the round-robin scheduling method. 
  /sbin/ipvsadm -A -t $VIP:9051 -s rr
# the real server IP (RIP) inside the cluster
  /sbin/ipvsadm -a -t $VIP:9051 -r $RIP1 -m
  /sbin/ipvsadm -a -t $VIP:9051 -r $RIP2 -m
  /sbin/ipvsadm -a -t $VIP:9051 -r $RIP3 -m
  /bin/touch /var/lock/subsys/ipvsadm.lock
;;
stop)
# Reset ipvsadm
  /sbin/ipvsadm -C
# Bring down the VIP interface
  ifconfig eth1:0 down
  ifconfig eth2:0 down
  rm -rf /var/lock/subsys/ipvsadm.lock
;;
status)
  [ -e /var/lock/subsys/ipvsadm.lock ] && echo "ipvs is running..." || echo "ipvsadm is stopped..."
;;
*)
  echo "Usage: $0 {start|stop}"
;;
esac
4、使用keepalived管理LVS的NAT集群配置如下:

点击(此处)折叠或打开

  1. ! Configuration File for keepalived


  2. global_defs {
  3. router_id LVS_ESB
  4. }


  5. vrrp_instance VI_1 {
  6.    state MASTER
  7.    interface eth1
  8.    lvs_sync_daemon_interface eth1
  9.    virtual_router_id 125
  10.    priority 100
  11.    advert_int 1
  12.    authentication {
  13.        auth_type PASS
  14.        auth_pass inzone
  15.    }
  16.    virtual_ipaddress {
  17.       36.0.0.70
  18.    }
  19. }
  20. vrrp_instance VI_2 {
  21.    state MASTER
  22.    interface eth2
  23.    lvs_sync_daemon_interface eth2
  24.    virtual_router_id 152
  25.    priority 100
  26.    advert_int 1
  27.    authentication {
  28.        auth_type PASS
  29.        auth_pass inzone
  30.    }
  31.    virtual_ipaddress {
  32.       192.168.101.100
  33.    }
  34. }
  35. virtual_server 36.0.0.70 9051{
  36.    delay_loop 60
  37.    lb_algo rr
  38.    lb_kind NAT
  39.    alpha
  40.    protocol TCP
  41.    real_server 192.168.101.3 9051{
  42.        weight 1
  43.        inhibit_on_failure
  44.        MISC_CHECK {
  45.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh SocketRequesterExec 192.168.101.3"
  46.        misc_timeout 30
  47.        misc_dynamic
  48.        }
  49.    }


  50.    real_server 192.168.101.4 9051{
  51.        weight 1
  52.        inhibit_on_failure
  53.        MISC_CHECK {
  54.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh SocketRequesterExec 192.168.101.4"
  55.        misc_timeout 30
  56.        misc_dynamic
  57.        }
  58.    }


  59.    real_server 192.168.101.5 9051{
  60.        weight 1
  61.        inhibit_on_failure
  62.        MISC_CHECK {
  63.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh SocketRequesterExec 192.168.101.5"
  64.        misc_timeout 30
  65.        misc_dynamic
  66.        }
  67.    }


  68. }




  69. virtual_server 36.0.0.70 40000{
  70.    delay_loop 60
  71.    lb_algo sh
  72.    lb_kind NAT
  73.    alpha
  74.    protocol TCP


  75.    real_server 192.168.101.3 40000{
  76.        weight 1
  77.        inhibit_on_failure
  78.        MISC_CHECK {
  79.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh MQRequesterExec 192.168.101.3"
  80.        misc_timeout 30
  81.        misc_dynamic
  82.        }
  83.    }


  84.    real_server 192.168.101.4 40000{
  85.        weight 1
  86.        inhibit_on_failure
  87.        MISC_CHECK {
  88.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh MQRequesterExec 192.168.101.4"
  89.        misc_timeout 30
  90.        misc_dynamic
  91.        }
  92.    }


  93.    real_server 192.168.101.5 40000{
  94.        weight 1
  95.        inhibit_on_failure
  96.        MISC_CHECK {
  97.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh MQRequesterExec 192.168.101.5"
  98.        misc_timeout 30
  99.        misc_dynamic
  100.        }
  101.    }


  102. }


  103. virtual_server 36.0.0.70 40011{
  104.    delay_loop 60
  105.    lb_algo sh
  106.    lb_kind NAT
  107.    alpha
  108.    protocol TCP


  109.    real_server 192.168.101.3 40011{
  110.        weight 1
  111.        inhibit_on_failure
  112.        MISC_CHECK {
  113.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh MQRequesterExec 192.168.101.3 40011"
  114.        misc_timeout 30
  115.        misc_dynamic
  116.        }
  117.    }


  118.    real_server 192.168.101.4 40011{
  119.        weight 1
  120.        inhibit_on_failure
  121.        MISC_CHECK {
  122.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh MQRequesterExec 192.168.101.4 40011"
  123.        misc_timeout 30
  124.        misc_dynamic
  125.        }
  126.    }


  127.    real_server 192.168.101.5 40011{
  128.        weight 1
  129.        inhibit_on_failure
  130.        MISC_CHECK {
  131.        misc_path "./usr/local/bin/esbrequester/misc_chk.sh MQRequesterExec 192.168.101.5 40011"
  132.        misc_timeout 30
  133.        misc_dynamic
  134.        }
  135.    }


  136. }


  137. virtual_server 36.0.0.70 9018{
  138.    delay_loop 60
  139.    lb_algo rr
  140.    lb_kind NAT
  141.    alpha
  142.    protocol TCP


  143.    real_server 192.168.101.3 9018{
  144.        weight 1
  145.    }


  146.    real_server 192.168.101.4 9018{
  147.        weight 1
  148.    }


  149.    real_server 192.168.101.5 9018{
  150.        weight 1
  151.    }


  152. }
#####################################################################################
5、把以上配置拷贝到LVS备调度服务器上并修改MASTER关键字为BACKUP并调小优先级即可
6、启动keepalived:service keepalived start
7、检查地址是否已经接管:ip addr
8、检查后端服务节点是否已经被加入ipvsadm -Ln
9、访问VIP验证业务
至此LVS的NAT模式搭建完成,如果遇到NAT模式下对外VIP无法访问,在调度服务器可以访问的问题,可能是没有开启icmp请检查 /proc/sys/net/ipv4/ip_forward文件内的值是否为1
    标签:lvs NAT 负载均衡
    分类: 网络服务 | 发布:admin | 查看: | 发表时间:2015/04/02
    分享按钮

    相关文章

    文章评论

    站内资料部分由本人收集整理,所有文章版权均系原作者和出版者所有,如涉版权问题,恳请来信告知!