Getting started with docker swarm
Docker Swarm is a container orchestration tool. Docker Swarm allows user to manage multiple containers deployed across multiple hosts/machines.
In Docker Swarm you create at least one manager and one/multiple workers. Manager manages all the workers.
One of the key benefit of Docker Swarm is the high availability. You deploy your application on multiple nodes and there could be one/more than one managers managing all worker nodes and ensuring the high availability of your application.
Second important benefit is the Load Balancing. Load balancing is very crucial and important part of any application deployed in production. Docker swarm makes sure that all containers have sufficient resources and load is balanced properly across all nodes to serve optimal efficiency.
Scalability also can serve very important role to keep application scale up when it's required.
Demo
Let's start with demo part on docker swarm. To perform demo you need some need to install docker first. If you are using older docker version then docker-machine gets installed along with the docker. But in recent versions docker machine does not get install along with docker. So you need to separately install the docker machine.
You also need to install virtualbox to run docker swarm. So make sure virtualbox is also installed on your machine.
Now just the below command to check if you have docker machine on your system. Make sure your docker is running on your system. This will give you docker version.
$ docker-machine version
docker-machine.exe version 0.16.2, build bd45ab13
If you get docker-machine command not found on windows then you need to install docker-machine separately. You also need to install
GitBash first. Once you have installed gitbash you can run below commands.
On windows using GitBash:
$ if [[ ! -d "$HOME/bin" ]]; then mkdir -p "$HOME/bin"; fi && \
curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe" && \
chmod +x "$HOME/bin/docker-machine.exe"
Run above command on GitBash. For Linux and Mac users follow
Now we have the setup, lets create the docker manager first.
docker-machine create --driver virtualbox manager1
If this command runs successfully then we are good but if this produces the below error,
Running pre-create checks...
Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"
Then you probably need to bypass the virtualbox check in your command. Run the below command.
docker-machine create -d virtualbox --virtualbox-memory=4096 --virtualbox-cpu-count=4 --virtualbox-disk-size=40960 --virtualbox-no-vtx-check manager
~ manager is name of node here. You can give any name.
Meanwhile it's running the checks, You can open virtualbox and see that it is creating a VM for you to run.
I kept the name default so it was created using default name. Follow the same command with different names like below.
docker-machine create -d virtualbox --virtualbox-memory=4096 --virtualbox-cpu-count=4 --virtualbox-disk-size=40960 --virtualbox-no-vtx-check worker1
docker-machine create -d virtualbox --virtualbox-memory=4096 --virtualbox-cpu-count=4 --virtualbox-disk-size=40960 --virtualbox-no-vtx-check worker2
Since we have created all the nodes lets check what are the nodes running on system using below command.
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER
ERRORS
default - virtualbox Running tcp://192.168.99.100 v19.03.12
worker1 - virtualbox Running tcp://192.168.99.101 v19.03.12
worker2 - virtualbox Running tcp://192.168.99.102 v19.03.12
That IP address will be same for everyone. Docker generates this as default. See the last octate which is only different ie. 100,101,102 .
Lets make the default node as manager node using below command.
$ docker-machine ssh default
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
Above command will login to default node. Once you are in the node then you can run the below command to make it manager.
docker swarm init --advertise-addr 192.168.99.100:2376
Output below:
Swarm initialized: current node (c7if56abzumka79k8mg0xbnu0) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token xyz
You can run the command which was just generated by manager node. You need to run this command on worked node. To login to worker node run the below command.
$ docker-machine ssh worker1
Now run the docker swarm join --token command to join this worker node as worker to manager.
Check all the nodes using docker node ls command.
So far we have created one total three nodes where one will be manager and other two will be worker nodes. That was it for preparing a setup and running your docker swarm. We have just created multiple nodes here. Then defined one of the node as manager and other two as worker node.
Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link