docker-compose swarm:强制容器在特定主机上 运行

docker-compose swarm: force containers to run on specific hosts

尝试 运行 使用独立的 Swarm 和 docker-compose 版本“2”在不同的虚拟机上集群应用程序。覆盖网络已设置。但是想要强制某些容器在特定主机上 运行。

文档中有以下建议,但使用此参数我根本无法启动任何容器:

environment:
  - "constraint:node==node-1"

ERROR: for elasticsearch1  Cannot create container for service elasticsearch1: Unable to find a node that satisfies the following conditions
[available container slots]
[node==node-1]

我们应该将主机注册为 node-1 node-2...还是默认完成。

[root@ux-test14 ~]# docker node ls
Error response from daemon: 404 page not found
[root@ux-test14 ~]# docker run swarm list
[root@ux-test14 ~]#  

[root@ux-test14 ~]# docker info
Containers: 8
 Running: 6
 Paused: 0
 Stopped: 2
Images: 8
Server Version: swarm/1.2.5
Role: primary
Strategy: spread
Filters: health, port, containerslots, dependency, affinity, constraint
Nodes: 2
 ux-test16.rs: 10.212.212.2:2375
  â ID: JQPG:GKFF:KJZJ:AY3N:NHPZ:HD6J:SH36:KEZR:2SSH:XF65:YW3N:W4DG
  â Status: Healthy
  â Containers: 4 (4 Running, 0 Paused, 0 Stopped)
  â Reserved CPUs: 0 / 2
  â Reserved Memory: 0 B / 3.888 GiB
  â Labels: kernelversion=3.10.0-327.28.3.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=devicemapper
  â UpdatedAt: 2016-09-05T11:11:31Z
  â ServerVersion: 1.12.1
 ux-test17.rs: 10.212.212.3:2375
  â ID: Z27V:T5NU:QKSH:DLNK:JA4M:V7UX:XYGH:UIL6:WFQU:FB5U:J426:7XIR
  â Status: Healthy
  â Containers: 4 (2 Running, 0 Paused, 2 Stopped)
  â Reserved CPUs: 0 / 2
  â Reserved Memory: 0 B / 3.888 GiB
  â Labels: kernelversion=3.10.0-327.28.3.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=devicemapper
  â UpdatedAt: 2016-09-05T11:11:17Z
  â ServerVersion: 1.12.1
Plugins:
 Volume:
 Network:
Swarm:
 NodeID:
 Is Manager: false
 Node Address:
Security Options:
Kernel Version: 3.10.0-327.28.3.el7.x86_64
Operating System: linux
Architecture: amd64
CPUs: 4
Total Memory: 7.775 GiB
Name: 858ac2fdd225
Docker Root Dir:
Debug Mode (client): false
Debug Mode (server): false
WARNING: No kernel memory limit support

环境变量 constraint 仅对遗留(独立)版本的 Swarm 有效。较新的 "Swarm Mode" 使用 modeconstraints 选项(不是环境变量)。

要为每个节点强制执行一项且仅一项任务(容器),请使用 mode=global

docker service create --name proxy --mode global nginx

默认模式是replicated,这意味着群管理器将在所有可用节点上创建任务(容器)以满足--replicas选项中指定的数量。例如:

docker service create --name proxy --replicas 5 nginx

要根据主机名(节点)、标签、角色、id 实施其他约束,请使用 --constraint 选项。例如:

docker service create --name proxy --constraint "node.hostname!=node01" nginx

https://docs.docker.com/engine/reference/commandline/service_create/#/specify-service-constraints

2016 年 9 月编辑:

别的。 docker-compose 当前在 "swarm mode" 中不受支持。 Swarm 模式改为理解新的 dab 格式。有一种方法可以将 docker-compose 文件转换为 dab,但它是实验性的,目前不能依赖。最好创建一个直接调用所有 docker service create ... 的 bash 脚本。

2017 年 3 月编辑:

从 docker 1.13 (17.03) 开始,docker-compose 现在可用于直接配置 swarm 环境,而无需处理 dab 步骤。

我的第一个回答是关于"swarm mode"。您已经澄清您使用的是旧版 Swarm 并添加了更多信息,所以在这里:

您列出的约束假定您有一个名为 node-1 的主机。您的主机名为 ux-test16.rsux-test17.rs。只需在您的约束中使用它而不是 node-1 即可。例如:

environment:
  - "constraint:node==ux-test16.rs"

相关问题 - 我最近有一个 Swarm 项目,其中混合了工作节点 (3 x Linux + 4 x Windows)。我的容器需要在特定 OS 上 运行,但不需要在任何特定节点上。 Swarm 模式现在支持在 docker-compose 文件中的“约束”下指定 OS。无需为每个节点创建标签:

version: '3'  
services:  
    service_1:  
        restart: on-failure  
        image: 'service_1'  
        deploy:  
            placement:  
                constraints:  
                    - node.platform.os == windows  
    junittestsuite:  
        restart: on-failure  
        image: 'junit_test_suite:1.0'  
        command: ant test ...  
        deploy:  
            placement:  
                constraints:  
                    - node.platform.os == linux