swarm - is a group of machines that are running docker and joined into a cluster.
Docker Swarm is a tool for container orchestration.
Orchestration - managing and controlling multiple docker containers as a single service
Let's suppose we have 100 containers. You need to do:
- Health check on every container.
- Ensure all containers are up on every system.
- Can scale up and down the containers, depending on the load.
- Adding updates/changed to all the containers.
Tools Available - Kubernetes, Docker swarm, Apache Mesos.
- Create docker machines to act as nodes for docker swarm. Create one machine as manager and others as worker.
NOTE - docker-machine, is not installed with latest docker desktop versions. You need to install it separately, https://docs.docker.com/machine/install-machine/#install-machine-directly. Also you need to install virtualbox as well.
Note - virtualbox is a OSS, that creates VM in which user can run other os.
docker-machine create --driver virtualbox manager1
docker-machine env manager1
Note - following commands can be used to see the machines created through docker-machine
docker-machine ls
docker-machine ip manager1 # this will give ip of manager 1
Now create a worker as well.
docker-machine create --driver virtualbox worker1
docker-machine create --driver virtualbox worker2
Connect to the docker machine (using ssh )
docker-machine ssh manager1
Initialize docker swarm.
# run this on manager machine. This will make this node a manager.
docker swarm init --advertise-addr MANAGER_IP
# run this on manager machine
docker node ls # will show who is manager and who is worker
Add workers to the swarm.
In manager node run the following commands
# this will give a command. Run it on any worker machine to make it a workre.
docker swarm join-token worker
On manager run standard docker commands.
# NOTE - now there will a swarm section as well.
docker info
Run containers on docker swarm
# run it on manager node
# NOTE - this will run 3 instances of nginx on three nodes (1 manager, 2 worker)
docker service create --replicas 3 -p 9090:80 --name web1 nginx
# to see the replicas
docker service ls
Scale service up and down. On manager node.
NOTE - if there are 3 nodes, and you have given web1=5, 4th and 5ht one can be randomly created on anyone.
docker service scale web1=5
docker@manager1:~$ docker service ps web1
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ati9mhoid0xp web1.1 nginx:latest worker1 Running Running 6 minutes ago
t20x8sekgmw4 web1.2 nginx:latest worker2 Running Running 6 minutes ago
rpfx6heamxd0 web1.3 nginx:latest manager1 Running Running 6 minutes ago
1kno0i0knifq web1.4 nginx:latest worker1 Running Running 2 minutes ago
jiollg0d0gz2 web1.5 nginx:latest worker2 Running Running 2 minutes ago
Now if you go into worker1. and do
# will show the running containers on worker1
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a7fe72c7ab4 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp web1.4.1kno0i0knifqqewbi1r8b5x2n
8d75807e1b49 nginx:latest "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp web1.1.ati9mhoid0xpnb4ns2hffrmzk
Inspect Nodes (this will run on manager node only)
docker node inspect worker1
# to inspect manager itself
docker node inspect self
Update your service running on multiple machines at once.
# till now we were using nginx:latest
docker service update --image nginx:1.14.0 web1
Shutdown node.
docker node update --availability drain worker1
Remove the service.
docker service rm web1
docker service ps web1
Leave the swarm
# if you run it on worker1, it will leave the swarm.
docker swarm leave
docker node ls (will run only on manager.)
Note - run these commands from outside of any worker/manager machine
docker-machine stop worker1