How to Use Docker Swarm in DevOps

21 Sep 2020

How to Use Docker Swarm in DevOps

Steps on How to Use Docker Swarm for container deployment.

Set Up VMs node using Docker-machine

  1. Create 1 manager node. Name it m1(indicating that it is manager 1)
    1. Use the driver as virtualbox. There are many other drivers which you can use i.e. amazonec2 etc.But to avoid any further complexities we will keep it simple. And we will create multiple nodes under same physical machine.
    2. So my system is that machine and on top of it , I am inducting 3 machines (1 manager and 2 workers)
    3. docker-machine create -d virtualbox m1  ==> this will create one manager
  2. Create two worker nodes, using the same command as above just change the name as w1 and w2 (worker 1 and worker 2)
    1. docker-machine create -d virtualbox w1
    2. docker-machine create -d virtualbox w2
  3. Any time you wish to rm the machines run the command: docker-machine rm w1 w2 (this will remove the w1 and w2 machines)
  4. To check the status of the machine: docker-machine ls (list all the machines and their ip addresses)
  5. To check details of the specific machine: docker-machine inspect m1 or docker-machine inspect m1 | grep -I swarm (to filter the text of the return item)

Create Docker Swarm on the machines created earlier using docker-machine

  1. Run docker-machine ls. Output as below: i.e. three machines but none of them is active and no swarm set up.

NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS

m1             virtualbox   Running   tcp://192.168.99.101:2376           v19.03.12   

w1             virtualbox   Running   tcp://192.168.99.102:2376           v19.03.12   

w2             virtualbox   Running   tcp://192.168.99.103:2376           v19.03.12   

  1. We will use m1 for manager and others as workers
  2. To make m1 as manager we need to first login in the m1 to run docker init command. There are two ways to do it:
    1. [Not recommended way (in my opinion)]: Export the DOCKER_HOST=IP i.e. in this case: “export DOCKER_HOST=192.168.99.101:2376
      1. run: docker info | grep Name ==> this will show u ‘docker-desktop’. This indicates that docker desktop Virtual env will receive the docker commands.
      2. Run export DOCKER_HOST= 192.168.99.101:2376  and again run : docker info | grep Name ==> this will give u ip address, indicting that now docker commands will be routed to the docker engine at this ip machine.
      3. All the ‘docker’ commands that we will run after this will route to the m1 machine.
      4. Not a recommended way for beginner because it might lead to confusion on to which docker engine on which host commands are routing.
    2. [Recommended way]: Do a ssh in the host machine and run commands.
      1. For ssh to the desired machine in our case is master
      2. Run : docker-machine ssh m1
      3. This will open bash inside the machine
      4. To exit any time from the bash: run:==> exit
    3. [One more recommend way]: run docker-machine env and follow the instruction.
    4. Run docker —version to check if docker is installed already. This installation of docker is made possible by the docker-machine create commands and drivers when we set up these virtual machines.
    5. Please be aware: Docker-compose and docker-machine will not be available on these VM machines and any way we do not need them to be there. All we need is docker.
    6. Now we are good to initialise the docker swarm on our master.
    7. Docker swarm commands to be executed next:
      1. Docker swarm Manager Set Up
      2. ‘Docker swarm init’ ==> this will give error since VM has two IPs for different ethernet interfaces.
      3. This is the error you will get: Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.0.2.15 on eth0 and 192.168.99.101 on eth1) – specify one with –advertise-addr
      4. run: docker swarm init –advertise-addr 192.168.99.101
      5. You will get this message:
        1. Swarm initialized: current node (m07p2twccp0pm74q3fehq4zal) is now a manager.
        2. To add a worker to this swarm, run the following command:
        3. docker swarm join –token SWMTKN-1-5pz127rl1a9qq2mvno4gk17ms80bgivooklkhgd1c8rka2pau6-5e6dkia4dlsn6m5ufgepiqyuf 192.168.99.101:2377
        4. To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
      6. We have one manager node so far. It acts as a master as well as worker until other worker node are not added.
      7. If you want to add another manager to swarm then you need to add run the command on manager node:
        1. Docker swarm join-token manager
        2. It will give you another token to Join as manager. Then you haver to login to another vm and run the command to join the swarm but with token generated for joining as a manager.
        3. If you forget the tokens simply run below commands:
          1. Docker swarm join-token manager. ==> will give command to join as a manager. then you Run generated command on other node to join as manager
          2. Docker swarm join-token worker. == > will give command to join as a worker. Run you run generated command on other side to join as worker.
      8. Docker swarm Worker Set Up
      9. Line 4.3 above gives the token to be used to join the swarm. Login to the worker nodes and join the swarm.
        1. Exit from m1 manager machine.
        2. Ssh to w1 and w2 machine. Run: docker-machine ssh w1
        3. Run command: format: docker swarm join <token> HOST:PORT
          1. docker swarm join –token SWMTKN-1-5pz127rl1a9qq2mvno4gk17ms80bgivooklkhgd1c8rka2pau6-5e6dkia4dlsn6m5ufgepiqyuf 192.168.99.101:2377
        4. You will get the result as “This node joined a swarm as a worker.”
        5. Run command to check if w1 is registered as node to the manager.  docker info | grep -i swarm  or simple docker info
          1. Result should be “swarm : Active” and manager ip will also be listed.
      10. To check your swarm state , ssh to m1 node:
        1. Docker-machine ssh m1
        2. Docker node ls
        3. Connect your w2 as well and you swarm is ready. You can then start your services.

                                  docker@m1:~$ docker node ls                                                                                                                                                         

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

m07p2twccp0pm74q3fehq4zal *   m1                  Ready               Active              Leader              19.03.12

rw606s86s1eh8dulontzr6iv5     w1                  Ready               Active                                  19.03.12

Create  service for your Docker swarm – Visualizer

  1. First service we are creating is to visualise the managers and workers running tasks and containers.
  2. Tool to be used: https://hub.docker.com/r/dockersamples/visualizer
  3. GO to master node and run command as mentioned in the above link as well:
    1. docker service create  –name=viz  –publish=8080:8080/tcp   –constraint=node.role==manager  –mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock  dockersamples/visualizer
  4. Then open in browser: “http://192.168.99.101:8080/”. ==> this is the url of manager node.
  5. This link displays the current state of tasks, services and containers
  6. Few commands to check:
    1. Docker service ls ==>will list all the services
    2. Docker service ps viz ==> will list all the tasks running under this service
    3. Docker ps ==> will list the containers

Deploy  API on SWARM

  1. Syntax for bringing the service up: docker service create [OPTIONS] IMAGE [COMMAND] [ARG…]
  2. Travel plan api: docker service create –name travel-plan-api –publish 8001:8001 <ur api>:20200901-160308.cf94898
  3. Scale the api: docker service scale <api-name>=5
  4. After scaling, this is how it looks.
  5. To scale down: docker service scale <api-name>=2
  6. Run load command: ab  -n 1000 -c 4 http://192.168.99.101:8001 ==> hitting the api with total of 1000 request with concurrency of 4 users.
  7. More containers we have up and running, there will be more throughput i.e. more request per second our swarm can handle.
  8. Load balancing is automatically done by swarm.
  9. Currently we have opened all the ports of all the workers. So api can be accessed on any of the node. But later we will try with other way hiding the port of workers. So that api will be published on one port across the whole swarm and should be routed based on loads.

        docker@m1:~$ docker node ls                                                                                                                                                         

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

m07p2twccp0pm74q3fehq4zal *   m1                  Ready               Active              Leader              19.03.12

rw606s86s1eh8dulontzr6iv5     w1                  Ready               Active                                  19.03.12

mhu10f1nqcvy82nms7wvyfowk     w2                  Ready               Active                                  19.03.12

After calling up: docker service scale <api-name>=5.  (only 5 instances will remain)

After calling down: docker service scale <api-name>=2.  (only 2 instance will remain)

Courses

View all