HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How do I prevent service A from being placed in the same node as service B in Docker Swarm?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
swarmpreventthesamedockernodebeingservicehowplaced

Problem

From the docs, I could only find information about placement restrictions based on node attributes, likes labels and its role. However, I couldn't find how to configure placement based on the services already running on a given node.

How can I do that?

Solution

At present the answer is "no", swarm mode does not support affinity or anti-affinity between a service and other containers/services running on a node. However, depending on your goal there are other options.

One option is to reserve resources like CPU and memory for your container. This will ensure two resource intensive applications do not schedule themselves on the same node if there are insufficient resources.

The other option, as you've mentioned, is to use labels and constraints. You can give the same label to multiple nodes, which is often done to specify a type of node, or assign nodes to a specific team/project/environment.

Both of these options will reduce your fault tolerance, particularly if you assign a label to one or a small handful of nodes. If those nodes with the label go down, docker will be unable to reschedule your service on another node.

Context

StackExchange DevOps Q#5141, answer score: 2

Revisions (0)

No revisions yet.