Introduction to Docker - Part 5

Introduction to Docker - Part 5

Welcome to part 5 of my Introduction to Docker blog post series! In part 1 I quickly went through installing docker and what containers are and in Part 2 we actually played with Docker containers a little bit by running a couple of Docker containers, in Part 3 we went got introduced to the Docker images and some basic operations and in Part 4 we created a simple Dockerfile. If you've not read the previous posts I encourage you to do so:

https://bobbyiliev.com/blog/introduction-to-docker-part-1

https://bobbyiliev.com/blog/introduction-to-docker-part-2

https://bobbyiliev.com/blog/introduction-to-docker-part-3

https://bobbyiliev.com/blog/introduction-to-docker-part-4

In this post I'll show you how to setup and work with Docker Swarm mode! 

I'll be using DigitalOcean for all of the demos, so I would strongly encourage you to create a DigitalOcean account follow along. You would learn more by doing!

To make things even better you can use my referral link to get a free $50 credit that you could use to deploy your virtual machines and test the guide yourself on a few DigitalOcean servers:

DigitalOcean $50 Free Credit

Once you have your account here's how to deploy your first droplet/server:

https://www.digitalocean.com/docs/droplets/how-to/create/

I'll be using Ubuntu 18.04 so I would recommend that you stick to the same so you could follow along.

What is Docker Swarm mode?

According to the official Docker docs a swarm is a group of machines that are running Docker and joined into a cluster. If you are running a Docker swarm your commands would be executed on a cluster by a swarm manager. The machines in a swarm can be physical or virtual. After joining a swarm, they are referred to as nodes. We would do a quick demo shortly on our DigitalOcean account!

The Docker Swarm consists of manager nodes and worker nodes. The manager nodes dispatch tasks to the worker nodes and on the other side Worker nodes just execute those tasks. For High Availability it is recommended to have 3 or 5 manager nodes. 

Docker Services

In order to have Docker services, you must first have your Docker swarm and nodes ready. A service is a group of containers of the same image:tag. Services make it simple to scale your application. 

Building a Swarm

I'll do a really quick demo on how to build a Docker swarm with 3 managers and 3 workers. For that I'm going to deploy 6 droplets on DigitalOcean:

Docker Swarm DigitalOcean

Then once you've got that ready, install docker just as we did in the Introduction to Docker Part 1 and then just follow the steps here:

    1. Initialize the docker swarm on your first manager node:

docker swarm init --advertise-addr your.dorplet.ip.here

 

    2. Then to  get the command that you need to join the rest of the managers simply run this:

docker swarm join-token manager

    * This would provide you with the exact command that you need to run on the rest of the swarm manager nodes. Example:

Docker Swarm Init and Join managers

    3. To get the command that you need for joining workers just run:

docker swarm join-token worker

    * It would be pretty similar to the join managers one but the token would be a bit different

The output that you would get when joinging a manager would looke something like thus:

Docker Join Manager

Then once you have your join commands, ssh to the rest of your nodes and join them as workers and managers accordingly.

After that to get some informaiton for your cluster status you could use these commands:

docker node ls

Docker Node list

Note:  this command can only be ran from a swarm manager!

And

docker info

Docker Info

To promote a worker to a manager run the following from one of your manager nodes;

docker node promote Node-ID-here

Also note that each manager also acts as a worker, so from your docker info output you should see 6 workers and 3 manager nodes.

Using Services 

To create a service use:

docker service create --name bobby-web -p 80:80 --replicas 5 bobbyiliev/php-apache

To get a list of your services run:

docker service ls

Docker create service

Then use docker services ps service-name-here to get a list of the running containers:

Docker Service ps

Then you can visit the IP address of any of your nodes and you should be able to see the service! We can basically visit any node from the swarm and we will still get the service.

Scaling a service

We could try shitting down one of the nodes and see how the swarm would automatically spin up a new process on another node so that it matches the desired state of 5 replicas:

docker replicas

You can see how I've shutdown the droplet called worker-2 and how the replica bobby-web.2 was instantly started again on another node called worker-01 to match the desired state of 5 replicas. 

To add more replicas run:

docker service scale bobby-web=7

docker more replicas

This would automatically spin up 2 more containers, you can check this with the docker service ps command:

docker service ps bobby-web

Then as a test try starting the node that we've shutdown and check if it picked up any tasks? 

Tip: Bringing new nodes to the cluster does not automatically distribute running tasks.

Deleting a service

To delete a service simply run:

docker service rm bobby-web

docker delete service

Conclusion

That is pretty much it! Now you know how to initialize and scale a docker swarm cluster!

This is the last part of my Introduction to Docker blog post series! I hope that it was helpful and you've managed to learn some cool new stuff!

As always let me know if you have any questions!

Coffee For Me
Recent Posts
Proxies Demystified
Proxies Demystified
2019-11-05 09:31:05