Docker Guides

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. A 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. A 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:

    Image- docker hello world

  3. Run docker –version to check the version of the docker you’re running.

    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

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

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.

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

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

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

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.

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

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.

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
Image – Scale the service

Now Run docker service ps upnxt to see the updated task 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

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
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
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik
Tags: dockerswarm

Recent Posts

Finding the Right Time to Build Your Software Instead of Buy

There are few things as valuable to a business as well-designed software. Organizations today rely…

6 days ago

Innovators in Crypto: Prominent AI-Powered Coins

The cryptocurrency industry is being reshaped by the fusion of blockchain technology and artificial intelligence…

3 weeks ago

Top AI Design Tools Every Graphic Designer Should Use in 2024

Introduction Artificial Intelligence (AI) has also found its relevance in graphic design and is quickly…

2 months ago

Transforming Industries: The Integration of AI and Blockchain

Imagine a world where the brilliance of Artificial Intelligence (AI) meets the unbreakable security of…

2 months ago

How Can I Use Automation to Streamline My Digital Marketing Efforts?

In today’s fast-paced digital landscape, automation is not just a luxury but a necessity for…

2 months ago

Top 5 AI Technologies Transforming the Casino Gaming Landscape in 2025

The world of casino gaming has leveraged the emerging technology advancements to create immersive and…

3 months ago

This website uses cookies.