
Swarm mode is a latest feature of docker, which was released in 2016. Swarm mode is the solution to various problems of docker, like how do we automate container life cycle? how to scale them easily? and a lot more. Swarm mode in docker is an orchestrator for docker containers.
Orchestration is the automated configuration, coordination, and management of computer systems and software. So basically, by swarm you can configure, manage, deploy and play with services in docker container and do this to containers also. It gives you a feel how actually containers works in production environment. You can have a overview at https://docs.docker.com/engine/swarm/ .
Before going further you should know how swarm node works, what are manager nodes? what are worker nodes? what’s the difference between them? and how it all works together?. You should peek into this https://docs.docker.com/engine/swarm/how-swarm-mode-works/nodes/ for more info.
So here, I am building a 3 node swarm cluster in which 1 will be a host(my laptop basically) and other 2 will be docker-machines. So lets get started with creating 2 docker machines.
Creating docker machines
docker-machine --help
#just to check the command is working and to check the other options of it. And it's a good practice.
docker-machine create --driver=hyperv myvm1
#in above cmd i have used hyper-v as a driver you can use virtual-box, aws, or etc. To use virtual-box #as driver you should install latest virtual-box.
#in some cases docker requires hyper-v to run so have it to be enabled to use as a driver in vritual-box
Note: By executing above command you may encounter several problems like: you got stuck in (myvm1) Starting VM… (myvm1) Waiting for host to start… loop or something different. So, here some solutions to some problems or can say tips not to encounter a problem.
- Using hyper-v you may lack in virtual switches so create one (external) to be used by docker-machine.
- Problem may persist after creating virtual switch so try to change the adapters using that virtual switch. You can do that by going to hyper-v manager.
- After doing this problems still arises then just google it and try out some randoms.
Create another machine doing the same. So you have 2 machines now and a docker desktop running on your host.
docker-machine ls
#to check machines
Initializing Swarm
So I am gonna make ‘myvm1’ as the manager and ‘host’ and ‘myvm2’ as worker nodes.
#this is how you connect to your machine
docker-machine env myvm1
#above cmd will result like this
"export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.43.228:2376"
export DOCKER_CERT_PATH="C:\Users\Pilani\.docker\machine\machines\myvm1"
export DOCKER_MACHINE_NAME="myvm1"
export COMPOSE_CONVERT_WINDOWS_PATHS="true"
# Run this command to configure your shell:
# eval $("C:\Users\Pilani\bin\docker-machine.exe" env myvm1)"
eval $("C:\Users\Pilani\bin\docker-machine.exe" env myvm1)
#above cmd may be different in your case so run what you see there
#do the same for 'myvm2'
We are not actually going into the machines, yes we can ssh into them but I will just operate them from outside. Also the above command does not let us into the machine. It just enables them for us to use.
docker-machine ssh myvm1 "docker info"
#this cmd will run 'docker info' into myvm1
docker-machine ssh myvm1 "docker swarm init"
#this will initialize the swarm in myvm1, you will see a join token command copy it. And...
docker-machine ssh myvm2 "docker swarm join --token SWMTKN-1-3fq53rnc6fuwyobfx8wulfutkm68aqpbbu40ow7eqjwak6z474-7ezn5u7344jemshm853pt1mzy 192.168.43.228:2377"
#now myvm2 will join myvm1 as worker
docker swarm join --token SWMTKN-1-3fq53rnc6fuwyobfx8wulfutkm68aqpbbu40ow7eqjwak6z474-7ezn5u7344jemshm853pt1mzy 192.168.43.228:2377
#by this host docker will join myvm1 as worker
docker-machine ssh myvm1 "docker node ls"
#above cmd will let you know the nodes connected and their status
#you can promote or demote a node also as may you want 2 managers or so...
docker-machine ssh myvm1 "docker node --help"
You have successfully crated a 3 node swarm cluster. Congrats!
#after you have done you can leave swarm cluster by...
docker swarm leave -f
#run this cmd on each node
NOTE:- You may actually find difficulty to make your host docker as manager and machines as workers or all as managers as I faced it too, it works when all are docker machines but a host docker is not becoming a manger in any case. It may work in your case. So keep trying.
Thanks! for coming up this far and if you find any problem going through all this. Let me know below in comments. I will be more than happy to help.
One thought on “How to create a 3 node swarm cluster using docker-machine”