Ajeet Raina I am a Docker Captain, ARM Innovator & Docker Bangalore Community Leader. I am a maintainer of Collabnix blogging site. I work for Redis and a primary maintainer of https://developer.redis.com. I run Collabnix Community Slack with over 6500+ audience . We have built a popular projects like Docker Labs, KubeLabs, KubeTools and DockerTools. You can follow me on Twitter(@ajeetsraina) & GitHub(@ajeetraina)

Getting Started with Docker Swarm

4 min read

Docker Swarm is 1 year old now. Docker Inc. during DockerConEU, December 2014 made the announcement of Docker Swarm. A Docker Swarm is a clustering manager for Docker and Docker Machine, a CLI tool for provisioning Docker hosts. Docker Inc. intented to provide a complete and integrated solution for running containers and not allowing themselves to be restricted to only providing the Docker engine.

Docker Swarm is the native clustering tool for Docker. Swarm uses the standard Docker API, i.e., containers can be launched using normal docker run commands and Swarm will take care of selecting an appropriate host to run the container on. What it actually means that the other tools that use the Docker API—such as Compose and bespoke scripts—can use Swarm without any changes and take advantage of running on a cluster rather than a single host.

 

Swarm_Arch

The basic architecture of Swarm is fairly straightforward:

i. Each host runs a Swarm agent and one host runs a Swarm manager (on small test clusters this host may also run an agent).

ii. The manager is responsible for the orchestration and scheduling of containers on the hosts.

ii. Swarm can be run in a high-availability mode where etcd,Consul, or ZooKeeper is used to handle failover to a back-up manager.

There are several different methods for how hosts are found and added to a cluster, which is known as discovery in Swarm. By default, token-based discovery is used, where the addresses of hosts are kept in a list stored on the Docker Hub.

 

Swarm_Setup

Let’ start with practical implementation of Docker Swarm. I quickly turned on 4 node cluster on my Google Cloud Engine, 1 Swarm Master Node, 2 Agent Nodes and 1 Swarm Manager Node to manage the overall cluster. Here is my environment details:

 

Swarm_Cluster

 

Setting up Swarm Master Node:

Ensure that Docker 1.9.1 is installed on all the nodes of Docker swarm cluster. You can follow the below command to update all the Docker Hosts to the latest version:

wget -qO- https://get.docker.com/ | sh
[Remember its capital-O and not zero]

Processing triggers for systemd (225-1ubuntu9) …
Processing triggers for man-db (2.7.4-1) …
Setting up docker-engine (1.9.1-0~wily) …
Installing new version of config file /etc/bash_completion.d/docker …
Installing new version of config file /etc/init.d/docker …
Installing new version of config file /etc/init/docker.conf …
Processing triggers for ureadahead (0.100.0-19) …
Processing triggers for systemd (225-1ubuntu9) …
+ sh -c docker version
Client:
Version: 1.9.1
API version: 1.21
Go version: go1.4.2
Git commit: a34a1d5
Built: Fri Nov 20 13:20:08 UTC 2015
OS/Arch: linux/amd64
Server:
Version: 1.9.1
API version: 1.21
Go version: go1.4.2
Git commit: a34a1d5
Built: Fri Nov 20 13:20:08 UTC 2015
OS/Arch: linux/amd64
If you would like to use Docker as a non-root user, you should now consider
adding your user to the “docker” group with something like:
sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!

Starting Docker Daemon

root@dockerhost-1 ~]# docker -H tcp://0.0.0.0:2375 -d &
[1] 11516
[root@dockerhost-1 ~]# Warning: ‘-d’ is deprecated, it will be removed soon. See usage.
WARN[0000] please use ‘docker daemon’ instead.
WARN[0000] /!\ DON’T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON’T KNOW WHAT YOU’RE DOING /!\
INFO[0000] Listening for HTTP on tcp (0.0.0.0:2375)
ERRO[0000] WARNING: No –storage-opt dm.thinpooldev specified, using loopback; this configuration is strongly disco
uraged for production use
INFO[0000] [graphdriver] using prior storage driver “devicemapper”
INFO[0000] Option DefaultDriver: bridge
INFO[0000] Option DefaultNetwork: bridge
WARN[0000] Running modprobe bridge nf_nat br_netfilter failed with message: modprobe: WARNING: Module br_netfilter
not found.
, error: exit status 1
INFO[0000] Firewalld running: true
INFO[0000] Loading containers: start.
INFO[0000] Loading containers: done.
INFO[0000] Daemon has completed initialization
INFO[0000] Docker daemon commit=a01dc02/1.8.2 execdriver=native-0.2 graphdriver=dev
icemapper version=1.8.2-el7.centos
[root@dockerhost-1 ~]#

Setting up Docker Swarm:

root@docker-1:~# docker run –rm swarm create
7733f838d176809cb2f2d24eb34ce78c

It created a token ID which is a heart of Docker Swarm cluster configuration.

Setting up Swarm Agent Node 1:[10.240.0.3]

Ensure that Docker 1.9.x is installed on Agent Node 1. Also, docker daemon need to be running following the similar steps during the master node setup.

#docker run -d swarm join –addr=10.240.0.3:2375 token://7733f838d176809cb2f2d24eb34ce78c

560e58a76ef235c8d74fffb2c680149111b8bbe687f1e0e164cd7c05dad59f33

Setting up Swarm Manager Node :[10.240.0.5]

root@docker-3:~# docker run -d -p 7000:2375 swarm manage token://7733f838d176809cb2f2d24eb34ce78c
b26a2bbb336e26e0cd6ac5d61b90d7a9d9f74a2d072f573b6890bd9b0f6e470f

Please remember that 7000 is swarm manager port which is being set.

Now you can see the swarm cluster details:

root@docker-3:~# docker -H tcp://10.240.0.5:7000 info
Containers: 0
Images: 2
Storage Driver:
Role: primary
Strategy: spread
Filters: health, port, dependency, affinity, constraint
Nodes: 1
docker-1: 10.240.0.2:2375
+ Status: Healthy
+ Containers: 0
+ Reserved CPUs: 0 / 1
+ Reserved Memory: 0 B / 3.794 GiB
+ Labels: executiondriver=native-0.2, kernelversion=4.2.0-18-generic, operatingsystem=Ubuntu 15.10, storagedriver
=devicemapper
Execution Driver:
Kernel Version:
Operating System:
CPUs: 1
Total Memory: 3.794 GiB
Name: b26a2bbb336e
ID:
Http Proxy:
Https Proxy:
No Proxy:

As just one node has been added to the cluster node, it shows just Node-1 added to the swarm cluster.

Setting up Swarm Agent Node 2 :[10.240.0.4]

root@docker-2:~# docker run -d swarm join –addr=10.240.0.4:2375 token://7733f838d176809cb2f2d24eb34ce78c
fe5b81d57f6394db655d15ce097f8ca537d6c74c2de6a477ac38544a7db0a5d8
root@docker-2:~#

Finally, you can list out all the nodes added to the swarm cluster  by running the below command on Swarm Manager node as shown:
#docker run –rm swarm list token://7733f838d176809cb2f2d24eb34ce78c

10.240.0.4:2375
10.240.0.2:2375

Wow !!! Here you go…Your Multi-Node Swarm Cluster is Ready to rock !!!

Let’s create a container and run it through swarm Manager:

sudo docker -H tcp://10.0.1.61:5001 run -dt –name swarm-test nginx /bin/sh

root@docker-1:~# docker images
INFO[0999] GET /v1.18/images/json
INFO[0999] +job images()
INFO[0999] -job images() = OK (0)
REPOSITORY TAG IMAGE ID CREATED VIRTU
AL SIZE
nginx latest 813e3731b203 4 days ago 133.8
MB
centos latest 14dab3d40372 5 days ago 194.7
MB
swarm latest e9ff33e7e5b9 11 days ago 17.15
MB
ajeetraina/dell-syscfg v1.0 d121b6e6dba4 12 weeks ago 1.449
GB
tduzan/docker-omsa latest ffcbdafb4aa6 16 months ago 806.4
MB
root@docker-1:~#

Lets see what containers are running on each swarm agent machines:

root@docker-3:~# docker -H tcp://10.240.0.5:7100 ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
d0ee6dad4b39 nginx:latest “/bin/sh” 58 minutes ago Up 58 minutes 80/tcp,
443/tcp docker-1/swarm-test
cd9bc8c96e45 ajeetraina/dell-syscfg:v1.0 “/bin/bash” About an hour ago Up About an hour
docker-1/jolly_curie
d0bcc8da090e bad926a6fb50:latest “/bin/bash” About an hour ago Up About an hour
docker-2/hopeful_tesla
root@docker-3:~#

What it means? Sitting on Swarm Manager Node you can simply create containers and interestingly you don’t need to worry about what node shall this container sit on. Your Swarm Cluster is intelligent enough to take care of right resource for your container. Happy Swarming !!!

 

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina I am a Docker Captain, ARM Innovator & Docker Bangalore Community Leader. I am a maintainer of Collabnix blogging site. I work for Redis and a primary maintainer of https://developer.redis.com. I run Collabnix Community Slack with over 6500+ audience . We have built a popular projects like Docker Labs, KubeLabs, KubeTools and DockerTools. You can follow me on Twitter(@ajeetsraina) & GitHub(@ajeetraina)

What is a Dockerfile – A Step-By-Step Guide

A Dockerfile is a text file which contains a series of commands or instructions. These instructions are executed in the order in which they...
Ajeet Raina
21 min read

Docker Desktop for Linux,  Docker Extensions and more with…

Docker Desktop 4.8.2 is now available for download for Mac, Windows and Linux. Here are the major highlights: Docker Desktop for various Linux distributions made available for the first...
Ajeet Raina
1 min read

30 Replies to “Getting Started with Docker Swarm”

  1. Hello! Someone inside my Myspace group shared this website
    around so I got to give it a look. I’m definitely loving the
    info. I’m bookmarking and are tweeting this to my followers!

    Outstanding blog and wonderful design.

  2. Very nice post. I just discovered your blog and
    wanted to mention that I’ve truly loved browsing your blog posts.
    Whatever the case I am going to be subscribing on the feed and
    I am hoping you write once more immediately!

  3. I do believe all the concepts you’ve offered within your post.

    They’re very convincing and will certainly work. Nonetheless, the posts are too
    brief for starters. May just you please prolong
    them a little from subsequent time? Thanks to the
    post.

  4. Pretty portion of content. I just stumbled upon your blog and in accession capital to claim that I
    get actually loved account your blog posts. Any way
    I’ll be subscribing on your augment or even I achievement you get
    right of entry to persistently fast.

  5. Howdy! I understand this is kind of off-topic but I needed to ask.
    Does running a well-established blog like yours require a massive amount work?
    I’m completely new to writing a blog however I do
    write in my diary on a daily basis. I’d like to start a blog so
    I can easily share my own experience and feelings online. Please let me know if you have any
    suggestions or tips for brand new aspiring blog owners.
    Thankyou!

  6. This is the perfect website for anyone who hopes to understand this topic. You realize a whole lot its almost hard to argue with you (not that I personally will need to…HaHa). You certainly put a new spin on a topic which has been written about for decades. Great stuff, just great!|

  7. Thank you for some other informative site. Where else may just I get that type of info written in such an ideal method? I’ve a project that I’m simply now operating on, and I have been at the glance out for such information.|

  8. I am extremely impressed together with your writing abilities and also with all
    the structure in your weblog. Is this a paid theme or have you modify it yourself?
    In any case keep the excellent premium quality writing, it can be uncommon to search a
    nice blog similar to this one nowadays..

  9. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but instead of that, this is wonderful blog. A fantastic read. I will definitely be back.|

  10. Fantastic website you have here but I was wanting to know if you knew of any user discussion forums that cover the same topics discussed in this article? I’d really like to be a part of community where I can get feedback from other experienced people that share the same interest. If you have any suggestions, please let me know. Many thanks!

  11. It’s a pity you don’t use a donate button! I’d certainly donate to the
    brilliant blog! I suppose for now i’ll accept book-marking and adding your RSS feed to my Google account.
    I look forward to completely new updates and definately will
    talk about this site with my Facebook group. Talk soon!

Leave a Reply