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 user space. 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.
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, 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
- The cluster management and orchestration features embedded in the Docker Engine are built using swarmkit.
- 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).
- A given Docker host can be a manager, a worker, or perform both roles.
- 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.Manager nodes also perform the orchestration and cluster management functions required to maintain the desired state of the swarm.
- Worker nodes receive and execute tasks dispatched from manager nodes.
- 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.
- 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
- 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.
- Once the installation of docker is over,check the installation by running following command docker run hello-world:
- Run docker –version to check the version of the docker you’re running.
OK, now we have got the docker setup,next step is to initialize swarm mode.
Step #2. Initialise 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 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 initialisation is a token used to add additional nodes in a secure fashion. The first node to initialise 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 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 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 below command. Joining is done by pointing the other host to a 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 an 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
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
Here are the details about command
docker service createcommand creates the service.
--nameflag names the service
--replicasflag specifies the desired state of 1 running instance.
alpine ping upnxtblog.comdefine the service as an Alpine Linux container that executes the command
You can view the services running on the cluster using the CLI command
docker service ls
To view the containers on each of the host,use
docker ps command. There should be one instance of the container on each host.
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 task to see if tasks are running according to the service definition.Containers running in a service are called “tasks.”
docker ps on the node where the task is running to see details about the container for the task.
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
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 & other one on host01 which is also manager host.
docker ps to see the containers running on that particular node
Last step is to delete the service using
docker service rm upnxt to remove the
Like this post? Don’t forget to share it!
- Official documentation as a reference to understand any command.
- Docker build reference
- Docker run reference
- Best Practices article on writing Docker files.
- Test your knowledge on Dockerfile.
Also published on Medium.