Docker swarm — How to use node labels

Node labels can be a powerful tool, a label is nothing more than a metadata of the node, imagine that we have two swarm nodes, one in a…

Node labels can be a powerful tool, a label is nothing more than a metadata of the node, imagine that we have two swarm nodes, one in a data-center named east and one in a data-center named west, lets label them based on the data-center

On the swarm manager enter the following for the node on Data-center east$ docker node update --label-add DC=east worker_node2

Add a label for the node on data center west also$ docker node update --label-add DC=west worker_node3

Suppose that the one Data center, named east is our production data center and the west is the failover data center, so lets start our services on data center east$ docker service create --name nginx-east --constraint node.labels.DC==east --replicas 3 nginx
m1qq0ztimybnivgu75yo9ci80
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged

Lets do the same for west$ docker service create --name nginx-west --constraint node.labels.DC==west --replicas 3 nginx
od3fmka6nvagrieptmxxjjmr4
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged

verify that both services have started$ docker service list
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
jle0zw7b51sg        nginx-east          replicated          3/3                 nginx:latest        
od3fmka6nvag        nginx-west          replicated          3/3                 nginx:latest

Verify that node constraints work as it should$ docker service ps nginx-west
ID                  NAME                IMAGE               NODE                                     DESIRED STATE       CURRENT STATE           ERROR               PORTS
yvjjwdgfbl10        nginx-west.1        nginx:latest        worker_node3  Running             Running 3 minutes ago                      
8tgqmjaqo1nj        nginx-west.2        nginx:latest        worker_node3  Running             Running 3 minutes ago                      
yzu2cl5mr36b        nginx-west.3        nginx:latest        worker_node3  Running             Running 3 minutes ago

And$ docker service ps nginx-east
ID                  NAME                IMAGE               NODE                                     DESIRED STATE       CURRENT STATE                ERROR               PORTS
cspu9krofwti        nginx-east.1        nginx:latest        worker_node2  Running             Running about a minute ago                      
prk2a3eimdjy        nginx-east.2        nginx:latest         worker_node2  Running             Running about a minute ago                      
o41geveazrdj        nginx-east.3        nginx:latest        worker_node2  Running             Running about a minute ago

Now, lets delete the services$ docker service rm nginx-east
$ docker service rm nginx-west

A common scenario is to start services evenly on all nodes, for this purpose there is the placement-pref parameter which allows to spread running services across all nodes of the swarm based on label values$ docker service create --name nginx-spread --placement-pref spread=node.labels.DC --replicas 6 nginx
783huvqwwojprs1ht26pxfj1b
overall progress: 6 out of 6 tasks
1/6: running   [==================================================>]
2/6: running   [==================================================>]
3/6: running   [==================================================>]
4/6: running   [==================================================>]
5/6: running   [==================================================>]
6/6: running   [==================================================>]
verify: Service converged

Doing this since the DC label has two values the services should be spread with services_number / 2, to verify this$ docker service ps nginx-spread
ID                  NAME                IMAGE               NODE                                     DESIRED STATE       CURRENT STATE           ERROR               PORTS
lljbufq3cf45        nginx-spread.1      nginx:latest        worker_node2  Running             Running 2 minutes ago                      
orwrewax6tib        nginx-spread.2      nginx:latest        worker_node3 Running             Running 2 minutes ago                      
ytbdrx7b7h32        nginx-spread.3      nginx:latest        worker_node2  Running             Running 2 minutes ago                      
na6qmbcskum0        nginx-spread.4      nginx:latest        worker_node3  Running             Running 2 minutes ago                      
w1ls5ail8tnm        nginx-spread.5      nginx:latest        worker_node2  Running             Running 2 minutes ago                      
rur1h80p6eo7        nginx-spread.6      nginx:latest        worker_node3  Running             Running 2 minutes ago

I hope you found the article interesting! :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…