1

Create a Docker Swarm cluster using Ansible

Introduction

Docker Swarm is a group of docker installed Virtual or physical machines that configured to join together in a cluster. Once a group of machines has been clustered together it will provide high availability to the docker containers running inside.

Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.

One of the key benefits associated with the operation of a docker swarm is the high level of availability offered for applications. In a docker swarm, there are typically several worker nodes and at least one manager node that is responsible for handling the worker nodes’ resources efficiently and ensuring that the cluster operates efficiently.

In this article, we will go through how to install and configure the 3 node Docker Swarm cluster with Ubuntu 18.04 server.

Docker Swarm is made up of :

Manager Node

The primary function of manager nodes is to assign tasks to worker nodes in the swarm. Manager nodes also help to carry out some of the managerial tasks needed to operate the swarm. Docker recommends a maximum of seven manager nodes for a swarm. One of the most important features of Docker in Swarm Mode is the manager quorum. The manager quorum stores information about the cluster and the consistency of information is achieved through consensus via the Raft consensus algorithm. If any Manager node dies unexpectedly, another one can pick up the tasks and restore the services to a stable state. Raft tolerates up to (N-1)/2 failures and requires a majority or quorum of (N/2)+1 members to agree on values proposed to the cluster. This means that in a cluster of 5 Managers running Raft if 3 nodes are unavailable, the system cannot process any more requests to schedule additional tasks. The existing tasks keep running but the scheduler cannot rebalance tasks to cope with failures if the manager set is not healthy.

Leader Node

When a cluster is established, the Raft consensus algorithm is used to assign one of them as the “leader node”. The leader node makes all of the swarm management and task orchestration decisions for the swarm. If the leader node becomes unavailable due to an outage or failure, a new leader node can be elected using the Raft consensus algorithm.

Worker Node

In a docker swarm with numerous hosts, each worker node functions by receiving and executing the tasks that are allocated to it by manager nodes. By default, all manager modes are also worker nodes and are capable of executing tasks when they have the resources available to do so.

Prerequisites

  • 3 Ubuntu 18.04 machines
  • user with sudo access
  • Make sure you have configured static IP on each node.
  • Make sure ansible server can access the nodes.

Step 1

Create an inventory file called hosts with docker nodes IP and specify docker swarm manager node IP and worker node IP

I am configuring all 3 nodes as master so I can maintain the quorum if one the node fails

[docker-swarm-managers]
10.10.10.10
10.10.10.11
10.10.10.12
[docker-swarm]
10.10.10.10
10.10.10.11
10.10.10.12

Step 2

Download my docker-swarm GitHub repository to your ansible role location.

Copy docker-swarm.yml to your playbook location and run the playbook

Step 3

$ ansible-playbook -i hosts docker-swarm.yml

This will install all the necessary packages and create a swarm cluster. Once the installation and configuration is completed, verify the docker swarm cluster node status by running

$ docker node ls

Deploy service to docker swarm by running

$ docker service create --replicas 1 --name helloworld alpine ping docker.com

Run docker service ls to see the list of running services:

ID            NAME        SCALE  IMAGE   COMMAND
9uk4639qpg7n  helloworld  1/1    alpine  ping docker.com

Conclusion

We have successfully set up a swarm cluster using ansible. Deploy services.

Please let me know your feedbacks/suggestions.

Check out these books from amazon more information.


One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *