Docker Swarm – An Introduction

photo_1448965531_temp

Overview

In previous tutorials, we have looked at how we can deploy dockerized applications on a single machine. In this post, we will be deploying applications on different machines/nodes using Docker Swarm.

Prerequisites

docker – See how to install docker here.

vagrant – See how to install vagrant here.

What is Docker Swarm?

Docker Swarm is a cluster management and orchestration tool for Docker. It is native to Docker and therefore does not need to be installed separately. Below are some of the features of Docker Swarm:

  • Load balancing
  • Rolling Updates
  • Service discovery
  • Scaling
  • Desired state reconciliation

To see the complete list of features click here.

Creating Our First Swarm

To create our first swarm we will be using the spring pet clinic application. The application will be deployed across four(4) virtual machines/nodes created using vagrant. The schematic diagram below shows the architecture of the system:

Dock Swarm Introduction (2)

Initializing the Nodes

To initialize the nodes/virtual machines perform the steps below:

  1. Clone the docker-swarm-mode repository:
git clone https://github.com/denisdbell/docker-swarm-mode.git

2. Navigate to the docker-swarm-mode folder and type the following command:

vagrant up

When this command is executed for the first time it takes several minutes to run. This is primarily because the “boxcutter/ubuntu1610” image is being downloaded. Subsequent executions will be much faster since the image would have already been downloaded.

After the vagrant up command has run successfully, access the Master node by executing the following command:

vagrant ssh Master

Initializing Docker Swarm Mode On The Master Node

Swarm mode is not enabled by default. To enable it we must execute the following command:

docker swarm init --advertise-addr 192.168.99.201

To  verify that the swarm is initialized execute the following command:

docker info | grep Swarm

You should see the following displayed:

Swarm: active

The Join Command

When the command to initialize the swarm is executed (docker swarm init) a join command is produced. This join command will allow worker nodes to be added to the swarm. See an example of the join command below:

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-5cyt5f348qxs3qye0uy9r8p5oqw7qb2vndzv3rnu7geneskrkz-11mggfs4apy7enmqbnz7706uv 192.168.99.201:2377

Save this command as we will be using it in the next section.

Adding Worker Nodes To The Swarm

To add workers to the swarm is simple, let’s start by adding Worker1 by using the following steps:

1. SSH into the Worker1 node:

vagrant ssh Worker1

2. Execute the join command

Note: The command below is just an example, you will have to use your own join command.

docker swarm join --token SWMTKN-1-5cyt5f348qxs3qye0uy9r8p5oqw7qb2vndzv3rnu7geneskrkz-11mggfs4apy7enmqbnz7706uv 192.168.99.201:2377

You should receive the following message:

This node joined a swarm as a worker.

3. Exit the Worker1 node session by typing exit.

Repeat the steps above for Worker2 and Worker3 to add them to the swarm.

To verify that the node has added to the swarm you will need to perform the following steps:

1. SSH into the Manager node

vagrant ssh Manager

2. Execute the following commands to view all nodes:

docker node ls

We can see from the output below that Worker1 has been added to the swarm.

ID                                               HOSTNAME  STATUS    AVAILABILITY     MANAGER STATUS
lysh71mckggg12rzxit2c6kgs  Worker1       Ready     Active
sqzl73fx8uh28osk5qx45qle3 * Master        Ready    Active                          Leader

Creating Our First Service

In docker swarm, a service is used to deploy containers. A service is essentially the definition of the desired state of the containers in a swarm. The first service that will be created is the visualizer service. It will be used to view the nodes and containers within our swarm. Follow the steps below to create the visualizer service:

1. SSH into the Manager node

vagrant ssh Manager

2. Run the following command to create the visualizer service:

docker service create 
--name=viz  
--publish=9090:8080/tcp 
--constraint=node.role==manager  
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock  
dockersamples/visualizer

The name of the service is set to viz using the –name=viz parameter. The internal port  8080 is mapped to the external port 9090 using the –publish=9090:8080/tcp parameter.  The service will only be created on the Manager node using constraint=node.role==manager parameter.

To verify that the service has been created type the following command:

docker service ls

You should see the following output below:

ID                     NAME MODE         REPLICAS IMAGE                                          PORTS
f6dqxdgr4bzr viz      replicated  0/1             dockersamples/visualizer:latest *:9090-8080/tcp

To view the containers that are associated with the service type the following command:

 docker service ps viz

Navigate to http://192.168.99.201:9090 to view the visualization service. You should see the following page displayed, see below:

viz

You can see that the viz service is on the Master node. We currently have no services on the Worker nodes.

Load Balancing

Even though the viz service is deployed on the Master node it can still be accessed from the worker nodes. This is because of load balancing feature of Docker Swarm. Navigate to http://192.168.99.211:9090 (Worker1 node ip address)  and you will be able to access the visualization service.

Creating The Pet Clinic Service

To deploy the pet clinic service, perform the following steps:

1. SSH into the Manager node

vagrant ssh Manager

2. Execute the following command to create the pet clinic service:

docker service create --name petclinic --publish 9000:8080 denisdbell/petclinic:1.5.1
Navigate to http://192.168.99.201:9000 to view the pet clinic application.
To view the current state of the swarm navigate to http://192.168.99.201:9090 , and you should see one instance of the pet clinic application deployed:
viz2

Scaling The Petclinic Application

To scale the application to all four(4) nodes we can use the following command:

 docker service scale petclinic=4

Navigate to http://192.168.99.201:9090 and you should  see the pet clinic application deployed to all four nodes:

viz3.png

This concludes the first docker swarm tutorial. For details on docker swarm, I recommend visiting the docker swarm documentation.

Advertisements

3 thoughts on “Docker Swarm – An Introduction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s