如何使用 Weave 以及 Docker 搭建 Nginx 反向代理/负载均衡服务

Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器。Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动发现。它可以让我们更加专注于应用的开发,而不是基础架构。Weave 提供了一个如此棒的环境,仿佛它的所有容器都属于同个网络,不需要端口/映射/连接等的配置。容器中的应用提供的服务在 weave 网络中可以轻易地被外部世界访问,不论你的容器运行在哪里。在这个教程里我们将会使用 weave 快速并且简单地将 nginx web 服务器部署为一个负载均衡器,反向代理一个运行在 Amazon Web Services 里面多个节点上的 docker 容器中的简单 php 应用。这里我们将会介绍 WeaveDNS,它提供一个不需要改变代码就可以让容器利用主机名找到的简单方式,并且能够让其他容器通过主机名连接彼此。

在这篇教程里,我们将使用 nginx 来将负载均衡分配到一个运行 Apache 的容器集合。最简单轻松的方法就是使用 Weave 来把运行在 Ubuntu 上的 docker 容器中的 nginx 配置成负载均衡服务器。

如何使用 Weave 以及 Docker 搭建 Nginx 反向代理/负载均衡服务

 

1. 搭建 AWS 实例

首先,我们需要搭建 Amzaon Web Service 实例,这样才能在 ubuntu 下用 weave 跑 docker 容器。我们将会使用AWS 命令行 来搭建和配置两个 AWS EC2 实例。在这里,我们使用最小的可用实例,t1.micro。我们需要一个有效的Amazon Web Services 账户使用 AWS 命令行界面来搭建和配置。我们先在 AWS 命令行界面下使用下面的命令将 github 上的 weave 仓库克隆下来。

  1. <span class="pln">$ git clone https</span><span class="pun">:</span><span class="com">//github.com/weaveworks/guides</span>
  2. <span class="pln">$ cd weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">/</span><span class="pln">aws</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">ubuntu</span><span class="pun">-</span><span class="pln">simple</span>

在克隆完仓库之后,我们执行下面的脚本,这个脚本将会部署两个 t1.micro 实例,每个实例中都是 ubuntu 作为操作系统并用 weave 跑着 docker 容器。

  1. <span class="pln">$ sudo </span><span class="pun">./</span><span class="pln">demo</span><span class="pun">-</span><span class="pln">aws</span><span class="pun">-</span><span class="pln">setup</span><span class="pun">.</span><span class="pln">sh</span>

在这里,我们将会在以后用到这些实例的 IP 地址。这些地址储存在一个 weavedemo.env 文件中,这个文件创建于执行 demo-aws-setup.sh 脚本期间。为了获取这些 IP 地址,我们需要执行下面的命令,命令输出类似下面的信息。

  1. <span class="pln">$ cat weavedemo</span><span class="pun">.</span><span class="pln">env</span>
  2. <span class="kwd">export</span><span class="pln"> WEAVE_AWS_DEMO_HOST1</span><span class="pun">=</span><span class="lit">52.26</span><span class="pun">.</span><span class="lit">175.175</span>
  3. <span class="kwd">export</span><span class="pln"> WEAVE_AWS_DEMO_HOST2</span><span class="pun">=</span><span class="lit">52.26</span><span class="pun">.</span><span class="lit">83.141</span>
  4. <span class="kwd">export</span><span class="pln"> WEAVE_AWS_DEMO_HOSTCOUNT</span><span class="pun">=</span><span class="lit">2</span>
  5. <span class="kwd">export</span><span class="pln"> WEAVE_AWS_DEMO_HOSTS</span><span class="pun">=(</span><span class="lit">52.26</span><span class="pun">.</span><span class="lit">175.175</span><span class="lit">52.26</span><span class="pun">.</span><span class="lit">83.141</span><span class="pun">)</span>

请注意这些不是固定的 IP 地址,AWS 会为我们的实例动态地分配 IP 地址。

我们在 bash 下执行下面的命令使环境变量生效。

  1. <span class="pun">.</span><span class="pun">./</span><span class="pln">weavedemo</span><span class="pun">.</span><span class="pln">env</span>

 

2. 启动 Weave 和 WeaveDNS

在安装完实例之后,我们将会在每台主机上启动 weave 以及 weavedns。Weave 以及 weavedns 使得我们能够轻易地将容器部署到一个全新的基础架构以及配置中, 不需要改变代码,也不需要去理解像 Ambassador 容器以及 Link 机制之类的概念。下面是在第一台主机上启动 weave 以及 weavedns 的命令。

  1. <span class="pln">ssh </span><span class="pun">-</span><span class="pln">i weavedemo</span><span class="pun">-</span><span class="pln">key</span><span class="pun">.</span><span class="pln">pem ubuntu@$WEAVE_AWS_DEMO_HOST1</span>
  2. <span class="pln">$ sudo weave launch</span>
  3. <span class="pln">$ sudo weave launch</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.2</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">/</span><span class="lit">24</span>

下一步,我也准备在第二台主机上启动 weave 以及 weavedns。

  1. <span class="pln">ssh </span><span class="pun">-</span><span class="pln">i weavedemo</span><span class="pun">-</span><span class="pln">key</span><span class="pun">.</span><span class="pln">pem ubuntu@$WEAVE_AWS_DEMO_HOST2</span>
  2. <span class="pln">$ sudo weave launch $WEAVE_AWS_DEMO_HOST1</span>
  3. <span class="pln">$ sudo weave launch</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.2</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">/</span><span class="lit">24</span>

 

3. 启动应用容器

现在,我们准备跨两台主机启动六个容器,这两台主机都用 Apache2 Web 服务实例跑着简单的 php 网站。为了在第一个 Apache2 Web 服务器实例跑三个容器, 我们将会使用下面的命令。

  1. <span class="pln">ssh </span><span class="pun">-</span><span class="pln">i weavedemo</span><span class="pun">-</span><span class="pln">key</span><span class="pun">.</span><span class="pln">pem ubuntu@$WEAVE_AWS_DEMO_HOST1</span>
  2. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">h ws1</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">apache</span>
  3. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">h ws2</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">apache</span>
  4. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.3</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">h ws3</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">apache</span>

在那之后,我们将会在第二个实例上启动另外三个容器,请使用下面的命令。

  1. <span class="pln">ssh </span><span class="pun">-</span><span class="pln">i weavedemo</span><span class="pun">-</span><span class="pln">key</span><span class="pun">.</span><span class="pln">pem ubuntu@$WEAVE_AWS_DEMO_HOST2</span>
  2. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.4</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">h ws4</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">apache</span>
  3. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.5</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">h ws5</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">apache</span>
  4. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.6</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">h ws6</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">apache</span>

注意: 在这里,--with-dns 选项告诉容器使用 weavedns 来解析主机名,-h x.weave.local 则使得 weavedns 能够解析该主机。

 

4. 启动 Nginx 容器

在应用容器如预期的运行后,我们将会启动 nginx 容器,它将会在六个应用容器服务之间轮询并提供反向代理或者负载均衡。 为了启动 nginx 容器,请使用下面的命令。

  1. <span class="pln">ssh </span><span class="pun">-</span><span class="pln">i weavedemo</span><span class="pun">-</span><span class="pln">key</span><span class="pun">.</span><span class="pln">pem ubuntu@$WEAVE_AWS_DEMO_HOST1</span>
  2. <span class="pln">$ sudo weave run </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">dns </span><span class="lit">10.3</span><span class="pun">.</span><span class="lit">1.7</span><span class="pun">/</span><span class="lit">24</span><span class="pun">-</span><span class="pln">ti </span><span class="pun">-</span><span class="pln">h nginx</span><span class="pun">.</span><span class="pln">weave</span><span class="pun">.</span><span class="kwd">local</span><span class="pun">-</span><span class="pln">d </span><span class="pun">-</span><span class="pln">p </span><span class="lit">80</span><span class="pun">:</span><span class="lit">80</span><span class="pln"> fintanr</span><span class="pun">/</span><span class="pln">weave</span><span class="pun">-</span><span class="pln">gs</span><span class="pun">-</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">simple</span>

因此,我们的 nginx 容器在 $WEAVEAWSDEMO_HOST1 上公开地暴露成为一个 http 服务器。

 

5. 测试负载均衡服务器

为了测试我们的负载均衡服务器是否可以工作,我们执行一段可以发送 http 请求给 nginx 容器的脚本。我们将会发送6个请求,这样我们就能看到 nginx 在一次的轮询中服务于每台 web 服务器之间。

  1. <span class="pln">$ </span><span class="pun">./</span><span class="pln">access</span><span class="pun">-</span><span class="pln">aws</span><span class="pun">-</span><span class="pln">hosts</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="pun">{</span>
  3. <span class="str">"message"</span><span class="pun">:</span><span class="str">"Hello Weave - nginx example"</span><span class="pun">,</span>
  4. <span class="str">"hostname"</span><span class="pun">:</span><span class="str">"ws1.weave.local"</span><span class="pun">,</span>
  5. <span class="str">"date"</span><span class="pun">:</span><span class="str">"2015-06-26 12:24:23"</span>
  6. <span class="pun">}</span>
  7. <span class="pun">{</span>
  8. <span class="str">"message"</span><span class="pun">:</span><span class="str">"Hello Weave - nginx example"</span><span class="pun">,</span>
  9. <span class="str">"hostname"</span><span class="pun">:</span><span class="str">"ws2.weave.local"</span><span class="pun">,</span>
  10. <span class="str">"date"</span><span class="pun">:</span><span class="str">"2015-06-26 12:24:23"</span>
  11. <span class="pun">}</span>
  12. <span class="pun">{</span>
  13. <span class="str">"message"</span><span class="pun">:</span><span class="str">"Hello Weave - nginx example"</span><span class="pun">,</span>
  14. <span class="str">"hostname"</span><span class="pun">:</span><span class="str">"ws3.weave.local"</span><span class="pun">,</span>
  15. <span class="str">"date"</span><span class="pun">:</span><span class="str">"2015-06-26 12:24:23"</span>
  16. <span class="pun">}</span>
  17. <span class="pun">{</span>
  18. <span class="str">"message"</span><span class="pun">:</span><span class="str">"Hello Weave - nginx example"</span><span class="pun">,</span>
  19. <span class="str">"hostname"</span><span class="pun">:</span><span class="str">"ws4.weave.local"</span><span class="pun">,</span>
  20. <span class="str">"date"</span><span class="pun">:</span><span class="str">"2015-06-26 12:24:23"</span>
  21. <span class="pun">}</span>
  22. <span class="pun">{</span>
  23. <span class="str">"message"</span><span class="pun">:</span><span class="str">"Hello Weave - nginx example"</span><span class="pun">,</span>
  24. <span class="str">"hostname"</span><span class="pun">:</span><span class="str">"ws5.weave.local"</span><span class="pun">,</span>
  25. <span class="str">"date"</span><span class="pun">:</span><span class="str">"2015-06-26 12:24:23"</span>
  26. <span class="pun">}</span>
  27. <span class="pun">{</span>
  28. <span class="str">"message"</span><span class="pun">:</span><span class="str">"Hello Weave - nginx example"</span><span class="pun">,</span>
  29. <span class="str">"hostname"</span><span class="pun">:</span><span class="str">"ws6.weave.local"</span><span class="pun">,</span>
  30. <span class="str">"date"</span><span class="pun">:</span><span class="str">"2015-06-26 12:24:23"</span>
  31. <span class="pun">}</span>

 

结束语

我们最终成功地将 nginx 配置成一个反向代理/负载均衡服务器,通过使用 weave 以及运行在 AWS(Amazon Web Service)EC2 里面的 ubuntu 服务器中的 docker。从上面的步骤输出可以清楚的看到我们已经成功地配置了 nginx。我们可以看到请求在一次轮询中被发送到6个应用容器,这些容器在 Apache2 Web 服务器中跑着 PHP 应用。在这里,我们部署了一个容器化的 PHP 应用,使用 nginx 横跨多台在 AWS EC2 上的主机而不需要改变代码,利用 weavedns 使得每个容器连接在一起,只需要主机名就够了,眼前的这些便捷, 都要归功于 weave 以及 weavedns。

如果你有任何的问题、建议、反馈,请在评论中注明,这样我们才能够做得更好,谢谢:-)

更多Nginx相关教程见以下内容

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里


via: http://linoxide.com/linux-how-to/nginx-load-balancer-weave-docker/

作者:Arun Pyasi 译者:dingdongnigetou 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关推荐