Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Container Orchestration with Docker Swarm

The power of containers shines brightest when you build large systems made up of many machines running lots of interacting containers. That's were Docker Swarm comes -- arguably the simplest orchestration solution for Docker containers and it is built into the Docker engine itself.


advertisement

Containers eat the DevOps world. Docker is leading the pack with the ubiquitous Docker engine runtime and a plethora of related tools. In this article, I provided some basic information about Docker. A single container is, however, not very useful. Even a collection of containers on the same machine has limited utility. The power of containers shines brightest when you build large systems made up of many machines running lots of interacting containers. Orchestrating all these containers is a difficult problem. Kubernetes and Mesos, as well as most cloud platforms, provide solutions. But Docker, as a company, wants in on the orchestration cake too. That's were Docker Swarm comes in. It is arguably the simplest orchestration solution for Docker containers and it is built into the Docker engine itself.

I assume you have installed Docker on your machine. To create a local cluster called "swarm-cluster" run the following command:


> docker-machine create -d virtualbox swarm-cluster

Running pre-create checks...
(swarm-cluster) No default Boot2Docker ISO found locally, downloading the latest release...
(swarm-cluster) Latest release for github.com/boot2docker/boot2docker is v1.12.3
(swarm-cluster) Downloading C:\Users\the_g\.docker\machine\cache\boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.12.3/boot2docker.iso...
(swarm-cluster) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(swarm-cluster) Copying C:\Users\the_g\.docker\machine\cache\boot2docker.iso to C:\Users\the_g\.docker\machine\machines\swarm-cluster\boot2docker.iso...
(swarm-cluster) Creating VirtualBox VM...
(swarm-cluster) Creating SSH key...
(swarm-cluster) Starting the VM...
(swarm-cluster) Check network to re-create if needed...
(swarm-cluster) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe env swarm-cluster


Now, you need to load the configuration into your shell.

On Windows:


& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env swarm-cluster | Invoke-Expression

On Mac/Linux:


 eval "$(docker-machine env local)"

Generating a Discovery Token

The next step is generating a discovery token. The containers that participate in the cluster need to be able to discover each other.

You do this by running the "swarm" image from the Docker registry.


> docker run swarm create

Unable to find image 'swarm:latest' locally
latest: Pulling from library/swarm

220609e0bc51: Pull complete
b54bf338fe2f: Pull complete
d53aac5750d5: Pull complete
Digest: sha256:c9e1b4d4e399946c0542accf30f9a73500d6b0b075e152ed1c792214d3509d70
Status: Downloaded newer image for swarm:latest
e18fe8919e3c504e7f00284879fa9933

The last line is our token: e18fe8919e3c504e7f00284879fa9933. This discovery token is provided by Docker's hosted discovery service. You may use other discovery back ends such as etcd, consul or zoo keeper.

Now, armed with this token we can add start building out cluster. First, we need a master. The master is responsible for scheduling containers to run on the cluster nodes (AKA agents).


> docker-machine create \
          -d virtualbox \
          --swarm \
          --swarm-master \
          --swarm-discovery token://e18fe8919e3c504e7f00284879fa9933 \
          swarm-master
          
Running pre-create checks...
Creating machine...
(swarm-master) Copying C:\Users\the_g\.docker\machine\cache\boot2docker.iso to C:\Users\the_g\.docker\machine\machines\swarm-master\boot2docker.iso...
(swarm-master) Creating VirtualBox VM...
(swarm-master) Creating SSH key...
(swarm-master) Starting the VM...
(swarm-master) Check network to re-create if needed...
(swarm-master) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe env swarm-master          

Let's check our cluster status:


11:00:46 [G] docker-swarm> docker-machine ls
NAME            ACTIVE   DRIVER       STATE     URL                         SWARM                   DOCKER    ERRORS
swarm-cluster   *        virtualbox   Running   tcp://192.168.99.100:2376                           v1.12.3
swarm-master    -        virtualbox   Running   tcp://192.168.99.101:2376   swarm-master (master)   v1.12.3

As you can see we have two virtual machines, one of them is the master.

Adding Nodes

It's time to add nodes. The command for adding a node is very similar to creating the master. The only difference is that you don't specify the --swarm-master flag:


docker-machine create \
  -d virtualbox \
  --swarm \
  --swarm-discovery token://e18fe8919e3c504e7f00284879fa9933 \
  agent-smith
  
Running pre-create checks...
Creating machine...
(agent-smith) Copying C:\Users\the_g\.docker\machine\cache\boot2docker.iso to C:\Users\the_g\.docker\machine\machines\agent-smith\boot2docker.iso...
(agent-smith) Creating VirtualBox VM...
(agent-smith) Creating SSH key...
(agent-smith) Starting the VM...
(agent-smith) Check network to re-create if needed...
(agent-smith) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe env agent-smith  

You can name your nodes however you like, as long as the names are unique. I chose "agent-smith" for this node. Let's create another agent called "agent-007"


docker-machine create \
  -d virtualbox \
  --swarm \
  --swarm-discovery token://e18fe8919e3c504e7f00284879fa9933 \
  agent-007

Checking the cluster now we see the two new nodes:


> docker-machine ls
NAME            ACTIVE   DRIVER       STATE     URL                         SWARM                   DOCKER    ERRORS
agent-007       -        virtualbox   Running   tcp://192.168.99.103:2376   swarm-master            v1.12.3
agent-smith     -        virtualbox   Running   tcp://192.168.99.102:2376   swarm-master            v1.12.3
swarm-cluster   *        virtualbox   Running   tcp://192.168.99.100:2376                           v1.12.3
swarm-master    -        virtualbox   Running   tcp://192.168.99.101:2376   swarm-master (master)   v1.12.3

The cluster is up and running. Now you can run containers on the cluster and the Swarm will make sure to run them on one of the nodes that has enough capacity. Here it is running the hello-world container that just prints "Hello from Docker!" and some help text:


docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Give Docker Swarm a try — it is a relatively painless way to get into the world of container orchestration. If you want to graduate to the big leagues take a look at Kubernetes.



   
Gigi Sayfan is the chief platform architect of VRVIU, a start-up developing cutting-edge hardware + software technology in the virtual reality space. Gigi has been developing software professionally for 21 years in domains as diverse as instant messaging, morphing, chip fabrication process control, embedded multi-media application for game consoles, brain-inspired machine learning, custom browser development, web services for 3D distributed game platform, IoT/sensors and most recently virtual reality. He has written production code every day in many programming languages such as C, C++, C#, Python, Java, Delphi, Javascript and even Cobol and PowerBuilder for operating systems such as Windows (3.11 through 7), Linux, Mac OSX, Lynx (embedded) and Sony Playstation. His technical expertise includes databases, low-level networking, distributed systems, unorthodox user interfaces and general software development life cycle.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date