Docker Swarm
Docker Swarm
Docker Swarm
Docker Introduction
Docker is a tool intended to make the process of creating, deploying and running
applications easier by using container based virtualization technology.
Docker Engine
Docker engine is the Docker core component that is responsible for creating Docker
Images and running them as services.
Docker Engine Core Components:
Docker Daemon
Continuous running program (daemon process) that manages the service
and other docker objects tied to it.
REST API
Specifies interfaces, that programs can utilize to speak to the daemon and
direct it what to do.
Docker Client
CLI is utilized to interact with the daemon (docker command).
Desired state reconciliation
3
Docker Networking
The concept of networking in Docker comes into picture when working with Docker in
a real time scenario at a large scale.
Host and containers in Docker are tied with 1:N relationship, which means one
host can command multiple containers.
More on Docker
In this course, you will learn
Swarm - Benefits
Swarm does not require any additional installation. This comes as an in-built
feature in Docker itself.
With the decentralised service discovery, Swarm v2 supports clusters with
multi thousand nodes.
Swarm mode works out-of-the-box. You need no changes to the existing
container system to adapt to this tool.
Easy to use tool with few simple commands
Cluster Management
Before we get into Docker Swarm in detail, let us understand some basic concepts of
cluster.
Cluster Management is a technique to manage one or more cluster nodes. This is
done using Cluster manager and agent.
Cluster Manager is nothing but a GUI or CLI software.
Clustering Tool
Clustering tool is a software that manages a set of resources through a single
point of command. In our case, these set of resources are nothing but containers.
For e.g,
Workload distribution management across a distributed system/ cluster is very
tedious for the large enterprise systems. Clustering tool comes as a rescue by
automating this task.
The instructor will just specify the details such as cluster size, settings, and some
advanced features. Rest everything is taken care by the clustering tool.
Docker Swarm is one such example of a clustering tool for Containers.
In November 2014, Docker team retained the concept of cluster communication with
additional Remote APIs and named this as Swarm. This first generation is called
Swarm v1.
Decentralized design
1
Docker Node
Docker Service
Docker Tasks
Docker Node
Docker Node is a Docker Engine instance that is included in the Docker Swarm.
In Real time, these docker nodes are distributed across multiple cloud as well as
physical machines.
There are 2 kinds of Docker nodes.
Manager Node
Worker Node
Manager Node:
Manager Node is responsible for all Orchestration and container management
activities required to keep up the desired system state.
Worker Node:
Worker node executes the tasks assigned by Manager node.
Drain Node
You can set the availability of any node to Drain, if you do not want to execute any
task on the node.
Suppose you do not want manager node to process any task, you can set this as
Drain node. The scheduler gracefully terminates any more task allocation and moves
the node to Drain mode.
Docker Service
Docker Service:
Service is nothing but a task definition that has to be executed.
You will have to create a service specifying the image name and other additional
parameters.
In most cases, service is a image for a microservice of some large application.
Few Examples: HTTP server or a database, any kind of executable program that
you would run in a distributed system.
Docker Swarm- Architecture
Raft Consensus
Manager nodes use the Raft Consensus Algorithm to internally manage the cluster
state.
This is to ensure that all manager nodes that are scheduling and controlling tasks in
the cluster maintain/store consistent state.
Have more than one Master node as well as odd number of Master nodes for
High Availability.
Have maximum of seven manager nodes for a Swarm.
Scheduling Tasks
Consider an example shown in the picture. The diagram shows 3 replicas of redis
server running in 3 containers. You want to load balance between three instances of
an redis server.
In this model, each task invokes one container. You can assume a task to be a slot
where the scheduler places a container. The scheduler identified the task to be in a
running state when the container comes up.
Pending Services
If you do not want a service task to be executed currently, then you can configure to
place the service state in pending.
Ways to achieve this:
Replicated
Global
Replicated Service:
In this type, you will specify the number of replicas (identical tasks) that
you would run as tasks on the containers.
Global Service:
This service runs one task per worker node. You need not specify any
number of replicas. Swarm automatically adds or removes tasks based
on the total number of active nodes at a given time.
Few examples are monitoring agents and anti-virus scanners.
Note:
You can configure the manager node to publish its address as a Manager with the
mentioned ip address as below.
This command lists the basic Swarm commands with the usage details.
This command will list the number of nodes currently available in the Swarm.
You will learn more basic commands going through the upcoming scenario in the
playground.
Service Discovery
Service discovery is a technique that Docker uses to transfer the request that
comes from external clients to separate nodes to execute it without exposing any
node details.
For example, if you have an event service which saves data using MySQL service
(both services are connected through overlay network). You will have to expose the
port details of your event service to the client. MySQL service port details are
required to be shared with the event service alone.
Service Discovery - How it works
Docker assigns a virtual IP (VIP) to the service by default when you create a
service. This IP will be used by clients to reach out to the service.
Docker has the worker nodes list for every service to route request between nodes.
Routing Mesh in Docker Swarm enables multi-host networking. This enables
containers on various hosts to talk to each other as if they are on the same host.
This is carried out by creating a Virtual Extensible LAN (VXLAN), which is
designed for cloud-based networking.
Load Balancing
Docker Swarm internally has an ingress load balancer to distribute the traffic to
containers that are directly exposed to public.
You can also configure external load balancer to direct the request to appropriate
containers irrespective of whichever host runs the service.
User will still be able to fetch the application from any node in the Swarm, even
though the corresponding node/ Manager node is down
Overlay Networks
Communications between docker daemons participating in swarm mode happens
through overlay network.
Overlay network for Swarm mode is similar to user defined network bridge network
within containers.
Ingress Network
This is also a kind of overlay network which enables load balancing among nodes. This
network is available by default when you initialize or join a swarm.
Swarm Security
Docker has an in built Public Key Infrastructure (PKI) that enables to maintain a
secure container orchestration system.
Certificate Authorities
When you initialize a swarm, the current node is marked as the manager node and
it generates a new root certificate authority (CA) with a key pair. This is used
by other nodes to join the swarm.
You can also add your own externally- generated CA, which is added using the
flag --external-ca during swarm initialization.
Manager node also generates two tokens,
External CA
There is a possibility that the whole cluster could be compromised if the root CA gets
leaked.
Docker Swarm mode also uses external CAs. This is used to retain the Swarm
Managers' identity.
For e.g.
docker swarm update --cert-expiry 2h
Secrets
Secrets are nothing but a set of data that should be kept encrypted without being
exposed in the transport network. e.g. password, security token etc.
This secret can be accessed only by the services/tasks that is permitted explicitly
through permission grant access.
Passwords
SSH Keys
Database name/ Server name
TLS certificates and keys
Secrets isolate themselves from the container data.
e.g.
docker secret create secret1 tokenfile
List Secrets
docker secret ls
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
command and provide the following key:
SWMKEY-1-WuYH/IX284+lRcXuoVf38viIDK3HJEKY13MIHX+tTt8
When you restart the instance, you will have to unlock the swarm with the key
generated. Otherwise the services will not start and you will notice an error asking to
unlock the swarm.
Command to restart the docker instance.
docker service ls ↵
Error response from daemon: Swarm is encrypted and needs to be unlocked before it
can be used. Please use "docker swarm unlock" to unlock it.
To unlock a Swarm.
Note:
If the docker instance goes down before the key is rotated, you may have to unlock
the Swarm with the old key. Do keep a note of both the keys (old and new) for few
minutes.
What is Swarm
Benefits of using Swarm
Docker Swarm Architecture, Workflow
Service Discovery, High Availability and Load Balancing
Networking
Security