Docker Swarm cluster

Motivation

I have wanted to setup my own cluster of VM-s to run my home lab stuff, including this WordPress server as well.

Prior to this I have used only one single beefy Hetzner Cloud VM but I wanted to spice some things up, why not just use a farm of many low cost VM-s which can serve as hosts for docker containers.

First idea was to setup the small custom Kubernetes cluster (k3 anyone??), but this seemed like a overkill, so I naturally pivoted to the Docker Swarm cluster. It’s easy to setup, it works great and its free and open-source, what’s not to like here!?


Docker Swarm
In the following picture you can see the architecture of typical Docker Swarm cluster setup:

Manager nodes handle cluster management tasks such as scheduling, orchestrating services, and maintaining cluster state. Worker nodes, on the other hand, execute the tasks assigned to them by the manager nodes.

The distributed state store is a key component of Docker Swarm that stores the cluster state, including information about services, tasks, and nodes. This state store ensures that all manager nodes have consistent and up-to-date information about the cluster. By using a distributed state store, Docker Swarm provides high availability and fault tolerance, as the state can be replicated across multiple nodes, ensuring that the cluster can continue to operate even if some nodes fail.

Implementation

Since this is all a cheap and low footprint personal setup to mess around, I have decided to only run 1 Manager node and couple of VM-s as Worker nodes. By default the Manager nodes can also host containers (good practice to turn this off in production)

The following picture shows my architecture on my Hetzner Cloud project:

The picture is very clear, we have setup the LAN on Hetzner Cloud and spin up a couple of VM-s, they all have their public/private IPv4 addresses and share the same firewall rules (I’ll probably implement a Bastion host and remove the public IPv4-s in future). All VM-s have Docker installed and they are connected to the same Docker Swarm cluster, the root VM acts as Manager Node and rest of them are Worker nodes.

Since I will be running the containers which will need to persist their data, I needed some kind of NFS to act as universal storage for these volumes, we don’t want to have container redeployed from node-1 to node-2 and then losing that data because its stored at the local host.

I managed to implement this by attaching a volume as a disk on one “root” VM aka the Manager node, installing the filesystem on it and then mounting it on other nodes in cluster via sshfs.

This was needed because of Hetzner limitations, sadly volume can be attached to only ONE VM.

Don’t forget to use this filesystem as docker volume :D.

More info about this project on:
https://github.com/ptisma/docker-swarm-cluster