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
[email protected]:~$ sudo service docker.io stop docker.io stop/waiting [email protected]:~$
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
[email protected]:~$ 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”
3. export the go path using the following command.
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.
6. Verify the swarm setup by executing the swarm version command.
[email protected]:~$ swarm --version swarm version 0.0.1 [email protected]:~$
8. You can view all the available swarm options by just executing the swarm command.
[email protected]:~$ 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 [email protected]:~$
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.
[email protected]:~$ swarm create 73f8bc512e94195210fad6e9cd58986f [email protected]:~$
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>
[email protected]:~$ swarm join --discovery token://73f8bc512e94195210fad6e9cd58986f --addr=184.108.40.206: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>
[email protected]:~$ swarm list --discovery token://73f8bc512e94195210fad6e9cd58986f 220.127.116.11:2375 18.104.22.168:2375 22.214.171.124:2375 [email protected]:~$