Docker Swarm is the Docker native clustering solution, which can turn a
group of distributed,
docker hosts into a single large virtual server.
- Docker Swarm provides the standard Docker API, and it can communicate with any tool that already works with Docker daemon allowing easy scaling to multiple hosts
- In docker swarm you create one or more managers and worker machines in the cluster .. the manager(s) take care of the orchestration of your deployed services (e.g., Creation/Replication/Assigning tasks to nodes/load balancing/service discovery )
- docker run --rm swarm --help
docker-machine create --driver virtualbox --virtualbox-memory "3000" master
docker-machine create --driver virtualbox --virtualbox-memory "3000" worker1
docker-machine create --driver virtualbox --virtualbox-memory "3000" worker2
PS C:\Program Files\Docker Toolbox> docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
master - virtualbox Stopped Unknown
worker1 - virtualbox Stopped Unknown
worker2 - virtualbox Stopped Unknown
PS C:\Program Files\Docker Toolbox> docker-machine start master
PS C:\Program Files\Docker Toolbox> docker-machine ip master
192.168.99.101
PS C:\Program Files\Docker Toolbox> docker-machine ssh master
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 17.05.0-ce, build HEAD : 5ed2840 - Fri May 5 21:04:09 UTC 2017
Docker version 17.05.0-ce, build 89658be
docker@master:~$ exit
Step 3: Init the swarm:You have to run this command on a manager machine.
PS C:\Program Files\Docker Toolbox>
docker-machine ssh master
docker swarm init --advertise-addr 192.168.99.101
docker@master:~$
The below swarm join command is always generated after init swarm command above (include token).
In order to run on both workers I will have to SSH to each one run the command then exit.
docker-machine ssh worker1 docker swarm join
\ --token SWMTKN-1-41e2y83z8t462u4039lk0ugfwnuot5vqzbiv82ug6eadtbnbhl-2lfjygo3ic0nrnhjijffxhy47 192.168.99.101:2377 exit
To add a manager to this swarm, run 'docker swarm join-token manager' and followdocker-machine ssh worker2
docker swarm join \ --token SWMTKN-1-41e2y83z8t462u4039lk0ugfwnuot5vqzbiv82ug6eadtbnbhl-2lfjygo3ic0nrnhjijffxhy47 192.168.99.101:2377 exit
Step 5: Display all cluster nodes configured
PS C:\Program Files\Docker Toolbox> docker-machine ssh master
docker@master:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
63izegp4x3to7cgoao1j9uqqg * master Ready Active Leader
svamypaqkur7ai3je5ob2z78t worker2 Ready Active
whopzq6ywt2eakrend8ji3hb8 worker1 Ready Active
Notice the master is configured as leader and the * is because we are currently connected to itStep 6: Create a network to make our services visible to each other
docker@master:~$ docker network create -d overlay my_network
Step 7: Create services and publish its portsI've pushed both images to my docker hub account and now I will pull/run them on the cluster.
docker tag bipin:eureka bipingupta007/eureka
docker push bipingupta007/eureka
docker tag bipin:config bipingupta007/config
docker push bipingupta007/config
docker@master:~$
docker service create -p 8761:8761 --name eureka --network my_network bipingupta007/eureka
docker@master:~$
docker service create -p 8888:8888 --name config --network my_network bipingupta007/config
- Here I've just deployed the services with 1 replicas .. however I could have used --replicas parameter to set the number of replicas of the service.
- You can also see that I've exposed the ports and also specified the network to make sure they can access each other using the service name
docker@master:~$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
5t42nzl8bvo0 eureka replicated 2/2 bipingupta007/eureka:latest *:8761->8761/tcp
uffvcped9b5o config replicated 1/1 bipingupta007/config:latest *:8888->8888/tcp
This will display the services and show you many of the replicas has
been started and how many are still being prepared .. it should show
something like this:Step 9: Display tasks of a service
docker@master:~$ docker service ps eureka
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
cvppczf7wgfw eureka.1 bipingupta007/eureka:latest worker1 Running Running 2 minutes ago
Step 10: Scale a serviceAs I've mentioned before I could have set the number of replicas of a service at creation time .. now since I've already started it lets scale one of them to run 5 replicas.
docker@master:~$
docker service scale eureka=3
eureka scaled to 3
docker@master:~$
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
5t42nzl8bvo0 eureka replicated 3/3 bipingupta007/eureka:latest *:8761->8761/tcp
docker@master:~$
docker service ps eureka
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS cvppczf7wgfw eureka.1 bipingupta007/eureka:latest worker1 Running Running 8 minutes ago 86vuhj694evg eureka.2 bipingupta007/eureka:latest master Running Running about a minute ago a6oi3gfq9pcv eureka.3 bipingupta007/eureka:latest worker2 Running Running about a minute ago
Of course you can either scale up or down.
Step 11: Remove the serviceand verify that the service was removed.
docker service rm eureka
docker@master:~$
docker service ls
docker@master:~$
docker ps
docker@master:~$
Step 12: Let's try calling the serviceYou can actually specify any of the cluster machine IPs not necessarily the manager.
http://192.168.99.101:8761/
Thank for this tutorial. It's very clear. Can u pls tell how to get that tocken number of master server when you join docker machine.?
ReplyDelete