Skip to main content

Docker Swarm

Docker Swarm is a container orchestration tool. It is a cluster management tool that manages and scales virtual servers as a cluster and ensures the continuity of services without interruption. In this post, I will explain how to run Ant Media Server onto the docker swarm.


First, let’s create a total of 3 instances, one Manager and 2 worker nodes. Manager Node1 Node2

Docker Swarm is easy to install. You can divide it into two parts as Manager Node and Worker Node.

1- Manager: The node that has the manager role manages the docker swarm.

2- The nodes that have a worker role are generally responsible for running the services.

Install the Docker CE on all of the nodes by following the steps below.

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] focal stable"
sudo apt update && sudo apt install docker-ce -y
sudo systemctl enable docker

Docker Swarm Cluster Installation

In order to create a Docker Swarm cluster, you have to start the swarm mode first.

Run the command below to initialize Docker swarm node on the manager.

sudo docker swarm init --advertise-addr

Run the following command on node1 and node2.

sudo docker swarm join --token SWMTKN-1-2jxta71638d1pyioznb9jo4hi4u5ppd8t7lc90linwi9acu54s-aef4mqdy23ktrkcxsp57uyoma

The all nodes you added will show up in the docker node ls command’s output.

Nginx Load Balancer Installation

Create a directory called /opt/nginx on all nodes and save the following lines as default.conf.

mkdir /opt/nginx
vim /opt/nginx/default.conf
server {
listen 80;
location / {
proxy_pass http://backend;
upstream backend {
server; #node1 ip address
server; #node2 ip address

Let’s complete the deployment on master.

docker service create --name nginx --mount type=bind,source=/opt/nginx/,target=/etc/nginx/conf.d --constraint node.hostname==master  --publish 80:80 nginx

Ant Media Server Installation

On the master node, save the following lines as stack.yml. Don't forget to change the host addresses in the image and entrypoint according to your system.

version: "3.9"
image: your_image_url
entrypoint: /usr/local/antmedia/ -r true -m cluster -h your_mongo_db_address
mode: global
cpus: "0.5"
memory: 1G
condition: on-failure
- host

name: host
external: true

Now, deploy the stack by running the command below.

docker stack deploy -c stack.yml ant-media-server

You can control the running containers with the docker ps command.

Now, you can access your cluster via the master URL.