Docker swarm for developer
Start docker swam from zero to a running application.
Introduction
A swarm is a group of machines that are running Docker and joined into a cluster. After that has happened, you continue to run the Docker commands you’re used to, but now they are 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.
This article is broken down into the following sections:
- Installation.
- Configuring Swarm.
- Configuring an HTTP application (for test).
- Verifying the deployments.
Installation
For most usable situations, docker is best installed onto a Linux OS. I have had performance issues with Windows and Mac OS alike (as both these OS run docker host using hypervisor).
Docker Swarm is available as part of Docker CE (which also contain the standalone docker program as well).
(a) Debin / Ubuntu uses apt-get
(based package-manager) and can be tamed with
sudo apt-get install docker-ce -y
(b) Manjaro / Arch uses pacman
(based package-manager) and can be tamed with
sudo pacman -Syu docker
(c) CentOS uses rpm
(based package-manager) and can be tamed with
sudo yum install docker-ce docker-ce-cli containerd.io
For more install options, please refer to https://docs.docker.com/install/linux/docker-ce/binaries/
Docker Swarm
A swarm is a cluster of one or more computers running Docker. The nodes in the cluster run as
* Manager
* Worker
In order to start a cluster, there needs to exist at least a Manager node.
To keep the installation simple, we shall create one manager
node and one worker
node. In the next section, we shall attempt to create a manager
node.
Configuring Swarm
Once docker is installed, docker info
can be used to check if its up and running.
The init command creates a Swarm manager whose purpose is to receive commands on behalf of the cluster and assign containers to Swarm nodes.
docker swarm init
swarm init generates a token to be provided to worker nodes
, in order for them to join the cluster. In case, if the join token needs to be re-fetched, the following command would help.
Once complete, the following command should show the cluster with a single node.
docker node ls
Commands to refetch the token used to join the cluster.
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-0uayalpzd2dxw18x8bb7nfeatroicbwhs3o0agsa0ju5gvdv2-33l4wrdw7e3x4iuk6x2rthm3w 192.168.1.15:2377$ docker swarm join-token manager
To add a manager to this swarm, run the following command:docker swarm join --token SWMTKN-1-0uayalpzd2dxw18x8bb7nfeatroicbwhs3o0agsa0ju5gvdv2-c26zyhrb99sc0k3jp6ru5h981 192.168.1.15:2377
The next step is to run the following command in another Node that has docker installed. Please note, this node would become a worker
node.
docker swarm join --token SWMTKN-1-0uayalpzd2dxw18x8bb7nfeatroicbwhs3o0agsa0ju5gvdv2-33l4wrdw7e3x4iuk6x2rthm3w 192.168.1.15:2377
The same command from before can be used to verify if the new worker is added to the cluster.
Once verified, we now have a working docker swarm cluster.
Sample Application
The next step is to create a sample application, that can be deployed into the above cluster.
The most common way to do this is by using a docker-compose.yml
file.
version: '3.7'services:
http-server:
image: hashicorp/http-echo
ports:
- "8080:5678"
command: -text "test1"
In the above config file, we are creating a simple http echo service that would would respond test1
when the action GET
is made. It would bind into port 8080
on the host machine.
The above config can be deployed using
docker stack deploy -c docker-compose.yml http_test
Note: docker stack
is the docker-compose
for docker swarm
. Also, the docker-compose does not support build
attribute for services
.
In my setup above, if I run docker network inspect rrfetnsypnx8
I would find a section
"Peers": [
{
"Name": "9da9c07878d1",
"IP": "10.1.1.15"
}
]
This means that, if I run http://10.1.1.15:8080 from my local browser, I would be seeing a message test1
. Docker would tweak the network so that I would be able to see the http endpoint of this application from the worker node as well (example, if my worker is running at 10.1.1.16
then http://10.1.1.16:8080 would also reach to the same endpoint).
With docker swarm, we can scale out the application
If trying to have notes on these docker commands is troubling, now we have a good GUI tool for use. Its called Portainer (https://www.portainer.io/). The installation is literally two lines for a Docker Swarm.
$ curl -L https://downloads.portainer.io/portainer-agent-stack.yml -o portainer-agent-stack.yml
$ docker stack deploy --compose-file=portainer-agent-stack.yml portainer
⛑ Suggestions / Feedback ! 😃