Docker Swarm Service

How to place containers within a Docker Swarm deployment.

Use Cases

Constraints can be helpful to maintain a cluster environment for various needs.

  • Maintain resource hungry services to avoid a deployment to similar cluster nodes. Therefore to avoid an overlap of resource demanding services.
  • Services which cannot be moved around to random hosts within a cluster (i.e. due to underlying IP etc)
  • Deployment of services to a specific group of hosts based on infrastructure needs (i.e. certain volume assignments, memory, CPU etc)

Node constraints

When a swarm cluster is created and starts to provide services those will be started on any available node with a given cluster.

Usage of constraints will help to label/tag nodes within a Docker Swarm cluster in a way that it allows to place a given subset of services to a certain group of cluster nodes.

Nodes may be added with a single label/tag to deploy services efficiently. Various services otherwise though can be deployed by utilizing the same constraint. Therefore it would be a 1:N relation.

A label is independent to the cluster node’s role. A worker as well as a leader/manager node can set with similar labels.

Setting constraints in a cluster

Generate a list of node IDs with docker node ls

b4y5fxmnqaw6za652bgrwg413 *   host1  Ready               Active              Reachable           18.06.1-ce
v6brbtejckon3nja78h66wlu0     host2   Ready               Active              Leader              18.06.1-ce

A label then can be assigned just easily by the docker CLI

  docker node update –label-add example1=true b4y5fxmnqaw6za652bgrwg413
  docker node update –label-add example2=true v6brbtejckon3nja78h66wlu0

Verification on the set labels can be checked by a ‘docker inspect’ for a given cluster host.

Service deployment with constraints set

docker service create –constraint node.labels.example1==true –replicas 2 –name service1 imageName
docker service create –constraint node.labels.example2==true –replicas 1 –name service2 imageName

A ‘docker service ps service1’ then shows the cluster nodes where ‘service1’ is deployed to. Nodes which were not labeled as ‘example1’ will not show service1 as a running service

Join the Conversation

1 Comment

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: