【LVS 】NAT方式实现过程

LVS-NAT方式实现负载均衡

一、环境介绍

director: RHEL7.0 两块网卡

RS1、RS2 : RHEL6.7

网络配置:VIP为公网地址 , RS1,RS2 网关指向DIP 

RS1,RS2安装web server 并测试正常访问。

 

二、director环境配置:

[root@rhel7 ~]# ifconfig | grep cas
        inet 192.168.2.13  netmask 255.255.255.0  broadcast 192.168.2.255
        inet 10.0.0.1  netmask 255.255.255.0  broadcast 10.0.0.255
[root@rhel7 ~]# curl 10.0.0.10/index.html  # 测试RS1是否正常访问
<h1>10.0.0.10</h1>
[root@rhel7 ~]# curl 10.0.0.11/index.html  # 测试RS2 是否正常访问
<h1>10.0.0.11</h1>
[root@rhel7 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf  # 打开转发功能,在这里之前请将iptables关闭
[root@rhel7 ~]# sysctl -p  
net.ipv4.ip_forward = 1
[root@rhel7 ~]# vim /etc/hosts    # 注意:经过测试rhel6和rhel7 使用ipvs保存时,都会以主机名的形式,请在/etc/hosts里填写正确

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
# director
192.168.2.13    rhel7
10.0.0.1        rhel7

# realserver
10.0.0.10       RS1
10.0.0.11       RS2

[root@rhel7 ~]# yum install ipvsadm -y


三、IPVS配置:

[root@rhel7 ~]# ipvsadm -A -t 192.168.2.13:80 -s rr    # -A 新增 -t tcp协议 VIP socket -s 算法 这里使用静态rr算法以方便测试

# 当访问192.168.2.13:80 通过nat方式转发到后端的realserver
[root@rhel7 ~]# ipvsadm -a -t 192.168.2.13:80 -r 10.0.0.10 -m -w 1    
[root@rhel7 ~]# ipvsadm -a -t 192.168.2.13:80 -r 10.0.0.11 -m -w 1
[root@rhel7 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.2.13:80 rr
  -> 10.0.0.10:80                 Masq    1      0          0         
  -> 10.0.0.11:80                 Masq    1      0          0
[root@rhel7 ~]# ipvsadm-save > /etc/sysconfig/ipvsadm  # 保存相关配置,关闭和开启服务都会去/etc/sysconfig/ipvsadm读取
[root@rhel7 ~]# cat !$
cat /etc/sysconfig/ipvsadm
-A -t rhel7:http -s rr
-a -t rhel7:http -r RS1:http -m -w 1
-a -t rhel7:http -r RS2:http -m -w 1
[root@rhel7 ~]# systemctl status ipvsadm.service
ipvsadm.service - Initialise the Linux Virtual Server
   Loaded: loaded (/usr/lib/systemd/system/ipvsadm.service; enabled)
   Active: active (exited) since Sun 2016-04-24 15:30:48 CST; 1s ago
  Process: 4069 ExecStart=/bin/bash -c exec /sbin/ipvsadm-restore < /etc/sysconfig/ipvsadm (code=exited, status=0/SUCCESS)
 Main PID: 4069 (code=exited, status=0/SUCCESS)

Apr 24 15:30:48 rhel7 systemd[1]: Starting Initialise the Linux Virtual Server...
Apr 24 15:30:48 rhel7 systemd[1]: Started Initialise the Linux Virtual Server.

四、测试

       

测试成功。

[root@rhel7 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.2.13:80 rr
  -> 10.0.0.10:80                 Masq    1      0          5         
  -> 10.0.0.11:80                 Masq    1      0          6 

可以看到InActConn是轮询的方式进行的。

 

posted @ 2016-04-24 15:35  hukey  阅读(454)  评论(0编辑  收藏  举报