Placement Preferences
- 17.04+ feature for a best effort "soft requirement"
- Only one strategy for now, "spread"
- Spreads tasks among all values of a Label
- Good to ensure distribution across availability zones, datacenters, racks, subnets, etc.
- Works on service create and update
- Can add multiple preferences for multi-layer placement control
- Won't move service tasks if labels change
- Use with Constraints if labels aren't on all nodes
- "missing label is the same as having the label with a null value"
Examples
- Label all your AWS nodes with availability zone
docker node update --label-add=azone=1 nodeX
docker node update --label-add=azone=2 nodeY
docker node update --label-add=azone=3 nodeX
- Make sure your service is deployed to all availability zones
docker service create --placement-pref spread=node.labels.azone --replicas 3 nginx
Hands On
[node1] ~> docker node update --label-add azone=1 manager1
manager1
[node1] ~> docker node update --label-add azone=2 manager2
manager2
[node1] ~> docker node update --label-add azone=2 worker1
worker1
[node1] ~> docker service create --placement-pref spread=node.labels.azone --replicas 2 --name webapp1 nginx
mi0rgp8uylpweml8m1rbh347z
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
[node1] ~> docker service create --placement-pref spread=node.labels.azone --replicas 2 --name webapp2 nginx
pvjulrsmouklw47lqlo4a5wg6
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
[node1] ~> docker service update --placement-pref-rm spread=node.labels.azone webapp1
webapp1
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
[node1] ~> docker service scale webapp2=8
webapp2 scaled to 8
overall progress: 8 out of 8 tasks
1/8: running [==================================================>]
2/8: running [==================================================>]
3/8: running [==================================================>]
4/8: running [==================================================>]
5/8: running [==================================================>]
6/8: running [==================================================>]
7/8: running [==================================================>]
8/8: running [==================================================>]
verify: Service converged
[node1] ~> docker service update --constraint-add node.role==worker webapp2
webapp2
overall progress: 8 out of 8 tasks
1/8: running [==================================================>]
2/8: running [==================================================>]
3/8: running [==================================================>]
4/8: running [==================================================>]
5/8: running [==================================================>]
6/8: running [==================================================>]
7/8: running [==================================================>]
8/8: running [==================================================>]
verify: Service converged
Placement Preference in Stack Files
version: "3.1"
services:
web:
image: nginx
deploy:
placement:
preferences:
spread: node.labels.azone
External links