Although Kubernetes is the first choice container orchestration platform, Docker Swarm can be a sufficient alternative for small and development environments. Besides basic scheduling it also provides more advanced features. Within this post I show how different Docker Swarm regions can be implemented.

Pursued Docker Swarm Setup

Our goal is to build a Docker Swarm which spans across two data centres in different world regions. Most of the time this is done in order to provide customers the best experience when accessing services a company provides. However, there are cases where it is not feasible or possible to spread the same services, versions or configurations across all regions. Reasons might be different user expectations due to cultural differences or legislation in different areas of the world.

For this post we want to create a Docker Swarm consisting of three manager nodes and one dedicated worker node. So overall our cluster consists of four Docker nodes. Within our scenario two of these nodes run in data center EAST and two in data center WEST.

Docker Swarm Setup

Now let us assume there are some legal requirements regarding our service in region WEST and we have to deploy an altered software version. How can we make sure that this version is deployed to data center WEST?

Configuring Docker Swarm Regions

First of all, we have to define that our Docker nodes H1 and H2 reside in data center WEST and nodes H3 and H4 in data center EAST. For this purpose Docker node labels can be used. Node labels are simple string or key-value combinations which can be applied to each node. To add labels each node has to be updated.

The resulting configuration can be checked by inspecting a node.

With this configuration we are able to point Docker to deploy services to specific Docker nodes. This can be achieved by adding constraints to service definitions. Constraints can check different Docker node characteristics such as labels or node roles. The listing below shows a service creation telling Docker Swarm to deploy this service only to nodes which have a key-value label region set to west. The constraint syntax allows equal (==) and not equal (!=) checks. Be aware that there are some resources which omit the prepending node.labels path in front of the constraint check. This did not work in my case, I had to use the fully qualified path node.labels.region.

After tasks got applied we can check our service instances.

As one can see only docker nodes having label region set to west run the service defined.

Further Reading

Docker Swarm Mode Overview
Docker Node Label Metadata
Docker Service Create Documentation
Docker Service Placement Constraints