如何使用Docker 1.12 swarm模式部署consul

时间:2016-08-24 20:13:09

标签: docker docker-swarm

我有一个由3台服务器组成的领事群。我还有一个大约6名工人和3名主人的码头工人(主人与领事服务器位于相同的硬件上但设置为可用性==排水以防止他们接受工作)。

我通常使用consul-template来阅读领事K / V.我不能为我的生活弄清楚如何合理地推出一个领事代理服务。如果我使用全局服务,那么每个节点都会获得一个代理,但服务器群集会抱怨,因为客户端代理似乎都具有相同的IP地址。

复制服务似乎是要走的路,但我相信我需要发布客户端端口8301,这似乎会导致与我的服务器集群(运行swarm主服务器和consul服务器(不在docker下)的冲突) 。

我很欣赏正确方向的一般指导 - 记住这是1.12群模式,因此与早期版本有很大不同。

4 个答案:

答案 0 :(得分:6)

令人困惑的是Docker" Swarm Mode"真的是一种不同的动物,仍称为Docker Swarm。在群体模式中,您不需要领事。每个主机上的docker守护程序充当键值存储并执行服务发现。它为“老人”中需要的领事做了一切。 Docker Swarm。

请小心查找特定于" swarm模式"的文档/信息。只要。我希望他们实际上使用了不同的名称。

答案 1 :(得分:6)

经过深思熟虑和许多死胡同,我们终于想出了一个适合我们的解决方案。问题的一部分在于,在编写本文时,Docker 1.12有些少年,并且在它有意义之前引入了许多必须被理解的概念。在我们的例子中,我们之前使用Swarm 1.12变体的经验阻碍了我们的前瞻性思维而不是帮助。

我们用于为我们的群体部署领事K / V服务的解决方案如下

  1. 创建名为' consul'的覆盖网络。这为我们的服务创建了一个地址空间。

    docker network create --driver overlay --subnet 10.10.10.0/24 consul

  2. 将consul服务器群集部署到新的覆盖中。我们有三个主机用作管理器节点,我们希望consul服务器容器在这个集群而不是应用服务器上运行,因此约束'标志

    docker service create -e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true}' --name consulserver --network consul --constraint 'node.role == manager' --replicas 3 consul agent server -bootstrap-expect=3 -bind=0.0.0.0 -retry-join="10.10.10.2" -data-dir=/tmp

    这里的关键是swarm将在领事网络的开头分配一个新的VIP(10.10.10.2),映射到三个新实例。

  3. 接下来,我们部署了代理服务

    docker service create \ -e 'CONSUL_BIND_INTERFACE=eth0' \ -e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true, "retry_join":["10.10.10.2"]}' \ --publish "8500:8500" \ --replicas 1 \ --network consul \ --name consulagent \ --constraint 'node.role != manager' \ consul agent -data-dir=/tmp -client 0.0.0.0

  4. 指定consulserver服务的VIP。 (Consul不会解决加入的名称 - 其他容器可能做得更好,允许指定服务名称" consulserver"而不是VIP)

    完成此操作后,任何其他服务都可以通过加入领事网络并解析名称" consiggent"来访问其他人。可以根据需要缩放(或者可以部署为全局服务)顾问服务。 发布端口8500使服务在群集边缘可用,如果您不需要将其提供给非群组服务,则可以将其删除。

答案 2 :(得分:3)

在我的blog中,我探索了与MarkH的答案类似的方法,但关键的区别在于,我指的是前三个加入了服务器的节点,而不是指向新服务器的VIP。网络。这可能是有益的,因为VIP存在问题,它将指向自身与在该VIP上的所有节点上的负载平衡。根据我的经验,最好以这种方式为服务创建。

docker service create \
  --network=consul \
  --name=consul \
  -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \ 
  -e CONSUL_BIND_INTERFACE='eth0' \
  --mode global \
  -p 8500:8500 \ 
  consul agent -server -ui -client=0.0.0.0 \
  -bootstrap-expect 3 \
  -retry-join 172.20.0.3 \
  -retry-join 172.20.0.4 \
  -retry-join 172.20.0.5 \
  -retry-interval 5s

我在3节点群中使用全局模式,因此您可以将其交换为副本并放置约束。

答案 3 :(得分:3)

对于那些喜欢从docker-compose.yml文件运行我们服务的人,我设法" docker stack deploy"

https://github.com/thechane/consul/blob/master/docker-compose.yml

...将Consul作为Docker服务运行。

---编辑,糟糕的形式只是回答链接,所以这里是:

version: '3.1'
#customise this with options from
#https://www.consul.io/docs/agent/options.html

services:

seed:
  hostname: seed
  image: consul:0.8.0
  deploy:
    restart_policy:
      condition: none  #we do not want this to be restarted on timeout (see entrypoint options below)
    replicas: 1
    placement:
      constraints:
        - "engine.labels.access == temp"
        - "engine.labels.access != consul"
  environment:
    - "CONSUL_LOCAL_CONFIG={\"disable_update_check\": true}"
    - "CONSUL_BIND_INTERFACE=eth0"
  entrypoint:
    - timeout     #this seed fires up the cluster after which it is no longer needed
    - -sTERM      #this is the same signal as docker would send on a scale down / stop
    - -t300       #terminate after 5 mins
    - consul
    - agent
    - -server
    - -bootstrap-expect=5
    - -data-dir=/tmp/consuldata
    - -bind={{ GetInterfaceIP "eth0" }}
  networks:
    - "consul"

cluster:
  image: consul:0.8.0
  depends_on:
    - "seed"
  deploy:
    mode: global                                      ##this will deploy to all nodes that
    placement:
      constraints:
        - "engine.labels.access == consul"            ##have the consul label
        - "engine.labels.access != temp"
  environment:
    - "CONSUL_LOCAL_CONFIG={\"disable_update_check\": true}"
    - "CONSUL_BIND_INTERFACE=eth0"
    - "CONSUL_HTTP_ADDR=0.0.0.0"
  entrypoint:
    - consul
    - agent
    - -server
    - -data-dir=/tmp/consuldata
    - -bind={{ GetInterfaceIP "eth0" }}
    - -client=0.0.0.0
    - -retry-join=seed:8301
    - -ui                                              ##assuming you want the UI on
  networks:
    - "consul"
  ports:
    - "8500:8500"
    - "8600:8600"

networks:
  consul:
    driver: overlay

另请注意,我后来发现,如果没有种子,则无法添加更多的consul实例。因此,如果您打算扩展您的swarm节点计数,我将从种子入口点删除带有其选项的timeout命令。

相关问题