Docker Swarm

!
Warning: This post is over 365 days old. The information may be out of date.

Small howto to explain how to create a Swarm cluster with Docker. This setup has been realized with my Raspberry Pi systems but feel free to use whatever you want.

Install Docker

For this part, you can follow my setup here

Initialize the cluster

Swarm cluster is easy to create. At first you have to initialize your first node :

% docker swarm init
Swarm initialized: current node (iwy90it8c20vx9zbihmtsaqsi) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-3b49f4noaq1mqsltkhjybclmfzlhkjfiaff1rs509s1vek3f5k-683br7b1erxj1lxt24g88g405 192.168.1.34:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

You can specify an interface or ip if you have many. Look at the documentation

Nodes : managers vs workers

Now that you have our first node, we will attach the others. You have two options to join the cluster :

  • join the cluster as a manager
  • join the cluster as a worker

Here is a small image to explain the difference between manager and worker:

docker nodes

Very basically, managers will handle all the cluster management tasks AND execute containers. And the workers will only execute containers.

If you want to know more, the Docker documentation is here

A basic rule is to always maintain an odd number of managers to support node failures. As usual, the Docker documentation explain this very well.

Join the cluster

  • Join the cluster as manager

    • At first, you must retrieve the token. It was given to you by the docker swarm init command or if you has loose it :
      % docker swarm join-token manager
      To add a manager to this swarm, run the following command:
      
      	docker swarm join --token SWMTKN-1-3b49f4noaq1mqsltkhjybclmfzlhkjfiaff1rs509s1vek3f5k-crtdjmq4jaxcbsuuqgww6hdzr 192.168.1.34:2377
      

    This command must be typed on the first node initialized.

    • Connect to your another machine and type the command :

      % docker swarm join --token SWMTKN-1-3b49f4noaq1mqsltkhjybclmfzlhkjfiaff1rs509s1vek3f5k-crtdjmq4jaxcbsuuqgww6hdzr 192.168.1.34:2377
      This node joined a swarm as a manager.
      
    • Verify our cluster :

      ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
      iwy90it8c20vx9zbihmtsaqsi     docker01            Ready               Active              Leader
      keaxsm0qwne61l25hqn84zmjd *   docker02            Ready               Active              Reachable
      

Everything is fine ! We have a cluster of two machines (both are manager)

  • Join the cluster as worker

    • Same as for manager, you will need to get the token :

      % docker swarm join-token worker 
      To add a worker to this swarm, run the following command:
      
      	docker swarm join --token SWMTKN-1-3b49f4noaq1mqsltkhjybclmfzlhkjfiaff1rs509s1vek3f5k-683br7b1erxj1lxt24g88g405 192.168.1.34:2377
      

      Of course this token is not the same as the token for managers.

    • Connect to your another machine and type the command :

      % docker swarm join --token SWMTKN-1-3b49f4noaq1mqsltkhjybclmfzlhkjfiaff1rs509s1vek3f5k-683br7b1erxj1lxt24g88g405 192.168.1.34:2377
      This node joined a swarm as a worker.
      
    • Verify our cluster :

      ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
      iwy90it8c20vx9zbihmtsaqsi     docker01            Ready               Active              Leader
      keaxsm0qwne61l25hqn84zmjd *   docker02            Ready               Active              Reachable
      ryn90tzrr5c6tzq48xvptftj1     docker03            Ready               Active              
      

    As stated above, the worker cannot execute cluster management tasks (so docker node ls will not work on a worker).

Manage your nodes

  • Promote a node ( worker become manager ) :

    % docker node promote docker03
    Node docker03 promoted to a manager in the swarm.
    
  • Demote a node ( manager become worker ) :

    % docker node demote docker03
    Manager docker03 demoted in the swarm.
    
  • Change the availability of your nodes : active / drain / pause

    • active (the node is active) :

      % docker node update --availability=active docker03
      
    • drain ( no new tasks are assigned to the node. The existing tasks will be shut down and scheduled on an available node)

      % docker node update --availability=drain docker03
      
    • pause ( no new tasks are assigned to the node. The existing tasks remain running)

      % docker node update --availability=pause docker03
      

Hint : if you want to have a manager node that do NOT run containers, you must set it’s availability as drain

Related Posts