LVS环境搭建

网络配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
                    +----------+
| Client |
+-----+----+
eth0|192.168.43.63
|
---------------------------------------------net 192.168.43.0/24
|
eth0|192.168.43.225
+----------+
| LVS |
+-----+----+
eth1|192.168.2.128
|
---------------------------------------------net 192.168.2.0/24
|
------------------
192.168.2.131|eth0 eth0|192.168.2.132
+----------+ +----------+
|RealServer| |RealServer|
+-----+----+ +-----+----+

环境安装

配置Server

  1. 客户端路由指向LB

    1
    route add -net 192.168.43.0 netmask 255.255.255.0 gw 192.168.2.128
  2. 配置http server

    1
    2
    systemctl start nginx
    echo "192.168.2.131" > /usr/share/nginx/html/index.html

配置LB

系统使用centos 7

  1. 安装ipvsadm

    1
    2
    3
    4
    yum -y install ipvsadm
    # enable IP forward
    echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
    sysctl -p
  2. 配置ipvs

1
2
3
4
5
6
export VIP=192.168.43.225
export RS="192.168.2.131 192.168.2.132"
ipvsadm -A -t $VIP:80 -s lc
for server in $RS; do
ipvsadm -a -t $VIP:80 -r $server:80 -m
done

其中lc表示采用基于最小连接数的负载均衡算法

测试

在客户端上用浏览器或者curl 访问

1
http://192.168.43.225

这时候会返回Real Server响应的内容

1
2
# curl http://192.168.43.225
192.168.2.131