<img alt="" src="https://secure.bomb5mild.com/193737.png" style="display:none;">

Turbonomic Blog

Getting Started with Docker Swarm: Part 2

Posted by Steven Haines on Jul 20, 2016 1:02:07 PM
Find me on:

In our previous article, we started out with our introduction to Docker Swarm. As we left off, we have our token ready to go, so let’s start up our Swarm manager and take things from there.

Starting the Swarm Manager and Agents

First we need to start the Swarm manager and then we can create agents to join the Swarm cluster. Both activities are accomplished by launching the same “swarm” container we used in the previous step, but we’ll pass a different argument. The manager is already the "active" machine, so we can create the Swarm cluster with the following command:

$ docker run -d -p 3376:3376 -t -v ~/.docker/machine/machines/manager:/certs:ro swarm manage -H 0.0.0.0:3376
--tlsverify
--tlscacert=/certs/ca.pem
--tlscert=/certs/server.pem
--tlskey=/certs/server-key.pem
token://7c14cbf2a86ecd490a7ea7ae4b795a6b

We run the swarm container with the following configuration:

  • -d (or --detach): run the swarm container in background and print its container ID after it starts
  • -t: allocate a pseudo-TTY terminal output
  • -p: map port 3376 on the Docker Container to port 3376 on the Docker Host (your local laptop). This is the default port that the docker command expects to connect to
  • -v: mount the local volume (~/.docker/machine/machines/manager) on the container at the specified location (/certs) with read-only access (ro)
  • token: this is the discovery token that we creating the previous section

One challenge that I faced when setting this up was getting the certificates location correct. Depending on your version of Docker and your operating system, your certificates directory may be in a different location. As of this writing, on Macs, Docker creates a .docker folder in your home directory and when we created the manager virtual machine, it created its configuration in the following location: machine/machines/manager.

If you run into problems finding files such as server.key or server-key.pem during startup, then locate those files in your own Docker configuration and update your volume mounting accordingly.

With the manager running, the next step is to start the agents. We’ll need to change the active Docker machine to agent1, which can be accomplished by executing the following command:

$ eval $(docker-machine env agent1)

This command tells the docker client to send all docker commands to the Docker Engine running on the "agent1" machine. Now let's start agent1:

$ docker run -d swarm join --addr=$(docker-machine ip agent1):2376 token://7c14cbf2a86ecd490a7ea7ae4b795a6b
Unable to find image 'swarm:latest' locally
latest: Pulling from library/swarm
eada7ab697d2: Pull complete
afaf40cb2366: Pull complete
7495da266907: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:12e3f7bdb86682733adf5351543487f581e1ccede5d85e1d5e0a7a62dcc88116
Status: Downloaded newer image for swarm:latest
99c5ec703dc3230fcf769eb13e639079803ee36c33447a0290a2fb7ffe5e7952

This command, similar to the previous one, tells Docker to run the swarm container, but this time in "join" mode. We tell it to run in detached mode (-d) and pass it two arguments:

  • --addr: The address and port of the agent, which is used to advertise the presence of the agent to the manager
  • token: the discovery token that we created earlier and used to start the manager

Repeat this for the second agent:

$ eval $(docker-machine env agent2)
$ docker run -d swarm join --addr=$(docker-machine ip agent2):2376 token://7c14cbf2a86ecd490a7ea7ae4b795a6b
Unable to find image 'swarm:latest' locally
latest: Pulling from library/swarm
eada7ab697d2: Pull complete
afaf40cb2366: Pull complete
7495da266907: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:12e3f7bdb86682733adf5351543487f581e1ccede5d85e1d5e0a7a62dcc88116 Status: Downloaded newer image for swarm:latest
0b16ee511399c27d849c6a6c628822375c27755b14719b5295c9038f97ede72a

At this point, the manager and both agents should be running. Let's configure the docker client to connect to the Docker Swarm Manager and retrieve information about the environments. First, set your DOCKER_HOST environment variable to point to the Manager Docker Machine:

$ DOCKER_HOST=$(docker-machine ip manager):3376

The docker-machine ip command retrieves the IP address of the specified machine, which is manager in this case. Likewise, on Windows you can execute the SET DOCKER_HOST command to setup the environment. With the DOCKER_HOST environment set, we can now retrieve information about our Swarm cluster by executing the docker info command:

$ docker info
Containers: 2
Running: 2
Paused: 0
Stopped: 0
Images: 2
Server Version: swarm/1.2.2
Role: primary
Strategy: spread
Filters: health, port, containerslots, dependency, affinity, constraint
Nodes: 2
agent1: 192.168.99.101:2376
- ID: RDNQ:VD3I:AZPE:LSWW:7NND:XV7C:KHGH:5KR5:MZHG:4I7H:7RMU:XGQG
- Status: Healthy
- Containers: 1
- Reserved CPUs: 0 / 1
- Reserved Memory: 0 B / 1.021 GiB
- Labels: executiondriver=, kernelversion=4.4.8-boot2docker, operatingsystem=Boot2Docker 1.11.1 (TCL 7.0); HEAD : 7954f54 - Wed Apr 27 16:36:45 UTC 2016, provider=virtualbox, storagedriver=aufs
- Error: (none)
- UpdatedAt: 2016-05-22T19:03:35Z
- ServerVersion: 1.11.1
agent2: 192.168.99.102:2376
- ID: DXN7:FLLA:RMDW:HSPS:WT74:YM2I:CM3G:QBY7:FR7G:4WEO:LJ72:XB6L
- Status: Healthy
- Containers: 1
- Reserved CPUs: 0 / 1
- Reserved Memory: 0 B / 1.021 GiB
- Labels: executiondriver=, kernelversion=4.4.8-boot2docker, operatingsystem=Boot2Docker 1.11.1 (TCL 7.0); HEAD : 7954f54 - Wed Apr 27 16:36:45 UTC 2016, provider=virtualbox, storagedriver=aufs
- Error: (none)
- UpdatedAt: 2016-05-22T19:03:32Z
- ServerVersion: 1.11.1
Plugins:
Volume:
Network:
Kernel Version: 4.4.8-boot2docker
Operating System: linux
Architecture: amd64
CPUs: 2
Total Memory: 2.042 GiB
Name: 77d61b0fe67f
Docker Root Dir:
Debug mode (client): false
Debug mode (server): false
WARNING: No kernel memory limit support

This output shows two running two containers (Swarm containers running in "join" mode) for our two agents and those agents are healthy. Note that these are the Docker Machine containers and not our custom application containers. We can view the running custom application containers by executing the docker ps command:

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

As expected, there are no custom containers running.

Running Docker Containers

With three Docker machines running, let’s start a Docker container. Recall that we made our manager the “active” machine, so the docker command line tool will send commands to the manager. Execute the following command to start an instance of the Nginx container:

$ docker run -d -p 80:80 nginx cc6d627873f7b33f910129fafdcc5c544048cc864ef5433e667afc9a88632931

This command starts a container from the image nginx:latest, running in detached mode, and binds port 80 of the container to port 80 on the Docker host (Docker machine.) Now if we execute a docker ps command we’ll see where this container is running:

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc6d627873f7 nginx "nginx -g 'daemon off" 28 seconds ago Up 27 seconds 192.168.99.101:80->80/tcp, 443/tcp agent1/goofy_bassi

This output shows that the container is running on agent1. We can open a browser to agent1 and confirm that the container is running:

http://192.168.99.101/

You should see a screen like figure 2.

15-nginx

Let’s start one more instance to see how Docker Swarm deploys the second instance:

$ docker run -d -p 80:80 nginx
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
737d5d37d5a6 nginx "nginx -g 'daemon off" About a minute ago Up About a minute 192.168.99.102:80->80/tcp, 443/tcp agent2/condescending_galileo
cc6d627873f7 nginx "nginx -g 'daemon off" 3 minutes ago Up 3 minutes 192.168.99.101:80->80/tcp, 443/tcp agent1/goofy_bassi

The docker ps command shows that we now have one instance running on agent1 and one running on agent2. Swarm uses an algorithm that determines the number of containers running on each agent and deploys the new container to the agent with the least number of running containers. In this case agent1 has one container and agent2 has no containers so Swarm sends the new container to agent2.

You can test the second instance by opening a browser to the following URL:

http://192.168.99.102/

Congratulations, you now have a Swarm manager, two agents, and two containers running on those agents.
Cleaning up

Once you’re finished you can clean up your environment with the following steps:

1. Stop your Docker containers by specifying the beginning of their container ids:

$ docker stop 737
$ docker stop cc6

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

2. Stop your Docker machines using the docker-machine stop command:

$ docker-machine stop agent1
Stopping "agent1"...
Machine "agent1" was stopped.
$ docker-machine stop agent2
Stopping "agent2"...
Machine "agent2" was stopped.
$ docker-machine stop manager
Stopping "manager"...
Machine "manager" was stopped.


$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS agent1 - virtualbox Stopped Unknown agent2 - virtualbox Stopped Unknown default - virtualbox Stopped Unknown manager - virtualbox Stopped Unknown

Now you can safely shut down Docker.

Conclusion

This article introduced Docker Swarm, Docker’s native clustering solution, and walked through creating a local Docker Swarm cluster. Docker Swarm provides the basic functionality to manage agents, or machines running the Docker Engine, and deploy Docker containers across a set of agents.

When running Docker in AWS, Amazon ECS is probably the way to go, but when running in your local data center or another private cloud, Swarm is a very powerful option available to you.

Topics: Containers

Subscribe Here!

Recent Posts

Posts by Tag