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
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.
Quick Snapshot
OK, now we have got the docker setup,next step is to initialize swarm mode.
Below command turns single-host Docker host into a Multi-host Docker Swarm Mode.
docker swarm init
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
As you can see there is only one node in the cluster currently, we are going to add more nodes to the cluster.
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.
Manager will automatically accept new nodes being added to the cluster. You can view all nodes in the cluster using docker node ls
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
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.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
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.
To view the list of all the tasks associated with a service across the cluster, use docker service ps
command
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.
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
Now Run docker service ps upnxt
to see the updated task 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
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:
In today's digital-first world, businesses must adopt effective strategies to stay competitive. Social media marketing…
62% of UX designers now use AI to enhance their workflows. Artificial intelligence (AI) rapidly…
The integration of artificial intelligence into graphic design through tools like Adobe Photoshop can save…
The cryptocurrency trading world has grown significantly in recent years, with automation playing a key…
The non-fungible token (NFT) market has witnessed explosive growth over the past few years, transforming…
There are few things as valuable to a business as well-designed software. Organizations today rely…
This website uses cookies.