A swarm is a group of machines that are running Docker and joined into a cluster. Swarm turns multiple docker hosts into a single, virtual docker host.
Docker Swarm is a tool for Container Orchestration.
Let’s take an example:- You have 100 containers and you need to do following:-
- Health check on every container
- Ensure all containers are up on every system
- Scaling the containers up or down depending on the load
- Adding updates/changes to all the containers
Orchestration - managing and controlling multiple docker containers as a single service.
- Docker version 1.13 or higher.
- Docker Machine (pre installed Docker for Windows and Docker for Mac).
On mac OS and Windows, Machine is installed along with other Docker products when you install the Docker for Mac, Docker for Windows, or Docker Toolbox.
But in ubuntu you need to install the machine.
Command to install docker-machine in ubuntu:-
- base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
sudo install /tmp/docker-machine /usr/local/bin/docker-machine
- Command to check the docker-machine version:- $docker-machine version
Working with swarm.
- To check all docker-machine commands. $ docker-machine
- Command to create docker machine node. $ docker-machine create --driver virtualbox manager1
- Need driver to create machine: we going to use virtualbox. $ sudo apt-get install virtualbox
- Now create a docker machine manager. $ docker-machine create --driver virtualbox manager1
- To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run:- $ docker-machine env manager1
docker-machine:Error with pre-create check: “exit status 126”
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"
- 'docker-machine ls' to list out the machines that we have. $ docker-machine ls
- The machine has been created, it's active the driver in VirtualBox.
- IP of this particular machine<. li="">
$ docker-machine ip manager1
- Now we need to create more machine with same command but change the name of the machine. $ docker-machine create --driver virtualbox worker1.1
$ docker-machine create --driver virtualbox worker1.2
In short we have 3 nodes out of which one will be manager 2 will be workers.
Open new terminal (ctrl+shift+t)
- Log into or run a command on a machine using SSH. To login, just run docker-machine ssh machinename:
- Every time we need to open new terminal to link with ssh $ docker-machine ssh manager1
We will look how to do ssh into a docker machine directly and to use it as a VM .
$ docker-machine ssh worker1.1
$ docker-machine ssh worker1.2
You can link your Cloud and Service providers so that Docker Cloud can provision and manage swarms on your behalf. For this, you need an SSH key to authenticate Docker to your provider.
SSH is a secure protocol for accessing remote machines and applications. It provides authentication and encrypts data communication over insecure networks.
- Check node_id $ docker-machine ip manager1
- Now use same terminal in which we add a link between manager1 and ssh key. $ docker swarm init --advertise-addr MANAGER_IP
- Check the node status This command is run in only manager terminal(this command will work only in swarm manager and not in worker).
$ docker swarm init --advertise-addr 192.168.99.100
We can add more manager with same command.
$ docker node ls
- Command for joining as worker. Run command in manager1 node(terminal).
- Copy the swarn join command and run in worker-machine terminal to add worker for the manager node $ docker swarm join --token SWMTKN-1-1627nzpuotunfseghhlp0l8f5osvikkyi5cqmqezspoq2n1w8b-39fj8930ndg14mq20oy1ey86m 192.168.99.100:2377
- Now go back in to manager terminal and check the status of all nodes $ docker node ls
- Another way to join manager docker swarm join-token manager
This will give command to join swarm as worker
$ docker swarm join-token worker
This will give command to join swarm as manager.
docker swarm join --token SWMTKN-1-1627nzpuotunfseghhlp0l8f5osvikkyi5cqmqezspoq2n1w8b-1fv7a9blk474i605hbwz7vt6l 192.168.99.100:2377
SSH into worker node(machine) and run command to join swarm as worker.
In Manager Run command - docker node ls to verify worker is registered and is ready.
Do this for all worker machines.
- Check swarm section in which all details about swarm no of manager, nodes(manager is also node) etc. $ docker info
- Now check docker swarm command options. $ docker swarm
- Now we will create a services, and run these services to create replicas with count(number) of replica. Run this command in manager1 terminal.
- Check the status: $ docker service ls
- Running status: $ docker service ps web1
- Check on the browser by giving ip for all nodes Take the url port and run on browser.
docker service create --replicas 3 -p 80:80 --name service_Name nginx
replicas=3 means it run in 3 node, manager is also node.
Use nginx web browser to see rinning container.
$ docker service create --replicas 3 -p 80:80 --name web1 nginx
services running on all nodes
- We can increase or decrease the counts for example web1=6, till now it running in 3 nodes $ docker service scale web1=6
- Check the services in manger terminal. $ docker service ls
- if we run 'docker ps' command inside the manager1 terminal, we can see service is running on 2 containers on manager1
- check in worker1.1 node $ docker ps
- now decrease the services=2 now it running in worker1.1, and in worker1.2
- all details about the node Inspecting Nodes (this command can run only on manager node)
- inspect in running terminal $ docker node inspect self
$ docker service ps web
we can see 3 new services is created in manager1 terminal.
now 2 container is running in manager1, other 2 in worker1.1 and rest 2 in worker1.2
same for worker1.2 node
$docker node inspect nodename(worker1.1)
- When your service is running on multiple machines and you have to make some updates. docker service update --image imagename:version web1
$ docker service update --image nginx:1.14.0 serviceName
- Command to shutdown the node. $ docker node update --availability drain worker1.2
- Check the AVAILABILITY of the worker1.2 $ docker node ls
- docker service rm serviceName it will remove the node from all the machine. $ docker service rm web1
- docker swarm leave : to leave the swarm
- docker-machine rm machineName : to remove the machine. Run command in main terminal.
$ docker-machine rm worker1.1
$ docker-machine rm worker1.1
Stop workers nodes from main terminal and then check the status of nodes in manager1 terminal.
docker-machine stop machineName : to stop the machine
$ docker-machine stop worker1.1
$ docker-machine stop worker1.2
Now check in manager1 terminal
You can see the status of workers is down.
$ docker node ls