Setting Up Docker Swarm on Oracle Cloud Infrastructure

Setting Up Docker Swarm on Oracle Cloud Infrastructure

Hello there!

Recently, I leveraged Oracle Cloud Infrastructure’s (OCI) free tier to test out Docker Swarm clusters. While I found plenty of guides on Kubernetes, I couldn't find much tailored specifically to Docker Swarm. So, in this article, I’ll walk you through how I deployed a Docker Swarm cluster on OCI using their always-free services.

In my previous article, I discussed the free resources available on OCI, which include ARM based VMs. This time, I’ll focus on the practical steps to set up a Docker Swarm cluster using four virtual machines, all within the free tier. I’ll use 1 VM as the manager and the remaining 3 VMs as worker nodes. Initially, I thought about using OCI’s “Cluster” or “Stack” sections, but after some experimentation, I found that manually creating individual nodes was simpler and more efficient.

Many people choose Kubernetes for managing container clusters, but my interest lies in Docker Swarm due to its straightforwardness.

Setting Up the VMs

Creating Virtual Machines (VMs) on OCI is easy. For this setup, I used the ARM-based VM.Standard.A1.Flex shape, which is part of the always-free tier.

OCI defaults to Oracle Linux, which is based on CentOS. Personally, I prefer working with Ubuntu or Debian-based systems because I’m more familiar with apt. However, since Oracle Linux is the default, I proceeded with it and installed Docker using the following commands:

sudo yum update -y

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker $USER

Initializing Docker Swarm

After Docker was installed and running, I initialized the Docker Swarm manager node. This step involves advertising the private IP address of your VM to other nodes in the Swarm. Here’s the command I used, replacing YOUR_PRIVATE_IP with the actual private IP of the VM:

sudo docker swarm init --advertise-addr YOUR_PRIVATE_IP

This sets up the current VM as the manager node. Now, to add additional worker nodes to the Swarm, you need a special join command generated by the manager node. This includes a token and the manager’s private IP address.

On the manager node’s terminal, you’ll get something like this:

sudo docker swarm join --token SWARM_JOIN_TOKEN MANAGER_PRIVATE_IP:2377

Run this command on the worker nodes to join them to the Swarm cluster.

Networking Considerations

Once the nodes were connected, I tested communication between them using simple ping commands.

On the manager node:

ping VM2_private_IP

And on the worker node:

ping VM1_private_IP

Both nodes could communicate, but Docker Swarm still wasn’t working properly. After some digging, I found that Docker Swarm requires several ports to be open for communication between nodes. Specifically, the following ports are needed:

  • 2377/TCP for cluster management communication
  • 7946/TCP and UDP for node-to-node communication
  • 4789/UDP for overlay network traffic

These ports can be added as Ingress rules under the Networking and Virtual Cloud Networks (VCN) sections in OCI. It’s important to note that these rules apply to the private network and are not exposed to the internet.

To add the firewall rules directly on the VMs, use the following commands:

sudo firewall-cmd --add-port=2377/tcp --permanent
sudo firewall-cmd --add-port=7946/tcp --permanent
sudo firewall-cmd --add-port=7946/udp --permanent
sudo firewall-cmd --add-port=4789/udp --permanent
sudo firewall-cmd --reload

This ensures the necessary ports are open for Docker Swarm to function.

Final Steps

Once the firewall rules were set, I re-ran the docker swarm join command on the worker nodes, and everything worked perfectly. The Swarm nodes were able to communicate and form the cluster successfully.

And that’s it! You now have a functioning Docker Swarm cluster running on OCI’s free tier.

Read more