使用Docker Swarm进行负载均衡

4
我配置了一个由两个树莓派3型号B运行Raspbian的堆栈组成的2节点docker swarm,其中有一个管理节点pi-manager和一个工作节点pi-worker。我创建了一个图像,在本地主机上运行一个类似于helloworld的网页,并且还可以识别页面所在的容器。例如:

example of docker image webpage

从docker管理节点,我可以创建“flairhello”服务,然后将其扩展到2个容器(我们称之为A和B)。

期望的行为:

我期望得到的行为是,当我访问每个本地主机时,将显示显示在该主机上运行的容器ID的网页。例如,如果pi-manager有容器A,而pi-worker有容器B,当我访问pi-manager的本地主机地址时,我希望看到A的容器ID,当我访问pi-worker的本地主机地址时,我希望看到B的容器ID。但这不是正在发生的。

当前行为:

我遇到的现象是,当我在pi-managerpi-worker上访问本地主机时,我会被指向容器A的网页,然后在接下来一段时间内,无论我访问哪个本地主机(pi-managerpi-worker),我总是被导向容器B。我想这是Docker Swarm内置的负载均衡吗?

问题:

如何使用负载均衡器从我的Swarm获得我想要的行为?

我可以使用哪些工具?Docker compose?Haproxy图像?(从阅读中看到了这些)

是否有任何好的教程介绍此过程?


更新:

在我的答案中创建一个带有HAProxy的网络负载均衡器的步骤!

2个回答

4

我正在开发这个项目作为一种示例,以展示负载均衡的功能,所以当我进入一个主页面时,流量会被重定向到运行在“pi-manager”和“pi-worker”的这两个不同容器上。因此,如果您刷新页面,容器地址将不同,并且可以说明负载平衡功能。Nginx或HaProxy是否支持这个功能呢?非常感谢您提供的示例,我看到它是一个粘性连接,它到底是如何工作的呢? - ob1
你认为我能将这个Docker文件转换为在ARM架构上运行吗? - ob1
如果容器中没有正在运行的网络服务,我是否仍然会被重定向到具有服务运行的节点? - ob1

2
感谢 @kleinsasserm 指引我找到了解决问题的方法!我使用基于 HAProxy 的 Docker 镜像创建了一个负载均衡器,并使用其轮询算法创建了一个 Hello World 网页,该网页将显示它所在的容器,并在每次刷新时交替更换容器!这是一个实验项目,用于演示使用 Docker 进行负载均衡。
以下是此解决方案的步骤:
为了设置和配置此负载均衡器,我执行了以下操作:
  1. Created HAProxy Docker Image

    • Create a directory for your image

      $ mkdir haproxyImage
      $ cd hapoxyImage
      
    • Create you're docker file with the following contents

      FROM haproxy:1.7
      COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
      
    • Create your haproxy.cfg file

      global
          daemon                       
          log 127.0.0.1 local0 notice 
          maxconn 256                 
      
      
      defaults
          log     global
          mode    http
          option  httplog
          option  dontlognull
          retries 3
          option redispatch
          timeout connect  5000
          timeout client  10000
          timeout server  10000
      
      listen stats
          bind 0.0.0.0:80 # This is the page you will access
          mode http
          stats enable
          stats uri /haproxy?stats
          stats realm Strictly\ Private
          stats auth A_Username:user
          stats auth Another_User:password
          balance roundrobin # Defines our balancing algorithm as round robin.
          option httpclose
          option forwardfor
          server pi-manager <ip address>:8080 check # Docker node
          server pi-worker1 <ip address>:8080 check # Docker node
          # Add more docker nodes here
      
    • Build your Docker image

      $ docker build -t swarm-haproxy .
      
  2. Start your Docker Swarm Service:

    $ docker service create -p 8080:80 --name helloworld --replicas 10 <image name>
    
  3. Start you HAProxy image I am running this on a computer not the pi stack

    $ docker run -d -p 80:80 swarm-haproxy
    
  4. On the Machine running the HAproxy image go to http://0.0.0.0 refresh the page to show the different containers running the same service


    References:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接