简体   繁体   English

在docker swarm中的特定节点上运行docker容器

[英]Run a docker container on a specific node in a docker swarm

I have a jenkins pipeline where it build and runs various containers on a NUC server. 我有一个jenkins管道,它在NUC服务器上构建和运行各种容器。 This NUC is on a cluster (Swarm) with another NUC. 该NUC与另一个NUC在群集(群集)上。 I recently added a couple of raspberry Pis on my setup and on that cluster, so now I wonder if there is a way to command Jenknis to deploy on x86_x64 or armhf devices. 我最近在我的设置和该群集上添加了几个树莓派,所以现在我想知道是否存在一种命令Jenknis部署在x86_x64 or armhf设备上的方法。

I tried the -e constraint:node==<node_name> solution I found on other questions, but i had no luck. 我尝试了在其他问题上发现的-e constraint:node==<node_name>解决方案,但是我没有运气。

I tried the above command from one x86_x64 node pointing to another and from a x86_x64 node pointing to a armhf node 我尝试了上述命令从一个x86_x64节点指向另一个和从x86_x64指向节点armhf节点

I dont want to run those containers as a service and i dont care of any load balancer, I just want to run a container on a specific architecture ( x86_x64 or armhf depending on what i want to deploy) 我不想将那些容器作为服务运行, x86_x64 or armhf需要任何负载平衡器,我只想在特定体系结构( x86_x64 or armhf取决于我要部署的对象)上运行容器

You cannot use constraints on containers, only on services. 您不能仅在服务上对容器使用约束。

That being said, using constraints on services seems a good way to go. 话虽这么说,对服务使用约束似乎是一个不错的方法。 You can learn more about services constraints in the official documentation . 您可以在官方文档中了解有关服务约束的更多信息。 Here are a few example on how constraints can match node or Docker Engine labels: 以下是有关约束如何匹配节点或Docker Engine标签的一些示例:

# Node ID
node.id==2ivku8v2gvtg4

# Node hostname
node.hostname!=node-2

# Node role
node.role==manager

# Node labels
node.labels.security==high

# Docker Engine's labels
engine.labels.operatingsystem==ubuntu 14.04

If you want to match a specific hostname you need to use node.hostname==<hostname> and not node==<hostname> 如果要匹配特定的主机名,则需要使用node.hostname==<hostname>而不要使用node==<hostname>

You will also want to update the restart_policy key from your service definition deploy policy to prevent from starting a new container once the first one terminates its process successfully. 您还需要从服务定义deploy策略中更新restart_policy项,以防止第一个容器成功终止后启动新容器。

Wrapping it all together you need something like that: 将它们包装在一起,您需要这样的东西:

version: "3.7"
services:
  myapp:
    image: <my_image>
    deploy:
      placement:
        constraints:
          - node.labels.arch == x86_64
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s

Of course, it is up to you to add the labels to each Swarm node. 当然,由您决定是否将标签添加到每个Swarm节点。 Ansible and its module docker-node are really well suited for this purpose. Ansible及其模块docker -node确实非常适合此目的。 Here is an example playbook: 这是一个示例剧本:

- hosts: swarm
  tasks:
    - name: Add label to node specifying architecture 
      docker_node:
        hostname: "{{ inventory_hostname }}"
        labels:
          arch: "{{ ansible_architecture }}"

Your docker nodes would be labelled with the key arch and one of the following values: 您的docker节点将被标记为关键arch和以下值之一:

  • armhf armhf
  • armv7l armv7l
  • i386 I386
  • x86_64 x86_64的

Why don't you want to use a service? 您为什么不想使用服务? Services is how we orchestrate things in Swarm. 服务是我们编排Swarm中事物的方式。 docker container ... will only start/stop/inspect/manipulate things in on a single host. docker container ...将仅在单个主机上启动/停止/检查/操纵事物。 As you already have a swarm cluster set up, why not use a service? 由于已经建立了群集集群,为什么不使用服务?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM