LVS常见的类型实现方式

前言

 由于lvs的基础知识已经在第一篇lvs中讲解了,所以在这里只做实验,包括lvs-nat,lvs-dr,以及基于Freiwall标记和实现会话绑定实验。

一、lvs-nat:也是MASQERADING,简称为m(masquerading) 

  实验图:  2015-06-21_122212.png

  地址规划:

    VIP: 172.16.2.13/24

    DIP: 192.168.10.203/24

    RIP1:192.168.10.120/24

    RIP2:192.168.10.103/24

    CIP: 172.16.2.176/24

  

 实验步骤:

 (1)在Director上配置ntp服务,RS同步Director时间;

[root@Director ~]# date
Sun Jun 21 12:46:46 CST 2015

[root@RS1 ~]# date
Sun Jun 21 12:46:50 CST 2015

[root@RS2 ~]# date
Sun Jun 21 12:46:53 CST 2015

 (2)开启Director的转发功能,清空iptables的filter,nat规则

[root@Director ~]# vim /etc/sysctl.conf 
    net.ipv4.ip_forward = 1  \\修改为1

[root@Director ~]# sysctl -p \\重新读取systcl.conf配置文件

[root@Director ~]# iptables -F;iptables -F -t nat \\清除防火墙规则

 (3)将RS的网关指向Director的DIP

[root@RS1 ~]# route add default gw 192.168.10.203
[root@RS1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0     0.0.0.0         255.255.255.0      U   0   0     0 eth0
169.254.0.0     0.0.0.0         255.255.0.0        U   1002  0     0 eth0
0.0.0.0       192.168.10.203      0.0.0.0         UG   0   0     0 eth0
[root@RS2 ~]# route add default gw 192.168.10.203
[root@RS2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    0.0.0.0         255.255.255.0       U   0    0      0 eth0
169.254.0.0     0.0.0.0         255.255.0.0        U   1002  0      0 eth0
0.0.0.0      192.168.10.203      0.0.0.0          UG   0   0      0 eth0

 (4)在Director上创建集群服务

[root@Director ~]# ipvsadm -A -t 172.16.2.13:80 -s rr
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.2.13:80 rr

 (5)在Director创建的集群服务添加RS

[root@Director ~]# ipvsadm -a -t 172.16.2.13:80 -r 192.168.10.120:80 -m -w 1
[root@Director ~]# ipvsadm -a -t 172.16.2.13:80 -r 192.168.10.103:80 -m -w 1
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.2.13:80 rr
  -> 192.168.10.103:80            Masq    1      0          0         
  -> 192.168.10.120:80            Masq    1      0          0

 (6)在RS中添加默认网页

[root@RS1 ~]# echo "<h1>RS1</h1>" > /var/www/html/index.html 
[root@RS2 ~]# echo "<h1>RS2</h1>" > /var/www/html/index.html

 (7)启动httpd服务

[root@RS1 ~]# service httpd start
Starting httpd:                                            [  OK  ]

[root@RS2 ~]# service httpd start
Starting httpd:                                            [  OK  ]

 (7)打开浏览器,输入地址:http://172.16.2.13 刷新网页,查看测试结果

 3.png  2.png

 (8)到此lvs-nat类型已经搭建完成,可以自己更改调度方法,查看状态

二、lvs-dr类型:也称direct routing,简称为g(gatewaying)

 实验图:

1.png

 地址规划:

   VIP: 172.16.2.100

   DIP: 172.16.2.13

   RIP1:172.16.2.12

   RIP2:172.16.2.14

 配置步骤

 (1)同步时间,以Director为时间服务器,RS以Director为时间服务器,同步时间

[root@Director ~]# date 
Sun Jun 21 13:24:02 CST 2015

[root@RS1 htdocs]# date
Sun Jun 21 13:24:06 CST 2015

[root@RS2 html]# date
Sun Jun 21 13:24:08 CST 2015

 (2)修改RS的内核参数,禁止响应路由器查询VIP的arp请求

[root@RS1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@RS1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@RS1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@RS1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@RS2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@RS2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@RS2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@RS2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

 (3)修改Director的防火墙规则,开启转发功能

[root@Director ~]# iptables -F;iptables -F -t nat
[root@Director ~]# vim /etc/sysctl.conf 
    net.ipv4.ip_forward = 1  
[root@Director ~]# sysctl -p

 (4)在Director、RS服务器上配置VIP地址

[root@Director ~]# ifconfig eth1:0 172.16.2.100 broadcast 172.16.2.100 netmask 255.255.255.255
[root@Director ~]# ifconfig 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:5A:4F:52  
          inet addr:172.16.2.13  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe5a:4f52/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:34718 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12253 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:14209969 (13.5 MiB)  TX bytes:1351338 (1.2 MiB)

eth1:0    Link encap:Ethernet  HWaddr 00:0C:29:5A:4F:52  
          inet addr:172.16.2.100  Bcast:172.16.2.100  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:80 errors:0 dropped:0 overruns:0 frame:0
          TX packets:80 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:7266 (7.0 KiB)  TX bytes:7266 (7.0 KiB)
[root@RS1 ~]# ifconfig lo:0 172.16.2.100 broadcast 172.16.2.100 netmask 255.255.255.255
[root@RS1 ~]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:33  
          inet addr:172.16.2.12  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe0e:c433/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9328 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3795 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8278339 (7.8 MiB)  TX bytes:337391 (329.4 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:59 errors:0 dropped:0 overruns:0 frame:0
          TX packets:59 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:5699 (5.5 KiB)  TX bytes:5699 (5.5 KiB)

lo:0      Link encap:Local Loopback  
          inet addr:172.16.2.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
[root@RS2 ~]# ifconfig lo:0 172.16.2.100 broadcast 172.16.2.100 netmask 255.255.255.255
[root@RS2 ~]# ifconfig 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:F8:D4:92  
          inet addr:172.16.2.14  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef8:d492/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:161326 errors:0 dropped:0 overruns:0 frame:0
          TX packets:132169 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:25610678 (24.4 MiB)  TX bytes:16331857 (15.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:1935 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1935 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:197004 (192.3 KiB)  TX bytes:197004 (192.3 KiB)

lo:0      Link encap:Local Loopback  
          inet addr:172.16.2.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

 (5)在Director、RS上添加去往172.16.2.100的主机路由(目的是RS构建响应报文的时候以VIP为源IP封装数据包)

[root@Director ~]# route add -host 172.16.2.100 dev eth1:0
[root@Director ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.2.100    0.0.0.0         255.255.255.255       UH   0   0      0 eth1
172.16.2.0     0.0.0.0         255.255.255.0        U    0   0      0 eth1
0.0.0.0      172.16.2.1          0.0.0.0         UG   0   0      0 eth1
[root@RS1 ~]# route  add -host 172.16.2.100 dev lo:0
[root@RS1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.2.100    0.0.0.0         255.255.255.255      UH   0   0      0 lo
172.16.2.0     0.0.0.0         255.255.255.0       U    0   0      0 eth1
0.0.0.0      172.16.2.1        0.0.0.0          UG   0   0      0 eth1
[root@RS2 ~]# route add  -host 172.16.2.100 dev lo:0
[root@RS2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.2.100    0.0.0.0         255.255.255.255      UH   0   0     0   lo
172.16.2.0     0.0.0.0         255.255.255.0        U    0   0     0  eth1
0.0.0.0       172.16.2.1       0.0.0.0           UG   0   0     0  eth1

 (6)在Director上创建服务集群

[root@Director ~]# ipvsadm -A -t 172.16.2.100:80 -s rr
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.2.100:80 rr

 (7)在Director创建的集群中添加RS

[root@Director ~]# ipvsadm -a -t 172.16.2.100:80 -r 172.16.2.12:80 -g -w 1
[root@Director ~]# ipvsadm -a -t 172.16.2.100:80 -r 172.16.2.14:80 -g -w 1
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.2.100:80 rr
  -> 172.16.2.12:80               Route   1      0          0         
  -> 172.16.2.14:80               Route   1      0          0

 (8)在RS上添加默认网页

[root@RS1 ~]# echo "<h1>RS1</h1>" > /var/www/html/index.html 
[root@RS2 ~]# echo "<h1>RS2</h1>" > /var/www/html/index.html

 (9)启动httpd服务

[root@RS1 ~]# service httpd start
Starting httpd:                                            [  OK  ]

[root@RS2 ~]# service httpd start
Starting httpd:                                            [  OK  ]

 (10)打开浏览器,输入地址:http://172.16.2.100,刷新页面查看变化

4.png 5.png 

 (11)到此lvs-dr类型搭建完成,可以更改调度算法查看页面变化。

原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/5500

(0)
上一篇 2015-06-26 16:39
下一篇 2015-06-30 13:11

相关推荐

  • linux文本处理工具

    linux文本处理工具 1.Linux 文件系统上的特殊权限: SUID     SGID   Sticky       进程的安全上下文:(1)任何一个可执行程序文件能不能启动为进程,取决发起者对程序文件是否拥有执行去权限       &…

    Linux干货 2017-02-25
  • 20161028第9天作业

    20161028第9天作业 1、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问” 2、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率率,如果超过80%,就发广播…

    Linux干货 2016-10-30
  • 软件包管理

    rpm 与 yum 的用法 源代码:name-VERSION.tar.gz|bz2|xz VERSION: major.minor.release rpm包命名方式: name-VERSION-release.arch.rpm 例:bash-4.2.46-19.el7.x86_64.rpm VERSION: major.minor.release rele…

    Linux干货 2016-09-01
  • ​iptables简单介绍

    iptables简单介绍 什么是防火墙? 防火墙是工作在主机或网络边缘,能够对其所匹配到的报文根据事先定义好的规则作出相应处理的组件,可以是软件,也可以是硬件,还能软硬结合实现。 UNIX/Linux中对防火墙的实现 早期在openBSD中通过内核中的ipfw实现简单的数据报过滤功能、后来在Linux 2.2内核中使用ipchains来取代,意为链、后来在L…

    Linux干货 2016-03-31
  • 8月2日作业

    在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@localhost ~]# groupadd g1 [root@localhost ~]# groupadd g2 [root@local…

    Linux干货 2016-08-05
  • 网络管理(二)之IP地址划分子网、多块网卡共用单一IP

    网络管理(二)IP地址   一、认识学习IP地址的组成: 1、它们可唯一标识IP 网络中的每台设备 2、IP地址由两部分组成: 网络ID:标识网络;每个网段分配一个网络ID 主机ID:标识单个主机;由组织分配给各设备 3、点分十进制计法表示IPv4地址: 4、如下图,将系统中的IP地址用二进制表示,再通过转换合成的十进制数,使用ping命令可得出:…

    Linux干货 2016-09-05

评论列表(2条)

  • 黑白子
    黑白子 2015-12-08 21:30

    你好,怎么做dr模型时,轮询不起作用啊?

    • 马行空
      马行空 2015-12-25 10:32

      @黑白子把浏览器缓存清理一下,在访问的时候查看访问日志,看看日志里有没有访问记录。