Docker Orchestration (Swarm)

Orchestration is the automatic process of managing or scheduling the work of individual containers for applications based on micro services within multiple clusters. The widely deployed container orchestration platforms are based on open-source versions like Kubernetes, Docker Swarm.

Why We Need Container Orchestration?

Container orchestration is used to automate the following tasks at scale:
• Configuring and scheduling of containers
• Provisioning and deployments of containers
• Availability of containers
• The configuration of applications in terms of the containers that they run in
• Scaling of containers to equally balance application workloads across infrastructure
• Allocation of resources between containers
• Load balancing, traffic routing and service discovery of containers
• Health monitoring of containers
• Securing the interactions between containers.

Container orchestration works with tools like Kubernetes and Docker Swarm. Configurations files tell the container orchestration tool how to network between containers and where to store logs. The orchestration tool also schedules deployment of containers into clusters and determines the best host for the container. After a host is decided, the orchestration tool manages the lifecycle of the container based on predetermined specifications. Container orchestration tools work in any environment that runs containers.

Orchestration tools for Docker include the following:


Docker Machine — Provisions hosts and installs Docker Engine.
Docker Swarm — Clusters multiple Docker hosts under a single host. It can also integrate with any tool that works with a single Docker host.
Docker Compose — Deploys multi-container applications by creating the required containers.

A swarm consists of multiple Docker hosts which run in swarm mode and act as managers (to manage membership and delegation) and workers (which run swarm services). A node is an instance of the Docker engine participating in the swarm. To deploy your application to a swarm, you submit a service definition to a manager node. The manager node dispatches units of work called tasks to worker nodes.

service is the definition of the tasks to execute on the manager or worker nodes. It is the central structure of the swarm system and the primary root of user interaction with the swarm.

When you create a service, you specify which container image to use and which commands to execute inside running containers.

Join as a worker node

The Docker Engine joins the swarm depending on the join-token you provide to the docker swarm join command. The node only uses the token at join time.

# docker swarm join-token worker
To add a worker to this swarm, run the following command:
 # docker swarm join --token ********** (use same token generated while initialize master) 192.168.200.9:2044
# docker swarm join-token manager

List nodes

# docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
46aqrk4bt745z53cr3t    node-3    Ready   Active        Reachable
a5b2m3391pefq5u    node-2    Ready   Active   Reachable
ehkv3bcie79dn78otj5 *  node-1    Ready   Active        Leader

Create a service

To create a single-replica service with no extra configuration, you only need to supply the image name. This command starts an Nginx service with a randomly-generated name and no published ports. This is a naive example, since you can’t interact with the Nginx service.

$ docker service create nginx

The service is scheduled on an available node. To confirm that the service was created and started successfully, use the docker service ls command:

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                                                                             PORTS
a3mjhts1lxuem        quizzical_lamarr    replicated          1/1                 docker.io/library/nginx

To provide a name for your service, use the --name flag:

$ docker service create --name my_web nginx

Update a service

You can change almost everything about an existing service using the docker service update command. When you update a service, Docker stops its containers and restarts them with the new configuration.

Since Nginx is a web service, it works much better if you publish port 80 to clients outside the swarm. You can specify this when you create the service, using the -p or --publish flag. When updating an existing service, the flag is --publish-add. There is also a --publish-rm flag to remove a port that was previously published.

$ docker service update --publish-add 80 my_web

To verify that it worked, use docker service ls:

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                                                                          PORTS
4nhxl7oxw5vz        my_web              replicated          1/1                 docker.io/library/nginx@sha256:

Remove a service

To remove a service, use the docker service remove command. You can remove a service by its ID or name, as shown in the output of the docker service ls command. The following command removes the my_web service.

$ docker service remove my_web

Docker Service Child commands

CommandDescription
docker service createCreate a new service
docker service inspectDisplay detailed information on one or more services
docker service logsFetch the logs of a service or task
docker service lsList services
docker service psList the tasks of one or more services
docker service rmRemove one or more services
docker service rollbackRevert changes to a service’s configuration
docker service scaleScale one or multiple replicated services
docker service updateUpdate a service

Docker service create (options)

–detach , -dExit immediately instead of waiting for the service to converge
–env , -eSet environment variables
–hostnameContainer hostname
–limit-cpuLimit CPUs
–limit-memoryLimit Memory
–log-driverLogging driver for service
-modereplicatedService mode (replicated or global)
–mountAttach a filesystem mount to the service
–nameService name
–networkNetwork attachments
–publish , -pPublish a port as a node port
–tty , -tAllocate a pseudo-TTY
# docker service create --name redis --replicas=5 redis:3.0.6

Above Command Create a service with name redis with 5 replicas using redis:3.0.6 docker image.

References:

  1. https://docs.docker.com
  2. from Miscellaneous technical websites.