devxlogo

Container Orchestration with Docker Swarm

Container Orchestration with Docker Swarm

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-clusterRunning 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	he_g.dockermachinecacheoot2docker.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	he_g.dockermachinecacheoot2docker.iso to C:Users	he_g.dockermachinemachinesswarm-clusteroot2docker.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 FilesDockerDockerResourcesindocker-machine.exe env swarm-cluster

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

On Windows:

& "C:Program FilesDockerDockerResourcesindocker-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 createUnable to find image 'swarm:latest' locallylatest: Pulling from library/swarm220609e0bc51: Pull completeb54bf338fe2f: Pull completed53aac5750d5: Pull completeDigest: sha256:c9e1b4d4e399946c0542accf30f9a73500d6b0b075e152ed1c792214d3509d70Status: Downloaded newer image for swarm:lateste18fe8919e3c504e7f00284879fa9933

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	he_g.dockermachinecacheoot2docker.iso to C:Users	he_g.dockermachinemachinesswarm-masteroot2docker.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 FilesDockerDockerResourcesindocker-machine.exe env swarm-master          

Let’s check our cluster status:

11:00:46 [G] docker-swarm> docker-machine lsNAME            ACTIVE   DRIVER       STATE     URL                         SWARM                   DOCKER    ERRORSswarm-cluster   *        virtualbox   Running   tcp://192.168.99.100:2376                           v1.12.3swarm-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	he_g.dockermachinecacheoot2docker.iso to C:Users	he_g.dockermachinemachinesagent-smithoot2docker.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 FilesDockerDockerResourcesindocker-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 lsNAME            ACTIVE   DRIVER       STATE     URL                         SWARM                   DOCKER    ERRORSagent-007       -        virtualbox   Running   tcp://192.168.99.103:2376   swarm-master            v1.12.3agent-smith     -        virtualbox   Running   tcp://192.168.99.102:2376   swarm-master            v1.12.3swarm-cluster   *        virtualbox   Running   tcp://192.168.99.100:2376                           v1.12.3swarm-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-worldHello 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 bashShare images, automate workflows, and more with a free Docker Hub account: https://hub.docker.comFor 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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist