Wt is Docker swarm - amresh087/newronaRepos GitHub Wiki

A swarm is a group of machine that are running docker and joined into a cluster.

Docker swarm is tool for container orchestration(planning or coordination) where orchestration managing and controlling multiple docker container as a single service

On the above work there are multiple tools available like Docker swarm, Kubernetes, Apache Mesos

Suppose if we have 100 container.

So need to do health check on every container.

  1. Ensure that all container up every time up.
  2. Scaling the container up or down depending on the load.
  3. Add update/change to all the container.

Pre-requisites

  1. Docker 1.13 or higher
  2. Docker Machine (pre installed for Docker for Windows and Docker for Mac)

install docker-machine

docker machine docs

After install docker-machine you can verify by below command

docker-machine -v

.

.

Step 1.-->> Create docker machine (to act as nodes for docker swarm)

create on machine as manager and other as worker

docker-machine --help


For local system

docker-machine create --driver virtualbox manager1

where manager1 node name you can give any name

If you are getting below error then need to one moe command for install VM

Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path

brew cask install virtualbox for Mac

yum install virtualbox for CentOS

sudo apt-get install virtualbox for Ubuntu

And also enable virtualization


For EC2

If you want run docker swarm (docker-machine) then first create IAM in aws

Step for creation IAM

if you set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in environment variable then no need to type every time for running docker-machine

Note --> Environment variable set temporary mean if you close putty or terminal then variable remove. Next time you need set again.

export AWS_ACCESS_KEY_ID=AKIAJ4H737BX73IJDW5A

export AWS_SECRET_ACCESS_KEY=BOJts7NNmda...........(MY SECRETS KEY)

docker-machine create --driver amazonec2 manager1

or you use

docker-machine create --driver amazonec2 --amazonec2-access-key AKI**** --amazonec2-secret-key BOJ**** manager1

More Details link

Step 2.-->> docker-machine ls

docker-machine ip master1

Now create worker machine

docker-machine create --driver amazonec2 worker1

docker-machine create --driver amazonec2 worker2

docker-machine create --driver amazonec2 worker3

Step 3. -->> Now Here we need multiple terminal for connect master1 to worker1, worker2, worker3.

Now Here we need multiple terminal for connect master1 to worker1, worker2, worker3. So in putty that is not possible because putty support one session at a time. This problem solved by superputty

Download SuperPuTTY

After download unzip it and click on the exe now open one window Note--> Superputty also need putty if in you system putty not there then install.

Now click on OK button then open new screen like below here you can open multiple session if you want open same session on 4 terminal then click 4 time on green button otherwise you want open separate session then enter ip and other thin then click green button

Now open one terminal

docker-machine ssh master1

Now open second terminal

docker-machine ssh worker1

Now open third terminal

docker-machine ssh worker2

Now open fourth terminal

docker-machine ssh worker3

Step 4--> Initialize docker swarm

Go to master terminal

sudo docker swarm init --advertise-addr 18.205.17.178

you can get docker-machine ip by using docker-machine ls

Now you can see below generate token for join worker

In master/manager terminal you can run command sudo docker node ls but not work other terminal

Step 5--> Join worker in swarm

Open new terminal

docker-machine ssh worker1

docker swarm join --token SWMTKN-1-00yjv73f9i880jyfr2yetxwjt3cnjw4al0952gyu2ds5ja7ogx-05duudpgykzutxov7hoob3u39 54.84.49.167:2377

Open new terminal

docker-machine ssh worker2

docker swarm join --token SWMTKN-1-00yjv73f9i880jyfr2yetxwjt3cnjw4al0952gyu2ds5ja7ogx-05duudpgykzutxov7hoob3u39 54.84.49.167:2377

Open new terminal

docker-machine ssh worker3

docker swarm join --token SWMTKN-1-00yjv73f9i880jyfr2yetxwjt3cnjw4al0952gyu2ds5ja7ogx-05duudpgykzutxov7hoob3u39 54.84.49.167:2377

Step 6 -->> Know status on manager terminal

docker info

docker swarm

Step 7 -->> Run container on docker swarm

Now go to the Manager Terminal

sudo docker service create -- replicas 3 -p 9080:80 -- name myweb nginx

sudo docker service ps myweb

So here create 1 manager and 2 worker

Step 8 -->> Scale service up and down

Now go to the Manager Terminal

sudo docker service scale myweb =4

sudo docker node inspect worker1

update

for example i update service name so open manager terminal

sudo docker service update --image image name:version serviceName

sudo docker service update --image nginx:1.4.0 web1

Step 9 -->> Shutdown Node

Open manager terminal

docker node update -- availability drain worker1

Step 10 -->> Remove service

docker service rm web1

docker swarm leave worker1

docker-machine stop worker1