Docker swarm allows us to have multiple docker machines to avoid the single point of failure we have when running one machine with all our containers on it.
Docker swarm also load-balances different services on the different swarm hosts to have a high availability. We can for example run different database applications on different nodes to balance out the load between all the swarm nodes.
Setting up the swarm:
We need to first set up the Swarm manager using the below command:
Then, we run the output of the above command on the other nodes to join them to the swarm cluster as worker nodes:
The manager node:
The manager node is responsible for managing the cluster, adding, removing worker nodes and making sure that the containers are running without any issues across the docker swarm cluster.
Usually we would have more than one manager node in the cluster with one (manager) leader.
The leader periodically updates the other managers to avoid data inconsistencies among the managers.
The leader (manager) is chosen using a raft algorithm.
We set up the swarm manager using the below command:
On the worker node we run the below command:
We can display all the nodes by running the below command:
Creating an overlay network that will span both nodes:
An overlay network is a network that spans all the docker nodes making it possible for containers on different nodes to communicate with each other.
To create an overlay network we use the below command:
-d : indicates the type of network driver we are using.
We check the creation of the overlay network using:
Creating containers and attaching them to our overlay network:
The above command creates a service that deploys three "debian" containers and attaches them to the overlay network "overlay_1".
We can check our running containers (three replicas )using the below command: