Docker Swarm creation
docker-machine create --driver virtualbox manager1 ( Driver can be hyperv)
docker-machine create --driver virtualbox manager1
docker-machine create --driver virtualbox worker1
docker-machine create --driver virtualbox worker2
docker-machine create --driver virtualbox worker3
docker-machine create --driver virtualbox worker4
docker-machine create --driver virtualbox worker5
docker-machine ls docker-machine ls
docker-machine ip manager1
docker-machine ssh manager1
From inside manager1 do init of swarm
docker swarm init --advertise-addr MANAGER_IP
docker node ls
Now get the token for worker joining in manager.
docker swarm join-token worker
docker swarm join-token manager
To join worker1 to manager ssh into worker1 in a new terminal
docker-machine ssh worker1
docker swarm join --token SWMTKN-1-69hfhzjl83310rnx2b4t07qxmmi1xmgjkg5def1spotpiwki-dg29ra0thuptzmux2jq7brn1g 192.168.99.100:2377 (This is token from docker swarm join-token worker)
Continue with all workers
Now in manager do
docker info
docker node ls
Create a Service
Now that we have our swarm up and running, it is time to schedule our containers on it. This is the whole beauty of the orchestration layer. We are going to focus on the app and not worry about where the application is going to run.
All we are going to do is tell the manager to run the containers for us and it will take care of scheduling out the containers, sending the commands to the nodes and distributing it.
To start a service, you would need to have the following:
- What is the Docker image that you want to run. In our case, we will run the standard nginx image that is officially available from the Docker hub.
- We will expose our service on port 80.
- We can specify the number of containers (or instances) to launch. This is specified via the replicas parameter.
- We will decide on the name for our service. And keep that handy.
What I am going to do then is to launch 5 replicas of the nginx container. To do that, I am again in the SSH session for my manager1 node. And I give the following docker service create command:
docker service create --replicas 5 -p 80:80 --name web nginx
docker service ls
docker service ps web
docker ps
docker service inspect --pretty web
To scale up the service use below command
docker service scale web=8
Checking:
docker service inspect --pretty web
docker node inspect manager1 --format "{{ .ManagerStatus.Reachability }}"
docker node inspect worker1 --format "{{ .Status.State }}"
Drain:
docker node update --availability drain worker5
docker node update —availability active worker5
To leave (stop) a node go to manager node and issue below command
docker node demote worker5
docker node rm --force worker5 (This has to be done after sometime from previous command)
Before you forcefully remove a manager node, you must first demote it to the worker role. Make sure that you always have an odd number of manager nodes if you demote or remove a manager.
Docker Container commands:
docker container ls
docker container stop
docker container rm
docker container rm image