Docker Tutorial : Getting Started With Docker Swarm

docker swarm

Docker swarm is a clustering tool for docker. You can form a swarm cluster using one or more docker hosts. Like Kubernetes cluster manager, swarm schedules the containers on to the swarm cluster based on resource availability. You can also specify the resource for a container while deploying it. Swarm is designed to pack containers on to a host by saving other host resources for bigger containers rather than scheduling it randomly to a host in the cluster.

This tutorial is based on old swarm version. Find the latest swarm cluster tutorial here

In this article, we will learn how to set up  docker swarm cluster.

Docker Swarm Setup

In this setup, we will use two docker host (node1 and node2)  and one docker client machine (devopscube) with swarm configured on it.

You Might Like: First Look: Native Docker Clustering Video Course

Note: All the machines should have docker installed on it. You can check out this tutorial to install and configure docker on a host. How to install and configure docker

You need to configure the docker hosts to accept connections from a remote client on port 2375. You can configure this by following the steps given below. You need to perform theses stop on all the hosts.

1. Stop the docker service.

sudo service docker.io stop
ubuntu@node1:~$ sudo service docker.io stop
docker.io stop/waiting
ubuntu@node1:~$

2.  Execute the following command to start and make the docker host listen to connections on 2375

sudo docker -H tcp://0.0.0.0:2375 -d
ubuntu@node1:~$ sudo docker -H tcp://0.0.0.0:2375 -d
2015/01/13 11:46:45 docker daemon: 1.0.1 990021a; execdriver: native; graphdriver:
[0189987e] +job serveapi(tcp://0.0.0.0:2375)
[0189987e] +job initserver()
[0189987e.initserver()] Creating server
2015/01/13 11:46:45 Listening for HTTP on tcp (0.0.0.0:2375)
[0189987e] -job initserver() = OK (0)
[0189987e] +job acceptconnections()
[0189987e] -job acceptconnections() = OK (0)

Note: You need to perform the above step in all the nodes which is going to be the part of the swarm cluster. Also, if your server in the public network, do not use 0.0.0.0. instead, use the ip of the docker client machine in which you execute the swarm commands.

3.  Now, leave the connection open. You will need these terminals to see what’s happening in each host.

Now we have two nodes and one client machine with Docker installed and the docker daemon listening to 2375. Now in the client machine, we need to setup swarm for creating the cluster. Perform the steps given below on the client machine, not on node1 and node2.

1. Install go using the following command.

 sudo apt-get install -y golang

2. create a directory named “go”

mkdir go

3. export the go path using the following command.

export GOPATH=$HOME/go

4. Get the swarm binary using the following command. It will download the swarm files to the gopath (go directory) we set in the 3rd step.

go get -u github.com/docker/swarm

5. Add swarm to the PATH variable using the following command.

export PATH=$HOME/go/bin:$PATH

6. Verify the swarm setup by executing the swarm version command.

swarm --version
ubuntu@devopscube:~$ swarm --version
swarm version 0.0.1
ubuntu@devopscube:~$

8. You can view all the available swarm options by just executing the swarm command.

swarm
ubuntu@devopscube:~$ swarm
NAME:
   swarm - docker clustering
USAGE:
   swarm [global options] command [command options] [arguments...]
VERSION:
   0.0.1
COMMANDS:
   create, c    create a cluster
   list, l      list nodes in a cluster
   manage, m    manage a docker cluster
   join, j      join a docker cluster
   help, h      Shows a list of commands or help for one command
GLOBAL OPTIONS:
   --debug              debug mode [$DEBUG]
   --help, -h           show help
   --version, -v        print the version
ubuntu@devopscube:~$

Now we have the swarm setup ready. Next is to create a cluster by combining node1 and node2.

Setting Up Swarm Cluster

1. From the client machine (devopscube) execute the following command to create a swarm cluster.

swarm create
ubuntu@devopscube:~$ swarm create
73f8bc512e94195210fad6e9cd58986f
ubuntu@devopscube:~$

2.  As you can see, the swarm create command created a unique  cluster id

(73f8bc512e94195210fad6e9cd58986f) . Note down unique id because we will be using the unique id to join node1 and 2 to the swarm cluster.

3. Nodes can be joined to the cluster using the following command syntax.

swarm join --discovery token://<unique_cluster_id> --addr=<node_ip:2375>

Execute the following command on with relevant parameters to join node1 to the cluster. You will not see any output for the command.

swarm join --discovery token://73f8bc512e94195210fad6e9cd58986f --addr=<node1_ip:2375>
ubuntu@devopscube:~$ swarm join --discovery token://73f8bc512e94195210fad6e9cd58986f  --addr=54.149.184.25:2375

Close the terminal and execute the above command with node2 parameters to join it to the cluster.

3. Now let’s list the nodes in our swarm cluster using the following command.

Syntax: swarm list --discovery token://73f8bc512e94195210fad6e9cd58986f <swarm_ip:swarm_port>
ubuntu@devopscube:~$ swarm list --discovery token://73f8bc512e94195210fad6e9cd58986f 54.69.173.225:2375
54.167.134.89:2375
54.178.104.203:2375
ubuntu@devopscube:~$
5 comments
  1. swarm join –discovery token:// –addr=

    — we need run that command on node1 and node2 or only on client machine???

  2. go: missing Mercurial command. See http://golang.org/s/gogetcmd

    package github.com/docker/swarm

    imports code.google.com/p/go-uuid/uuid: exec: “hg”: executable file not found in $PATH

    Above is the output, when I tried to execute following command
    “go get -u github.com/docker/swarm”

    Please update article.
    -Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like