Recently I was asked to do some research on how to deploy a java application in Azure. While you could fire up and configure your own VM or create a Tomcat App Service, the option that intrigued me the most was Docker For Azure. Based on my experiences, I feel that introducing Docker Swarm Mode first and then diving into Docker For Azure will make the most sense. (NOTE: If you are completely new to Docker, go here first!).
What is Swarm Mode
Example diagram of a swarm cluster – See Docker Docs
Swarm Mode is Docker’s container management and orchestration tool. This allows admins and software developers to create and manage multiple Docker containers running on one or more nodes. Because this is native to Docker, you can use the Docker API to run native tools such as Docker Compose. Theoretically, the software can handle up to 30,000 containers and clusters of up to 1,000 nodes, without performance issues.
How services work – See Docker Docs
Node – A machine running the docker engine. In most cases, this is a VM but can be a stand alone server.
Service – The definition of the tasks to execute on the nodes. When creating a service, you specify what container image to use and any commands you wish to run on it.
Task – The Docker container and the commands to run inside the container. With Swarm Mode, you can have one or more tasks (e.g. – multiple web server containers) to distribute the load of work being done.
Swarm – A cluster of docker nodes. When initializing a swarm, you will create one or more “Manager” nodes and one or more “Slave” nodes. Managers are responsible for distributing tasks within the cluster based on what nodes are available, while workers receive and run tasks.
Setting Up Swarm Mode
While you can also install/configure Docker Swarm Mode on Linux and Macs (using Docker Machine), the following example is a walk through on setting up Swarm Mode locally on a Windows 10 Pro machine using Docker 1.13 Hyper-V (recommended) to create VM’s. Note: Docker 1.12 is the lowest version required to run Swarm Mode.
- Install Docker for Windows. This install package has everything you need to get going to run Docker and Swarm Mode commands via CLI.
- Create a virtual switch. This is needed for your VM’s to be created properly and for your nodes to be able to talk to one another.
- Open the Hyper-V Manager. You should be able to find this by opening your start menu and typing “Hyper-V”.
- In the right Actions menu, click on “Virtual Switch Manager…“. In the new modal, click on “New virtual network switch” and choose External.
- Enter in a desired name. For Connection type, choose the network adapter you use, and check “Allow management OS to share this network adapter“.
- Create your VM’s. We will be creating 3 VM’s (1 Manager and 2 Worker nodes)
- Open a windows command prompt and run as Administrator (This prevents Hyper-V pre-check errors during your VM creation). To create a VM using Hyper-V, run the following docker machine command in the following format: > docker-machine create -d hyperv --hyperv-virtual-switch ""
- For this example, I ran the following 3 commands:
> docker-machine create -d hyperv --hyperv-virtual-switch "PrimaryVirtualSwitch" manager1
> docker-machine create -d hyperv --hyperv-virtual-switch "PrimaryVirtualSwitch" worker1
> docker-machine create -d hyperv --hyperv-virtual-switch "PrimaryVirtualSwitch" worker2
- Initialize your swarm
- Figure out the IP address allocated to each VM by running the following command:
> docker-machine ip <name-of-vm>
- SSH into the manager node and initialize swarm mode on the manager by running the following:
> docker-machine ssh <name-of-manager-vm>
> docker swarm init --advertise-addr <manager-ip>
- You should see output similar to the following if successful:
- Now that the manager node is up and running, you can join your worker nodes to the manager. SSH into each worker node, just like how you SSH’ed into the manager, and then join them to the swarm by running the command generated from the docker swarm init command.
Deploying Services To Your Swarm
At this point, you should have 3 nodes created and talking to one another. Let’s start deploying some things and seeing how Swarm Mode works.
- Deploying “Hello World”
- SSH back into the manager node. This needs to be done as Swarm Mode specific commands can only be run on the manager node. > docker-machine ssh
- Once in, you can deploy your first service using the following command. > docker service create --replicas 1 --name HelloWorld alpine ping docker.com
So let’s examine this command to understand what’s going on.
- docker service create – The main command to create a new service
- –replicas <unit> – This option tells the docker swarm to create a number of tasks. In our example above, we only want one task. The default is 1.
- –name <string> – This option is to give the service a name. In our example above, the service is named “HelloWorld”. If this is not specified, a random name will be given to the service.
- alpine – This is the name of the docker image we want to use. Alpine is a very light wieght a flavor of linux.
- ping docker.com – This is a command we want to run on the alpine task after it has been instantiated.
- You can check to see if your service(s) deployed successfully:
> docker service ls
- Tinkering With Your Service
- If you want to inspect your running service to get more details about it:
> docker service inspect --pretty HelloWorld
(You can substitute “HelloWorld” for whatever name you use for your service)
- At this point we only have one task running in our service. Let’s add a total of 4 tasks for our HelloWorld service:
> docker service scale HelloWorld=4
If you run “docker service ls”, you should now see 4/4 replicas instead of 1/1. So what exactly just happened?
- The HelloWorld service is defined as an alpine container running “ping docker.com”.
- The scale command tells the manager node to create 3 more tasks. In other words, it will create 3 more alpine containers running “ping docker.com.
- With Docker Swarm Mode, the manager decides which nodes are the most available and tries to evenly distribute the additional tasks across manager and worker nodes.
- Running the following command will show which nodes are running what tasks > docker service ps HelloWorld
- Let’s say you only want 3 tasks to run instead of 4. Just run the same command and with a 3 instead. Swarm Mode will take the last 3 deployed containers and remove them.
- Fault Tolerance
You should have tasks running across some or all of your nodes. What happens if one of the tasks stops working?
- At the current command prompt, stay ssh’ed into the manager node.
- Open another command prompt (remember to do so with Admin rights) and ssh into the worker node that’s running the other task.
- Once in, run “docker ps” to see the container id’s/names running on the node. Force the removal of a container by running:
> docker rm -f
- Go back to the other command prompt window and run “docker service ps HelloWorld”. You should see output similar to the following:
So what just happened?
- You force stopped/removed the container on the worker node.
- Because you told the swarm manager to have a certain number of tasks always up and running, it will re-deploy another container to replace the one that was just killed.
- It will again try to determine the best node to host the container. It’s fairly easy to determine which one is the redeployment because it will have been up and running for a shorter amount of time than the initially deployed/scaled nodes.
This concludes the basics on how to setup docker swarm mode and how it works. In part 2, I’ll go over how we can do the same thing in Azure.