介绍

lvs是一款出色的负载均衡软件,被广泛的用于各大知名网站,其支持多种负载均衡方式,如:[VS/NAT,VS/TUN,VS/DR和full NAT,原来在搭建一个lvs的测试环境时,往往需要多台物理主机或者是虚拟机,在本文中将介绍一种使用docker在一个虚拟机上搭建LVS集群的方式,方便开发人员在开发和测试阶段使用

环境搭建

  1. 安装虚拟机环境,这里在window7上,使用virtualbox,安装64位的centos7,在安装过程选择 minimal 就可以了
  2. 由于在这个环境的搭建中使用到了docker和 docker-compose,所以首先设置国内比较快的yum源,推荐使用阿里云的yum源
  3. 由于使用到了docker-compose,并且通过pip来安装docker-compose,因此先通过yum来安装 pip,步骤如下:

              | yum -y install epel-release |
              | yum -y install python-pip   |
              | yum clean all               |
    
  4. 通过pip安装docker-compose

           |   pip install -U docker-compose  |
    
  5. 从github上将附件下载下来,放置到linux操作系统的某个目录下

  6. 解压后,在解压的目录下,运行 docker-compose up

    这样就通过docker-compose将这个lvs的运行环境搭建成功了

    相关配置的分析

    在这个环境中,lvs的两个docker容器的ip,如下表所示,在dockermaster_lvs1 上有分配的lvs分配的vip:172.17.0.8

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED            STATUS              PORTS               NAMES
9e52f1b85d79        dockermaster_client1   "sleep 2h"          2 days ago         Up 2 minutes        22/tcp              dockermaster_client1_1
ca7970401fa0        dockermaster_web2      "sh local.rc"       2 days ago         Up 2 minutes        22/tcp, 80/tcp      dockermaster_web2_1
0018316201a0        dockermaster_web1      "sh local.rc"       2 days ago         Up 2 minutes        22/tcp, 80/tcp      dockermaster_web1_1
ed93d015bc56        dockermaster_lvs1      "/app/run.sh"       2 days ago         Up 3 minutes        22/tcp, 80/tcp      dockermaster_lvs1_1
eac75212a3b7        dockermaster_lvs2      "/app/run.sh"       2 days ago         Up 3 minutes        22/tcp, 80/tcp      dockermaster_lvs2_1
[root@localhost ~]# docker exec -ti ed93d015bc56 /bin/sh
sh-4.1# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.17.0.8/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link
       valid_lft forever preferred_lft forever
sh-4.1# exit[root@localhost ~]#
[root@localhost ~]# docker exec -ti eac75212a3b7 /bin/sh
sh-4.1# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever

而在docker的web容器中,ip的情况如下,在这两个docker容器的lo网卡上都绑定了 172.17.0.8 这个VIP

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
9e52f1b85d79        dockermaster_client1   "sleep 2h"          2 days ago          Up 13 minutes       22/tcp              dockermaster_client1_1
ca7970401fa0        dockermaster_web2      "sh local.rc"       2 days ago          Up 13 minutes       22/tcp, 80/tcp      dockermaster_web2_1
0018316201a0        dockermaster_web1      "sh local.rc"       2 days ago          Up 13 minutes       22/tcp, 80/tcp      dockermaster_web1_1
ed93d015bc56        dockermaster_lvs1      "/app/run.sh"       2 days ago          Up 13 minutes       22/tcp, 80/tcp      dockermaster_lvs1_1
eac75212a3b7        dockermaster_lvs2      "/app/run.sh"       2 days ago          Up 13 minutes       22/tcp, 80/tcp      dockermaster_lvs2_1
[root@localhost ~]# docker exec -ti 0018316201a0 /bin/sh
sh-4.1# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 172.17.0.8/32 brd 172.17.0.8 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.4/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:4/64 scope link
       valid_lft forever preferred_lft forever
sh-4.1# exit
exit
[root@localhost ~]# docker exec -ti ca7970401fa0 /bin/sh
sh-4.1# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 172.17.0.8/32 brd 172.17.0.8 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.5/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:5/64 scope link
       valid_lft forever preferred_lft forever
sh-4.1#

然后在docker的宿主机上,通过curl 172.17.0.8 就能进行访问

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐