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

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

在文档中有以下build议,但是使用这个参数我根本无法启动任何容器:

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 

我的第一个答案是关于“群模式”。 你会澄清,你使用旧版Swarm并添加更多信息,所以在这里:

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

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

环境variables constraint仅对Swarm的传统(独立)版本有效。 较新的“群模式”使用modeconstraints选项(不是环境variables)。

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

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

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

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

要强制执行其他基于主机名(节点)的约束,标签,angular色,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年:

别的东西。 docker-compose当前不支持“swarm模式”。 Swarm模式了解新的dab格式。 有一种方法可以将docker-compose文件转换为dab文件,但这是实验性的,不能在此处使用。 最好创build一个bash脚本,直接调用所有的docker service create ...

编辑2017年3月:

从docker1.13(17.03)开始,docker-compose现在可以用来直接设置swarm环境,而不必处理dab步骤。