Docker Swarm (Docker-Machine) in Quick Notes
Docker Swarm (Docker-Machine) in Quick Notes
Table of Contents
Tutorial Objectives...............................................................................................................................1
A. Introductions...............................................................................................................................2
A. 1. Manager nodes...................................................................................................................3
A. 2. Worker nodes......................................................................................................................3
A. 3. Swarm Feature highlights..................................................................................................3
A. 4. Docker Swarm vs Kubernetes............................................................................................5
Kubernetes..............................................................................................................................6
Docker Swarm........................................................................................................................6
B. Install Docker + Docker-machine (swarm) :...............................................................................9
C. Swarm Design for Objectives:....................................................................................................9
D. Initialize swarm:........................................................................................................................10
E. Create Service to swarm:...........................................................................................................11
1. Create Nginx service at manager1........................................................................................11
2. Create MariaDB galera at docker Swarm :...........................................................................12
3. Setup Minio service:.............................................................................................................13
Important command............................................................................................................................15
Other References................................................................................................................................15
Tutorial Objectives
1. Understand how to install, setup and configure/administrate Docker container and Docker
Swarm.
2. Install and configure following container inside Docker Swarm :
• Nginx
• MariaDB Galera Cluster
• Minio
1
A. Introductions
Docker Docs:
• https://docs.docker.com/machine/overview/
• https://docs.docker.com/engine/swarm/
• https://docs.docker.com/get-started/part4/
A swarm is a group of machines that are running Docker and joined into a cluster. After that has
happened, you continue to run the Docker commands you’re used to, but now they are executed on
a cluster by a swarm manager. The machines in a swarm can be physical or virtual. After joining a
swarm, they are referred to as nodes.
● Manager Nodes: Tasks involved here include Control Orchestration, Cluster Management, and
Task Distribution.
● Worker Nodes: Functions here include running containers and services that have been assigned
by the manager node.
● Services: This gives a description of the blueprint via which an individual container can distribute
itself across the nodes.
● Tasks: These are slots in which single containers place their work.
There are two types of nodes: managers and workers. A swarm consists of one or more nodes:
physical or virtual machines running Docker Engine.
2
A. 1. Manager nodes
Manager nodes handle cluster management tasks:
A. 2. Worker nodes
Worker nodes are also instances of Docker Engine whose sole purpose is to execute containers.
Worker nodes don’t participate in the Raft distributed state, make scheduling decisions, or serve the
swarm mode HTTP API.
You can create a swarm of one manager node, but you cannot have a worker node without at least
one manager node. By default, all managers are also workers. In a single manager node cluster, you
can run commands like $ docker service create and the scheduler places all tasks on the
local Engine.
You can promote a worker node to be a manager by running $ docker node promote
3
• Decentralized design: Docker Engine handles any specialization at runtime from a single
disk image to create multiple nodes.
• Declarative service model: Docker Engine uses a declarative approach to let you define the
desired state of the various services in your application stack.
• Scaling: For each service, you can declare the number of tasks you want to run.
• Desired state reconciliation: The swarm manager node constantly monitors the cluster state
and reconciles any differences between the actual state and your expressed desired state.
• Multi-host networking: You can specify an overlay network for your services.
• Service discovery: Swarm manager nodes assign each service in the swarm a unique DNS
name and load balances running containers.
• Load balancing: You can expose the ports for services to an external load balancer.
Internally, the swarm lets you specify how to distribute service containers between nodes.
4
• Secure by default: Each node in the swarm enforces TLS mutual authentication and
encryption to secure communications between itself and all other nodes.
• Rolling updates: The swarm manager lets you control the delay between service
deployment to different sets of nodes.
Figure 4:
https://technologyadvice.com/blog/information-technology/kubernetes-vs-
docker/
5
A. 4. Docker Swarm vs Kubernetes
Reff: https://thenewstack.io/kubernetes-vs-docker-swarm-whats-the-difference/
Kubernetes
Kubernetes is an open-source platform created by Google for container deployment operations,
scaling up and down, and automation across the clusters of hosts. This production-ready, enterprise-
grade, self-healing (auto-scaling, auto-replication, auto-restart, auto-placement) platform is
modular, and so it can be utilized for any architecture deployment.
Kubernetes also distributes the load amongst containers. It aims to relieve the tools and components
from the problem faced due to running applications in private and public clouds by placing the
containers into groups and naming them as logical units. Their power lies in easy scaling,
environment agnostic portability, and flexible growth.
6
Figure 6: Docker Swarm
Docker Swarm
As a platform, Docker has revolutionized the manner software was packaged. Docker Swarm or
simply Swarm is an open-source container orchestration platform and is the native clustering engine
for and by Docker. Any software, services, or tools that run with Docker containers run equally well
in Swarm. Also, Swarm utilizes the same command line from Docker.
Swarm turns a pool of Docker hosts into a virtual, single host. Swarm is especially useful for people
who are trying to get comfortable with an orchestrated environment or who need to adhere to a
simple deployment technique but also have more just one cloud environment or one particular
platform to run this on.
---------------
7
Reff: https://blog.docker.com/2016/03/swarmweek-docker-swarm-exceeds-kubernetes-scale/
Docker Swarm allows organizations to leverage the full power of the native Docker CLI and APIs.
It allows developers to work in a consistent way, regardless of where their applications are
developed or where they will run. Also, Swarm smooths your transition to different providers.
8
Ref: https://hackernoon.com/kubernetes-vs-docker-swarm-a-comprehensive-comparison-
73058543771e
By using Kubernetes, you will be able to define how your applications should run and the manner in
which they interact with other applications. Empowering the user with interfaces and composable
platform primitives, Kubernetes allows high degrees of flexibility and reliability.
● Simple and powerful user experience through the Docker Universal Control Plane single interface
Swarm is resilient to failures and the swarm can recover from any number of temporary node
failures (machine reboots or crash with restart) or other transient errors. However, a swarm cannot
9
automatically recover if it loses a quorum (Majority). Tasks on existing worker nodes continue to
run, but administrative tasks are not possible, including scaling or updating services and joining or
removing nodes from the swarm. The best way to recover is to bring the missing manager nodes
back online. (Source: Recover from loosing a quorum)
Having the same consistent state across the cluster means that in case of a failure, any Manager
node can pick up the tasks and restore the services to a stable state. For example, if the Leader
Manager which is responsible for scheduling tasks in the cluster dies unexpectedly, any other
Manager can pick up the task of scheduling and re-balance tasks to match the desired state.
● Backward compatibility with existing components (highly relevant for existing users)
Every Engine release strives to be backward compatible with its predecessors, and interface stability
is always a priority at Docker. (https://docs.docker.com/engine/breaking_changes/)
Since every node in swarm are full docker machine, Compatibility is highly relevant.
10
B. Install Docker + Docker-machine (swarm) :
1. Install Docker
https://docs.docker.com/install/linux/docker-ce/ubuntu/ (Ubuntu)
And Post script install:
https://docs.docker.com/install/linux/linux-postinstall/
3. Install docker-machine
1. https://docs.docker.com/machine/install-machine/
Worker1
* Minio Container
Manager 1 * Galera Node
* Nginx service
* Minio Server
* MariaDB Galera Seeder
Worker2
* Minio Container
* Galera Node
Manager 1 role as swarm Leader.
D. Initialize swarm:
11
$ docker swarm init --advertise-addr [IP manager] --listen-addr
[network_name local]:2377
Fast-script:
docker-machine ssh manager1 "docker swarm init --listen-addr $(docker-
machine ip manager1) --advertise-addr $(docker-machine ip
manager1)"
Note: 1 Swarm must have at least 1 managers & >= 0 workers, and 1 swarm must have 1 leader.
Join manager to swarm using leader $manager_token.
Join worker to swarm using leader $worker_token.
Worker1:
docker-machine ssh worker1 \
"docker swarm join \
--token $worker_token \
--listen-addr $(docker-machine ip worker1) \
--advertise-addr $(docker-machine ip worker1) \
$(docker-machine ip manager1):2377"
Worker2:
docker-machine ssh worker2 \
"docker swarm join \
--token $worker_token \
--listen-addr $(docker-machine ip worker2) \
--advertise-addr $(docker-machine ip worker2) \
$(docker-machine ip manager1):2377"
12
5. Link host machine with manager1
[host machine]$ eval $(docker-machine env manager1)
Note: You can remote internally using normal ssh by using, for modifying the Nginx
$ docker-machine ssh [Node Name]
Expanding to another nodes is optional.
13
• Synchronous replication
• Active-active multi-master topology
• Read and write to any cluster node
• Automatic membership control, failed nodes drop from the cluster
• Automatic node joining
• True parallel replication, on row level
• Direct client connections, native MariaDB look & feel
Step: https://mariadb.com/kb/en/library/getting-started-with-mariadb-galera-cluster/
Docker Hub: https://hub.docker.com/r/colinmollenhour/mariadb-galera-swarm
Notes:
Seed : Used only to initialize a new cluster and after initialization and other nodes are joined the
"seed" container should be stopped and replaced with a "node" container using the same volume.
Node : Join an existing cluster. Takes as a second argument a comma-separated list of IPs or
hostnames to resolve which are used to build the --wsrep_cluster_address option for joining a
cluster.
14
In mariaDB galera Cluster, every node is seeder (has information to other nodes).
Note: docker note can be used for configure allocation of which service will be assigned. Followed
echo [key] is default key configured in this tutorial.
15
Data protection
Distributed Minio provides protection against multiple node/drive failures and bit rot using erasure
code. As the minimum disks required for distributed Minio is 4 (same as minimum disks required
for erasure coding), erasure code automatically kicks in as you launch distributed Minio.
16
Important command
$ docker node ls # Swarm node status
$ docker-machine ls # List all docker swarm (manager and worker)
$ docker-machine ssh [Node Name] “[command]”
#Execute [command] inside [Node Name]
$ docker service ls # List active service
$ docker ps # List running container
$ docker ps -a # List all container
$ docker stack ls # List SWARM service / stack
$ docker node ps [node] # List serivce in selected node
$ docker secret --help
Other References
https://rominirani.com/docker-swarm-tutorial-b67470cf8872
https://medium.com/@Grigorkh/docker-swarm-tutorial-c5d5cf4b4de
https://semaphoreci.com/community/tutorials/running-applications-on-a-docker-swarm-mode-
cluster
https://thenewstack.io/kubernetes-vs-docker-swarm-whats-the-difference/
https://blog.alexellis.io/openfaas-storage-for-your-functions/
https://18pct.com/zero-to-mariadb-cluster-in-docker-swarm-in-15-minutes-part-1/
https://sysdig.com/blog/monitor-docker-swarm/
https://www.slideshare.net/MariaDB/getting-started-with-mariadb-with-docker-84370473
17