LB&HA Cluster / LVS

1.4 lvs-nat daemon

 

lvs-nat配置(采用rr调度算法):
==================================

网络拓扑图:

Client ---> (VIP) Virtual Server (DIP)
               |
               |
               |
             switch
               |
               |---------(RIP1) Real Server 1 
               |
               |---------(RIP1) Real Server 1 

VS、RS1、RS2处在同一网络;

DIP:192.168.206.55    物理网卡:0(VIP):192.168.206.200
RIP1:192.168.206.66   lo:0(VIP):192.168.206.200
RIP2:192.168.206.99   lo:0(VIP):192.168.206.200

GATEWAY:192.168.206.2

1、同步网络时间:~]# ntpdate  ntp1.aliyun.com

备注,阿里云有提供ECS内网及外网的NTP服务;
以下为阿里云提供的内网和公网NTP服务器列表:

经典网络                                                   VPC网络                                   公网
ntp1.cloud.aliyuncs.com           ntp7.cloud.aliyuncs.com           ntp1.aliyun.com
ntp2.cloud.aliyuncs.com          ntp8.cloud.aliyuncs.com           ntp2.aliyun.com
ntp3.cloud.aliyuncs.com          ntp9.cloud.aliyuncs.com           ntp3.aliyun.com
ntp4.cloud.aliyuncs.com          ntp10.cloud.aliyuncs.com         ntp4.aliyun.com
ntp5.cloud.aliyuncs.com          ntp11.cloud.aliyuncs.com          ntp5.aliyun.com
ntp6.cloud.aliyuncs.com          ntp12.cloud.aliyuncs.com         ntp6.aliyun.com

2、ipvs集群服务器上安装ipvsadm工具;

~]# yum  -y  install  ipvsadm

3、2台RS主机各安装nginx,并配置一个index.html文件,内容不一样即可(方便测试);

4、2台RS的网关地址需指向VS的DIP地址;

5、在ipvs集群服务器上配置集群服务、添加RS主机;

采用轮询调度算法;
~]# ipvsadm  -A  -t  192.168.199.145:80  -s  rr

添加2台RS:
~]# ipvsadm  -a  -t  192.168.199.145:80  -r  192.168.206.66  -m
~]# ipvsadm  -a  -t  192.168.199.145:80  -r  192.168.206.99  -m

6、开启转发功能:
~]# sysctl  -w  net.ipv4.ip_forward=1

7、测试:

~]# for  i  in  {1..10};  do  curl  http://192.168.199.145; done

<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>

lvs-nat配置(采用wrr调度算法):
==================================

1>修改RS的权重:

~]# ipvsadm  -e  -t  192.168.199.145:80  -r  192.168.206.66  -m  -w  2
~]# ipvsadm  -e  -t  192.168.199.145:80  -r  192.168.206.99  -m  -w  3

2>修改ipvs集群服务的调度算法:
~]# ipvsadm  -E  -t  192.168.199.145:80  -s  wrr

3>测试:

]# for  i  in  {1..20};  do  curl  http://192.168.199.145; done

<h2> hello 192.168.206.99 ..........  </h2>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>
<h2> hello 192.168.206.99 ..........  </h2>
<h1> hello 192.168.206.66 </h1>

4>查看ipvs集群的连接状态:

]# ipvsadm  -Ln  –stats

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.199.145:80                 80      591      465    64950    53048
  -> 192.168.206.66:80                  36      319      263    41627    32556
  -> 192.168.206.99:80                  44      272      202    23323    20492

5> 测试清空计数器:

~]# ipvsadm -Z

~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.199.145:80                  0        0        0        0        0
  -> 192.168.206.66:80                   0        0        0        0        0
  -> 192.168.206.99:80                   0        0        0        0        0

重新多次访问后,再次查看状态信息;
~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.199.145:80                 20      118       99    13220     9704
  -> 192.168.206.66:80                   8       46       39     5240     3800
  -> 192.168.206.99:80                  12       72       60     7980     5904

 

Leave a Reply

Your email address will not be published. Required fields are marked *