Deploying Docker Swarm With Azure Container Service
Deploying Docker Swarm With Azure Container Service
Learn step-by-step how you can deploy Docker Swarm on Azure using the handy Azure Container Service.
Join the DZone community and get the full member experience.Join For Free
Docker Swarm is one of the most popular tools today for clustering and turning pools of Docker hosts into a single host. Through the Azure Container Service (ACS), the creation of Docker Swarm is simple, and becomes a time-saving and efficient option for bringing optimization and portability to your system.
Azure is one of the fastest growing infrastructure services today. It supports the ability to scale on demand, with customer support over 19 regions across the globe, as well as the ability to create hybrid environments, big data insights and more.
The Azure Container Service supports two different orchestrators for the cluster. Note that users must have an Azure account in order to get started.
- Docker Swarm uses the native Docker stack, so you can directly use Docker commands to deploy containers. The containers are deployed after connecting on Master using SSH, and then executing a Docker command. This process is described in more detail later in this article.
- DC/OS, a datacenter operating system, runs containers in different formats, including Docker images.
Step-by-Step: Creating a Docker Swarm Cluster
RSA Key Preparation
First, we need to hold an RSA key, which will be required on the following deployment steps.
Now, we will start with executing commands for generating keys. The first command after entering the terminal is: $ ssh-keygen
After clicking ENTER, you will be asked for a location for where to save the key.
SSH RSA Key Generation
If you’re a Windows user and want to point to a location somewhere inside the user’s folder, you’ll have to type the location (the part where we put
~/azure_keys/id_rsa). This should be something like
If you are a Linux/Mac user, you can type something like
~/azure_keys (where the ~ character points to a user’s location). On some Linux terminal emulators for Windows, such as Cygwin, the tilde character does the same thing.
After clicking Enter again, you will be asked for a passphrase for this key. If you don’t need the passphrase, just click Enter again; otherwise, enter your passphrase for this key. You will then get a notification about successfully saved private/public key pairs, which you can see in the specified location.
Docker Swarm Cluster Deployment
Enter your Azure account portal, and click on the + (new) button, and search for container.
Azure’s User Portal
Click Azure Container Service. In the next view, Azure Container Service will show as the first item. After clicking the Create button, the views for creating Docker Swarm will appear.
Azure Container Service in the Search List
After clicking Create, we can start with filling in the Basics configuration settings. The first field is the username of the administrator for virtual machines inside the Docker Swarm cluster. This information should be user-specific.
Docker Swarm Basics Configuration
The second field is the SSH public key, which we created in the first step. Select the generated files that have the extensions .PUB, open them in your text editor, and paste them in the area just below the SSH public key label.
Next, select your subscription, and create a new resource group for your Docker Swarm cluster with the name you specified. In the last field for Basics configuration, select the location of the data center. In our case we selected West US, but in the drop-down you have over 15 Azure regions to choose from.
In the next step, you have to provide configure the Orchestrator, which is Docker Swarm.
Go to Service Settings, where you’ll need to provide the number of agents (in our case, we will use 2). This number can be as high as 100. For the number of masters, we will use 1, and for the master and agent DNS prefix, we will use a meaningful term, dockerswarm-.
After clicking OK, you will see the Summary view:
Next, click OK and you will be transferred to the last step, Buy. Here you can pass the validation by clicking once more on the Purchase button. After clicking Purchase, you will be redirected to your home page (the dashboard) where you will see that your Docker Swarm cluster is awaiting deployment.
Dashboard and Waiting for Docker Swarm Deployment
Deploying Docker Swarm takes some time after which you will see the Docker Swarm resources on the dashboard. Click on any of them to see details of the deployed Docker Swarm cluster.
Connect to the Master
Once you have created and deployed Docker Swarm, you will need to connect to the Master to be sure that everything is where it should be. Click on the Docker Swarm resource shown on the dashboard. Select the Deployments item on the left menu, under the Settings section.
Under the Deployments entries, select the last one, which is actually the first entry in the list. On the right side, you will find information about the deployment. Here you can find the DNS of the master as the command for connecting via SSH to the master. We will copy the command from the field - which is labeled SSHMASTER0 - and use it in the terminal.
SSH command to connect to the master:
ssh <DNS_FROM_FIELD> -A -p 2200 -i <PUB_FILE_LOCATION>
The above command is constructed when you click on the Copy icon next to the text box, where the part with argument
-i <PUB_FILE_LOCATION> is added. This is added because sometimes users have several pairs of private/public keys. They aren’t usually stored in the default SSH folder of UNIX based OSes, so we want to avoid any conflicts and generate the pair without interfering with default system configurations.
Once successfully connected with the master, we want to get information about the cluster. Before doing that, we need the IP address of the master. We will now need to click on the See More link inside the resource box of the Docker Swarm cluster. When we click on Swarm master virtual machines and then select the Network Interfaces on the left side menu of the Swarm master detail view, we will notice the IP address in the result table. So, head back to the terminal and then type the command:
docker -H 172.16.0.5:2375 info
The cluster info output should look something like this:
Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Role: primary Strategy: spread Filters: health, port, dependency, affinity, constraint Nodes: 2 swarm-agent-XXXXXXXX: 10.0.0.4:2375 Status: Healthy …….. swarm-agent-YYYYYYYY: 10.0.0.5:2375 Status: Healthy …..
Using Azure CLI
Before moving onto commands for creating a cluster, you have to first log into Azure from Azure CLI, which can be done in several ways (all of which are explained in this article). Docker also has to be installed on the machine; installation instructions can be found on the official Docker site.
Here we will describe how to create a Docker Swarm cluster with the Azure CLI tool. After linking your account through Azure CLI, you can also verify that it worked. Type the following command into the terminal:
azure account show
In the output, you will get something like the following:
info: Executing command account show data: Name : <SUBSCRIPTION PLAN> data: ID : <ID0> data: State : Enabled data: Tentat ID : <ID1> data: Is Default : true data: Environment : AzureCloud data: Has Certificate : No data: Has Access Token : Yes data: User name : <EMAIL>
Now we will proceed with creating the Docker Swarm cluster. The next series of commands will create one master, which has the role of maintaining the cluster and two nodes for the containers.
Azure CLI Commands for Creating Master and Nodes:
azure vm docker create <NAME_OF_SWARM_MASTER> -l <LOCATION> -e 22 b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140618.1-en-us-30GB ops <PASSWORD> azure vm docker create <NAME_OF_SWARM_NODE01> -l <LOCATION> -e 22 b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140618.1-en-us-30GB ops <PASSWORD> azure vm docker create <NAME_OF_SWARM_NODE02> -l <LOCATION> -e 22 b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140618.1-en-us-30GB ops <PASSWORD>
The output will look something like this:
info: Executing command vm docker create warn: --vm-size has not been specified. Defaulting to "Small". info: Found docker client certificates. + Looking up image b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140618.1-en-us-30GB + Looking up cloud service info: cloud service <NAME_OF_MASTER_OR_NODES> not found. + Creating cloud service + Retrieving storage accounts + Creating a new storage account 'portalvhds1477345946449' + Creating VM
To verify that VMs were created and are ready to use, you can type the command
azure vm list, which will return:
Now we will use these VMs and create the Docker Swarm cluster. The first thing we will do is connect to the master machine. Then we will execute the swarm image in order to generate a token that we will use later for discovering members and creating the swarm group.
The command that has to be executed on the master is:
docker --tls -H tcp://<DNS_NAME_OF_MASTER_FROM_PICTURE_ABOVE>:2376 run --rm swarm create
The DNS name of the master is located in the table above, which we got when we listed all VMs that are created. The swarm output created is shown below.
The token generated as a result of executing the previous command needs to be saved, as it will be used shortly.
Now we will begin with joining the node VMs into the cluster. Before that, we will need to retrieve the Docker port from the nodes in order to use it for joining the cluster. For this, we will use Azure CLI and the following command:
azure vm endpoint list <NAME_OF_NODE01>
Which will generate output similar to the one below.
Now using the docker and -H argument we will join the first node into the cluster.
docker --tls -H tcp://<DNS_NAME_OF_NODE01_FROM_TABLE_OF_VM>:2376 run -d swarm join --addr=<VIRTUAL_IP_FROM_ENDPOINT_OUTPUT>:2376 token://<STORED_TOKEN_FROM_SWARM>
The command for joining the node into the cluster contains information including the DNS name and IP of the node which can be found in the VMs lists. In addition, we will use the token that we got when creating the swarm master machine.
We will repeat these steps for the second node.
Then we will join the nodes to the cluster:
docker --tls -H tcp://<DNS_NAME_OF_NODE02_FROM_TABLE_OF_VM>:2376 run -d swarm join --addr=<VIRTUAL_IP_FROM_ENDPOINT_OUTPUT>:2376 token://<STORED_TOKEN_FROM_SWARM>
Both commands for joining the nodes into the cluster generate a similar output.
To verify that both nodes executed the swarm join command well, you can type the command:
docker --tls -H tcp://<DNS_NAME_NODES>:2376 ps -a
As output, both nodes will generate the output as below.
After all steps are done, we will execute the swarm manage command. This creates the Swarm manager, whose purpose is to receive commands and assign containers to Swarm nodes.
docker --tls -H tcp://<DNS_NAME_OF_MASTER>:2376 run -d -p 2375:2375 swarm manage token://<TOKEN>
In order to list your nodes using the master node, you can type the following command:
docker --tls -H tcp://<DNS_NAME_OF_MASTER>:2376 run --rm swarm list token://<TOKEN>
As the output of this command, you will see the IPs of the created nodes.
In this article, we described in detail how to create a Docker Swarm cluster using the Azure Container Service. The Azure Container Service is intended for hosting container-like solutions and orchestrating them through a flexible and user-friendly portal. This approach is best for those looking for a fast solution for cluster deployment, where, through a few clicks and entering some basic information, users can create clusters that can be easily controlled.
The second, more low-level, approach is to use the Azure CLI shell. With this approach the automation of creating/managing the cluster can be achieved, and the approach can be highly useful for those who like to work on scripting and automatization processes.
In conclusion, we can say that creating a Docker Swarm cluster through both approaches can be done relatively quickly, and that problems that occur in the path of creation are generally minor. With good support and documentation, this serves as an easy, user-friendly solution for those deploying Docker Swarm on the Azure cloud.
Published at DZone with permission of Jeremy Hess , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.