天天看点

linux 负载均衡(4层)

拓扑

                              ++++++++++++

                              +     Client            +   eth0 192.168.1.1/24

            |

             |

                              ++++++++++++   up eth0:192.168.1.254/24

                              +        GW             +

                              ++++++++++++   down eth1:1.1.1.254/24

                                          |

                                           |

                              ++++++++++++   VIP eth0:1.1.1.1/24

                              +      Director        +

                              ++++++++++++   DIP eth1:172.16.1.254/24

                        ___________|____________

                       |                                         | 

                       |                                         |

          ++++++++++++            ++++++++++++

          +  Real Server A  +               +  Real Server B  +

           eth0:172.16.1.1/24             eth0:172.16.1.2/24

配置LVS VS/NAT模式

=================================

Client:

[root@localhost ~]# route add default gw 192.168.1.254 dev eth0

设置Client 的网关

GW:

[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

在网关服务器设置开启转发

Real Server A & Real Server B:

[root@localhost ~]# yum install httpd    

在两台网站服务器装httpd    

A /var/www/html/index.html A的网页zhangyunming1 

A /var/www/html/index.html B的网页zhangyunming2

  [root@localhost ~]# route add default gw 172.16.1.254 dev eth0

为eth0设置网关

Director:

[root@localhost ~]# route add default gw 1.1.1.254 dev eth0

[root@localhost ~]# yum install ipvsadm

用yum装ipvsadm包

[root@localhost ~]# ipvsadm -A -t 1.1.1.1:80 -s rr

[root@localhost ~]# ipvsadm -a -t 1.1.1.1:80 -r 172.16.1.1:80 -m

[root@localhost ~]# ipvsadm -a -t 1.1.1.1:80 -r 172.16.1.2:80 -m

[root@localhost ~]# ipvsadm -Ln

<a href="http://s3.51cto.com/wyfs02/M00/22/F1/wKiom1Mr9lbxPP4SAADZ_2_bk4U766.jpg" target="_blank"></a>

[root@localhost ~]# ipvsadm -Ln --stat

<a href="http://s3.51cto.com/wyfs02/M02/22/F3/wKioL1Mr9oehngULAADceLf5SJE334.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/22/F3/wKioL1Mr9WPgmGaKAAHaf_S8K1g955.jpg" target="_blank"></a>

备注数据包走向

====================================

1.Client----------&gt;GW

sip:CIP dip:VIP

smac:Client_mac dmac:GW_up_mac

2.GW--------------&gt;Director

smac:GW_down_mac dmac:VIP_mac

3.Director--------&gt;Real Server (DNAT)

sip:CIP dip:RIP

smac:DIP_mac dmac:RealServer_mac

4.Real Server-----&gt;Director

sip:RIP dip:CIP

smac:RealServer_mac dmac:DIP_mac

5.Director--------&gt;GW

sip:VIP dip:CIP

smac:VIP_mac dmac:GW_down_mac

6.GW--------------&gt;Client

smac:GW_up_mac dmac:Client_mac

本文转自zhang25yun51CTO博客,原文链接: http://blog.51cto.com/1585654/1381068,如需转载请自行联系原作者

继续阅读