Docker_swarm

600x400

    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.


    Requirements

    1. Docker version 1.13 or higher.

    2. 1

    3. 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:-

    1. 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

    2. 2

    3. Command to check the docker-machine version:-
    4. $docker-machine version
      $docker-machine -v

    Working with swarm.

  1. Step1: Create docker machine(to act as node for docker swarm)
  2. We will create one machine as manager and other two as workers.
    1. To check all docker-machine commands.
    2. $ docker-machine
    3. Command to create docker machine node.
    4. $ docker-machine create --driver virtualbox 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"
    5. Need driver to create machine: we going to use virtualbox.
    6. $ sudo apt-get install virtualbox

      3

    7. Now create a docker machine manager.
    8. $ docker-machine create --driver virtualbox manager1

      4

    9. To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run:-
    10. $ docker-machine env manager1

      5


  3. Step2: Check machine created successfully.
    1. 'docker-machine ls' to list out the machines that we have.
    2. $ docker-machine ls

      6

    3. The machine has been created, it's active the driver in VirtualBox.
    4. IP of this particular machine<. li=""> $ docker-machine ip manager1

    5. Now we need to create more machine with same command but change the name of the machine.
    6. $ 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.

      7


  4. Step3: SSH(connect) to docker machine.
    1. Open new terminal (ctrl+shift+t)
    2. Log into or run a command on a machine using SSH.
    3. To login, just run docker-machine ssh machinename:
      We will look how to do ssh into a docker machine directly and to use it as a VM .

      9

    4. Every time we need to open new terminal to link with ssh
    5. $ docker-machine ssh manager1
      $ docker-machine ssh worker1.1
      $ docker-machine ssh worker1.2

      SSH key:-

      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.


  5. Step4: Initialize Docker Swarm.
    1. Check node_id
    2. $ docker-machine ip manager1
    3. Now use same terminal in which we add a link between manager1 and ssh key.
    4. $ docker swarm init --advertise-addr MANAGER_IP
      $ docker swarm init --advertise-addr 192.168.99.100

      10

      We can add more manager with same command.

    5. Check the node status
    6. This command is run in only manager terminal(this command will work only in swarm manager and not in worker).
      $ docker node ls

      11


  6. Step5: Join workers in the swarm.
    1. Command for joining as worker.
    2. Run command in manager1 node(terminal).
      This will give command to join swarm as worker
      $ docker swarm join-token worker

      12

    3. Copy the swarn join command and run in worker-machine terminal to add worker for the manager node
    4. $ docker swarm join --token SWMTKN-1-1627nzpuotunfseghhlp0l8f5osvikkyi5cqmqezspoq2n1w8b-39fj8930ndg14mq20oy1ey86m 192.168.99.100:2377

      13

    5. Now go back in to manager terminal and check the status of all nodes
    6. $ docker node ls

      14

    7. Another way to join manager
    8. docker swarm join-token manager
      This will give command to join swarm as manager.
      docker swarm join --token SWMTKN-1-1627nzpuotunfseghhlp0l8f5osvikkyi5cqmqezspoq2n1w8b-1fv7a9blk474i605hbwz7vt6l 192.168.99.100:2377

      15

      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.


  7. Step6: On manager run standard docker commands.
    1. Check swarm section in which all details about swarm no of manager, nodes(manager is also node) etc.
    2. $ docker info

      16

    3. Now check docker swarm command options.
    4. $ docker swarm

      17


  8. Step7: Run containers on Docker Swarm.
    1. Now we will create a services, and run these services to create replicas with count(number) of replica.
    2. Run this command in manager1 terminal.
      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

      18

    3. Check the status:
    4. $ docker service ls

      19

    5. Running status:
    6. $ docker service ps web1

      20

      services running on all nodes

    7. Check on the browser by giving ip for all nodes
    8. Take the url port and run on browser.

      21


  9. Step8: Scale service up and down.
    1. We can increase or decrease the counts for example web1=6, till now it running in 3 nodes
    2. $ docker service scale web1=6
    3. Check the services in manger terminal.
    4. $ docker service ls
      $ docker service ps web

      23

      we can see 3 new services is created in manager1 terminal.

    5. if we run 'docker ps' command inside the manager1 terminal, we can see service is running on 2 containers on manager1

    6. 24

      now 2 container is running in manager1, other 2 in worker1.1 and rest 2 in worker1.2

    7. check in worker1.1 node
    8. $ docker ps
      same for worker1.2 node

      25

    9. now decrease the services=2
    10. now it running in worker1.1, and in worker1.2

      26

    11. all details about the node
    12. Inspecting Nodes (this command can run only on manager node)
      $docker node inspect nodename(worker1.1)
    13. inspect in running terminal
    14. $ docker node inspect self

  10. Step9: Update service.
    1. When your service is running on multiple machines and you have to make some updates.
    2. docker service update --image imagename:version web1
      $ docker service update --image nginx:1.14.0 serviceName

      27


  11. Step10: Shutdown node
    1. Command to shutdown the node.
    2. $ docker node update --availability drain worker1.2
    3. Check the AVAILABILITY of the worker1.2
    4. $ docker node ls

      28


  12. Step11: Remove service
    1. docker service rm serviceName it will remove the node from all the machine.
    2. $ docker service rm web1

      29

    3. docker swarm leave : to leave the swarm
      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

        30

        Now check in manager1 terminal

        You can see the status of workers is down.

        $ docker node ls

        31

    4. docker-machine rm machineName : to remove the machine. Run command in main terminal.
    5. $ docker-machine rm worker1.1
      $ docker-machine rm worker1.1

      32