Docker

Docker tutorial – Create,Add Nodes,Deploy and Scale service on the Swarm

From the last post, we have understood what is container & why do we use containers in general. Just to recap here are some of the key points

  • Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in userspace. Containers take up less space than VMs (container images are typically tens of MBs in size), and start almost instantly.
  • A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.
  • Containers run apps natively on the host machine’s kernel. They have better performance characteristics than virtual machines that only get virtual access to host resources through a hypervisor. Containers can get native access, each one running in a discrete process, taking no more memory than any other executable.

In this post, we are going to take look at managing a cluster of Docker Engines using Swarm mode. Swarm Mode enables the ability to deploy containers across multiple Docker hosts, using overlay networks for service discovery with a built-in load balancer for scaling the services.

This quickstart assumes basic understanding of Docker concepts, please refer to earlier posts for understanding on Docker & how to install and containerize applications.

Docker Swarm: Key Features :

  • Docker Engine’s CLI can be used to create a swarm of Docker Engines where you can deploy application services. There is no need for any additional orchestration software to create or manage a swarm.
  • Docker Engine uses a declarative approach to let you define the desired state of the various services in your application stack.
  • For each service, a number of tasks can be declared that you want to run. When you want to scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.
  • You can specify an overlay network for your services. The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application.
  • Swarm manager nodes assign each service in the swarm a unique DNS name and load balances running containers.
  • 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.
  • At rollout time you can apply service updates to nodes incrementally. The swarm manager lets you control the delay between service deployment to different sets of nodes. If anything goes wrong, you can roll-back a task to a previous version of the service.

Swarm Mode : Key Concepts

  1. The cluster management and orchestration features embedded in the Docker Engine are built using swarmkit.
  2. A swarm consists of multiple Docker hosts which run in swarm mode and act as managers (to manage membership and delegation) and workers (which actually runs services).
  3. A given Docker host can be a manager, a worker, or perform both roles.
  4. node is an instance of the Docker engine participating in the swarm.
  5. 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. Manager nodes also perform the orchestration and cluster management functions required to maintain the desired state of the swarm.
  6. Worker nodes receive and execute tasks dispatched from manager nodes.
  7. 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.
  8. Docker includes a load balancer to process requests across all containers in the service. The swarm manager uses ingress load balancing to expose the services you want to make available externally to the swarm.

Step #1. Setup Docker

  1. From the docker site, install the latest version of the docker for your platform. Docker is available in two editions: Community Edition (CE) and Enterprise Edition (EE). Docker Community Edition (CE) is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps. Docker Enterprise Edition (EE) is designed for enterprise development and IT teams who build, ship, and run business-critical applications in production at scale.
  2. Once the installation of docker is over, check the installation by running following command docker run hello-world:

    Docker
    Image- docker hello world
  3. Run docker –version to check the version of the docker you’re running.

    Docker
    Image- check docker version

OK, now we have got the docker setup,next step is to initialize swarm mode.

Step #2. Initialize Swarm Mode

Below command turns single-host Docker host into a Multi-host Docker Swarm Mode.

docker swarm init

Docker Swarm Mode Initialize
Image – Docker Swarm Mode Initialize

Swarm Mode is built into the Docker CLI. You can find an overview of the possibility commands via docker swarm --help

After running the Swarm init command, the Docker Engine knows how to work with a cluster and becomes the manager. The results of an initialization is a token used to add additional nodes in a secure fashion. The first node to initialize the Swarm Mode becomes the manager. As new nodes join the cluster, they can adjust their roles between managers or workers.

If you want to view the current state of swarm run docker info command.

To view information about nodes run docker node ls

View information about nodes
Image – View information about nodes

As you can see there is only one node in the cluster currently, we are going to add more nodes to the cluster.

Step #3. Add nodes

Now go to 2nd node that you wish to add to the cluster, use the Docker CLI to join the existing group by using the below command. Joining is done by pointing the other host to the current manager of the cluster. In this case, the first host where we have executed the swarm mode init command on the previous step.

docker swarm join 172.17.0.28:2377 --token SWMTKN-1-0ua6xa57ie2rsn0cb0amh2q59r534u5tpe1icgyg7p2bepar5l-208qlvgjlz9w6949g4emj5s89

Token is what we got in the first step. Docker uses a port# 2377 for managing the Swarm.

Add Nodes to the Swarm Cluster
Image – Add Nodes to the Swarm Cluster

Manager will automatically accept new nodes being added to the cluster. You can view all nodes in the cluster using docker node ls

Node added to Docker Swarm Cluster
Image – Node added to Docker Swarm Cluster

Step #4. Deploy service

A service allows you to define how applications should be deployed at scale. By updating the service, Docker updates the container required in a managed way.

docker service create --replicas 1 --name upnxt alpine ping upnxtblog.com

Create service
Image – Create service

Here are the details about command

  •  docker service create command creates the service.
  • --name flag names the service upnxt.
  • --replicas flag specifies the desired state of 1 running instance.
  • arguments alpine ping upnxtblog.com define the service as an Alpine Linux container that executes the command ping upnxtblog.com.

You can view the services running on the cluster using the CLI command docker service ls

View the services running on the cluster
Image – View the services running on the cluster

To view the containers on each of the hosts,use docker ps command. There should be one instance of the container on each host.

The next step is to inspect the health and state of your cluster and the running applications.

Inspect service running on the cluster
Image – Inspect service running on the cluster

To view the list of all the tasks associated with a service across the cluster, use docker service ps command

To see list of running services
Image – To see list of running services

Here you can see DESIRED STATE and LAST STATE of the service, the task to see if tasks are running according to the service definition. Containers running in a service are called “tasks.”

Run docker ps on the node where the task is running to see details about the container for the task.

Command to see details about the container
Image – Command to see details about the container

Step #5. Scale service

Service allows us to scale how many instances of a task is running across the cluster. As it understands how to launch containers and which containers are running, it can easily start, or remove, containers as required.

Now that we have deployed, inspected the state of the service. We can use the Docker CLI to scale the number of containers in the service using the below command

docker service scale upnxt=3
Scale the service
Image – Scale the service

Now Run docker service ps upnxt to see the updated task list

Updated tasks list
Image – Updated tasks list

You can see that swarm has created 2 new tasks to scale to a total of 3 running instances of Alpine Linux. The tasks are distributed between the 2 nodes of the swarm.2 of them are running on host02 & another one on host01 which is also manager host.

Run docker ps to see the containers running on that particular node

To see the containers running on the node
Image – To see the containers running on the node

The last step is to delete the service using docker service rm upnxt to remove the upnxt service.

Like this post? Don’t forget to share it!

Additional Resources:

Summary
Docker tutorial - Create,Add Nodes,Deploy and Scale service on the Swarm
Article Name
Docker tutorial - Create,Add Nodes,Deploy and Scale service on the Swarm
Description
In this post, we are going to take look at managing a cluster of Docker Engines using Swarm mode.
Author
Publisher Name
upnxtblog
Publisher Logo

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Previous post How to wipe your personal data before selling it (For PC/Mobile/Router/eBook reader)
kubernetes logo Next post Kubernetes tutorial – Create Kubernetes cluster on AWS with kops