Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker placement preference vs Docker placement constraints

I am learning Docker. As part of my learning there is one topic I haven't understood. The usage of the Docker placement preferences when compared with constraints in Docker.

I have been keep searching in internet and asked many people everyone aware of the constraints not the preferences. I haven't found any good reference in internet. What is the use of placement preferences in Docker? When should I use them? Any sample example?

like image 558
Madhu Avatar asked Oct 28 '25 01:10

Madhu


2 Answers

Placement prefs allow you to spread your replicas across nodes with certain tags, choosing to put the replicas as much diverse as possible.

Constraints will constrain (limit) the replicas to those nodes that match the constraint.

Imagine you have 4 servers in 2 datacenters (east, west) and some of the servers have an SSD drive, while others don't.

If you apply the following labels to the nodes:

node-1: datacenter=us-east, disk=ssd
node-2: datacenter=us-east, disk=ssd
node-3: datacenter=us-west, disk=sas
node-4: datacenter=us-west, disk=nl-sas

If you would deploy a service with 2 replicas and you want to provide geographic redundancy, you would start that service as follows:

docker service create \
  --replicas 2 \
  --name webserver \
  --placement-pref 'spread=node.labels.datacenter' \
  mywebservice:production

Docker Swarm would then try to "spread" the 2 replicas across both datacenters, so you'll end up with 1 replica on either node-1 or node-2, and the other replica on node-3 or node-4.

If you have a service (eg a reporting job) that does a lot of IOPS, you'll probably want to use your SSD equipped servers, so you'll place a constraint on the service:

docker service create \
  --constraint 'node.labels.disk == ssd' \
  myreporter:latest
like image 103
Frank Louwers Avatar answered Oct 30 '25 21:10

Frank Louwers


Refer to "Control service placement" section in https://docs.docker.com/engine/swarm/services/

In short Placement constraint limits where a task can run (a task will not run on a node unless it satisfies the constraint, it will remain in pending state)

While placement constraints limit the nodes a service can run on, placement preferences try to place tasks on appropriate nodes in an algorithmic way (currently, only spread evenly). Placement preference helps you in distributing the tasks based on a constraint across the nodes.

e.g. placement constraint of node.region=east will let a task only run on nodes labelled "east" whereas, placement preference of node.region=east will help you spread the tasks across nodes evenly based node.region. If any node does not have this label, it will still get the task.

like image 36
Ashish C Avatar answered Oct 30 '25 22:10

Ashish C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!