Deploying Docker Swarm On NGINX Web Server

This HOL will guide you in deploying Docker Swarm along and hosting NGINX web server. Followed by the same, load balancing and scaling the number of instances will be shown.

Docker Swarm

This hands-on session will guide you in deploying Docker Swarm on the hosting of NGINX web server. Then load balancing and scaling the number of instances will be shown.

Creating Web API app for the role

Login to your Azure account via portal and go to Active Directory->App Registrations->+New application registration.

Docker Swarm

 Give a name to your app and choose Settings as denoted below. Finally, click on "Create" to deploy an application.

Docker Swarm

 After the app gets created, make note of your Application ID and click on Settings >> Keys.

Docker Swarm

 Give a name for your password and choose an expiry date and click on the Save button to get your key.

Docker Swarm

 Note the key as well. It is called as app secret and you will be using it in further steps.

Docker Swarm

 

Providing access to Subscription

Now, let us grant access to subscription for the app that we have created now. For this, go to subscription->Access Control->Add. Here, choose the role as contributor and select the app that you created now. Finally, click on the Save button.

Docker Swarm

 

Creating Docker Swarm

Go to the URL - https://docs.docker.com/docker-for-azure/#quickstart and click on the below-denoted menu. This will take you into the Azure portal again.

Docker Swarm

 

Choose or create a resource group and select a suitable location. Then, fill the App ID and App Key that you copied in the previous step. This is why we registered an application in AD.

Docker Swarm

 

Generate a public key for your Linux environment and paste it in the Public key box. Choose the required number of worker count, manager count, and VM size. Fill in the needed details, agree to the terms, and click on Purchase button. The deployment will take a minimum of 5 minutes depending on the SKUs that you choose.

Docker Swarm

After all the resources get deployed, click on the externalLoadBalancer.

Docker Swarm

 In the left side menu of externalLoadBalancer, click on Load Balancing Rules and add a rule.

Docker Swarm

 Give a name to the rule and click on OK without changing any other settings.

Docker Swarm

 Now, copy the public IP address of externalSSLoadBalancer. Using the IP address, connect to the Linux VM using Putty.

Docker Swarm

 Make sure to use Docker as the username.

Docker Swarm

 

Run the command

docker node ls.
 
This will show you all the nodes.

Docker Swarm

 

Hosting an NGINX server

Run this command  -

docker service create --name mywebsite -p 80:80 nginx
 
This will host NGINX server on your container.
 
Docker Swarm

 Now, run the command to view the website that you hosted now.

docker service ls
 
Docker Swarm

 

Run the following command to see the number of replications of your site.

docker service ps my website
 
Docker Swarm

 

Ping the IP of your externalSSLoadBalancer in the browser. You will get a response from the NGINX server.

Docker Swarm

Replicating multiple instances of website

Run this command to get multiple replicas for your website.

docker service update --replicas 10 mywebsite 

Docker Swarm

Now, run the command -
 
docker service ps my website
 
This will show you all 10 replicas.

Docker Swarm

 

Scaling Instances of Worker VMSS

Go to the Worker-VMSS in the resources that you created initially.

Docker Swarm

 

In the left side menu, click on Scaling and change the instance count to your requirement and click on save button.

Docker Swarm

 

Click on the instances menu in the left side. Wait for the additional instances to spin up. In a while, you will be able to find the newly scaled instances. 

Docker Swarm

 

At last, run this command to get the state of all your containers.

docker service ps mywebsite
 
Again, run the command -
 
docker service update --replicas 15 mywebsite
 
This will be spinning up another 5 instances for your NGINX Server.
 
Docker Swarm

 

At last, run the following command to get details about every process and the container instance in which it is running.

docker service ps mywebsite
 
Docker Swarm