Dockerize your Node JS application and deploy it to Azure Kubernetes Service (AKS)

Kubernetes Up & running with Azure Kubernetes Service

--

Kubernetes aka K8 is an open-source container-orchestration system design by Google for automating computer application deployment, scaling, and management.

Azure Kubernetes Service (AKS) is a place where you can deploy and manage your containerized applications with fully managed Kubernetes services. If this is the first time you have heard of Kubernetes, I recommend to go and check out this medium story and then come back to deploy applications to the Kubernetes cluster which is I’m going to tell you today. Okay, let’s see what is the outline for today’s story.

Outline 📃

  • Introduction to Azure Kubernetes Service (AKS) and Azure Container Registry (ACR)
  • Overview of the use case
  • Create Azure Container Registry (ACR)
  • Build and push the Docker image to ACR
  • Create and deploy Azure Kubernetes Cluster
  • Deploy containerized Node JS application to K8 cluster
  • Conclusion

Azure Kubernetes Service (AKS)

Azure Kubernetes Service is a managed container orchestration service based on the open-source Kubernetes system, which is available on the Microsoft Azure public cloud. As an AKS user, we are only responsible to manage the worker node because Azure Kubernetes Service manage the master node as a service.

If you wondering what is a node in K8, a node is a worker machine in Kubernetes and it may be either a virtual machine or a physical machine. Want more information, click right here.

Azure Container Registry (ACR) 📦

Azure Container Registry is Microsoft’s own hosting platform for Docker images 🐳. It is a private registry where you can push your containerized Docker images and other related artefacts. These images we can pull and run locally or we can deploy these images in a cluster or any other hosting platform.

Overview of the use case

You were given the responsibility as a to develop and deploy 🚀 a small application to demonstrate the benefits of Azure Container Registry as a container orchestration platform for your organization’s next microservice project.

The developer implements the application and uses Dockerfile to build a new image out of the application. Then push the dockerized image to ACR. After all these steps are done, the developer applies the Kubernetes deployment YAML file. Then AKS start the deployment with the dockerized image attach to it. After the deployment is done, the load balancer will expose the application to the public internet.

Okay, now we know what our ultimate goal is. Let’s make it a reality. 🔥

Create Azure Container Registry

As the first task, we are going to create the container registry on the Azure portal. But we need to clean up a few things before we continue. To ensure the success of this lesson, make sure you have the necessary tools installed on your computer.

✔️ Docker (Download for Windows, Linux and Mac)

✔️ Kubectl — Kubernetes Command Line Tool

✔️ Azure CLI — Command Line Tool to manage Azure resources

✔️ Azure Subscription — Get $200 free Credit

Nice. Now we have all the tools and resources that need to deploy a Node JS application to Azure Kubernetes Service. Okay, now let’s move on to the next step.

1. Log in to Azure

Before doing anything, we need to authenticate ourselves in the Azure portal. There are many ways to log in to the Azure portal. But here, I’m using web-based authentication. Open your terminal and run the commands below.

az login

After successfully login, run this command to list down your subscriptions.

az account list -0 table

2. Create Azure Resource Group

It is mandatory to create a resource group before we create container registry and Kubernetes service. In Azure resource, groups are used to manage and organize our resources. You can create multiple resource groups under one Azure subscription. Run the below command to create a resource group under your subscription.

az group create --name azure-cloud-streak --location sotheastasia

📝 — name: Name for the resource group

📝 — location: Azure region

This is how it looks on the Azure portal.

Now we create the Azure Resource Group successfully. Let’s move on to the next level.

3. Create Azure Container Registry

We need to create the ACR to upload our containerized Node JS application. When we create Azure Kubernetes Service (AKS), we need to connect this ACR with that AKS to deploy our application to the Kubernetes cluster. Okay then, let’s create the container registry. Run the following command to create the container registry.

az acr create --name "azurecloudstreakdemo" -g "azure-cloud-streak" --location "southeastasia" --sku "Basic"

  • - -name - Name for your Azure Container Registry
  • -g - Resource group name. (For this, you need to give the resource group name that we created in the above section.)
  • - -location - Region for the ACR
  • - -sku Pricing level for ACR (“Basic plan is more than enough”)

You can view the newly created ACR on the Azure portal under the resource group.

Build and Push the Docker image to ACR 📦

Okay. now we create the ACR perfectly. Now it’s time to build our Docker image 🐳 and push it to the created Azure Container Registry. This is how our project structure looks like.

The app.js is the starting place of our application. The view folder contains the static website which renders to screen after the application start. You can get the entire project from here. ⭐ for the project, really appreciated😉. Okay. now let’s create the Dokerfile.

Okay, let’s edit this Dockerfile to get the Docker image of the Node JS application. There is no extension for the Docker file.

“But the file name must be Dockerfile without any extensions.”

I will explain this Dockerfile in simple terms. Let’s start from line number 1.

1 — We are going to create a Node JS application. In order to run our application on the container, we need to install Node JS on it. Here, we tell Docker to get the Node JS image from the docker hub.

2 — Here, we tell docker to create a new working directory(folder) to store our application code. This will create a new directory in our Docker container. You can give any directory name. Here I named the directory as app

3 — Then we copy our entire project code to the directory that we created in line number 2. If your Dockerfile is in the same project folder, you can put a “.” as the first argument. Otherwise, you have to give the absolute path to your Dockerfile. As the second argument, we need to give where we want to paste our codebase. Here, we can specify the working directory that we created in line number 2.

4 — After we copy our code to the working directory, we can run npm install to install our application dependencies to our container.

5 — After installing all the dependencies, we tell Docker to initiate the executables to run our application on the container.

6 — Here, we specify the instruction that is to be executed when the Docker container starts. It’s like, when we start the docker file, we tell docker to run npm run start command on the container.

The Dockerfile is vary from one project to another. Go and check out the below documentation to get more information about how to write a good Dockerfile. 👇🏻

Okay, now it’s time to build our Docker image using the Dockerfile. You can use the following command to build the Docker image.

docker image build -t azurecloudstreakdemo.azurecr.io/portfolio-app:v1 .

-t - Tag name for the Docker image

IMPORTANT:- When you give a tag name for your docker image, make sure to give a name like in this format. <acr-login-server-name>/<repository-name>:tag . To get the ACR login server name, navigate to the container registry that we created in the previous step (Step 3). There you can get the login server name like this.

For <repository-name> , you can give any name. For tag , you can give names like this, v1, version1, latest etc.

Building the Docker image

You can view the newly created Docker image by running this command.

docker images

Docker images

Nice. Now we successfully build the Docker image for our Node JS application. Now it’s time to PUSH our image to Azure Container Registry.

PUSH Docker image to ACR

Before we push our Docker image to the ACR, we need to authenticate ACR. You can use the following command to log in to the ACR.

docker login azurecloudstreakdemo.azurecr.io

This login will take your Azure Container Registry’s username and password. Follow these steps to grab your login credentials.

Select your Resource Group and click on the created Azure Container Registry. Then navigate to the Access Keys 🔑 and get your ACR username and password 🔐.

After successful login into the ACR, you can run the following command to push your Docker image 🐳 to the ACR.

docker image push azuecloudstreakdemo.azurecr.io/portfolio-app:v1

Then Docker will start to push the image into the Azure Container Registry. (Depending on your internet connection and image size, this will take some times to upload. ⌛) After successfully push all the artefacts to the ACR, you can see an output like this.

Output after push Docker image to ACR

You can check on the Azure portal whether the Docker image is available or not. Navigate to your ACR and click on the Repositories tab.

Docker image on Azure Container Registry

Very nice. We successfully build our Docker image and push it to the ACR. Now we can move on to the next step which is Creating Azure Kubernetes Service.

Create and deploy Azure Kubernetes Cluster

Now it’s time to create the Kubernetes cluster. For this, we are going to create a two-node cluster. Based on the organization requirements, the number of nodes can be changed.

To create the AKS cluster you can run the following commands. The command parameters that I used here are very basic. But if you want to learn more about parameters go and check out this documentation.

az aks create --resource-group azure-cloud-streak --name azurecloudstreakaks-demo --node-count 2 --generate-ssh-keys --node-vm-size=Standard_B2s --attach-acr azurecloudstreakdemo

  • - -resource-group — Resource Group name
  • - -name — Name for the Azure Kubernetes Cluster
  • - -node-count — Number of worker nodes to create in the cluster
  • - -generate-ssh-keys — Generate SSH keys for the worker nodes.
  • - -attach-acr — This parameter is really important. This is where we attach our ACR with our AKS cluster.
  • - -node-vm-size — Pricing tier for the AKS cluster. Check out this documentation to get more information about pricing tiers.

After successfully create the Kubernetes cluster, now we need to verify our cluster. We can verify our cluster using kubectl. Kubectl helps us to verify the state of our cluster. But before that, we need to get authenticated with the Azure Kubernetes Service.

az aks get-credentials --resource-group azure-cloud-streak --name azurecloudstreakaks-demo

Now it’s time to verify our cluster state with the help of kubectl. Run this command to check the state of the worker nodes. (We create 2 worker nodes in our cluster. Let’s check whether it’s work or not.)

kubectl get nodes

Beautiful. Here you can see our 2 worker nodes ready for their work 😊. Now it’s time to move on to the next step.

Deploy containerized Node JS application to K8 cluster

Before we start the deployment, let’s take a look at what we have done so far.

✅ Create the Azure Container Registry (ACR)

✅ Build and PUSH the Docker image to ACR

✅ Create and deploy Azure Kubernetes Service (AKS)

✅ Check the status of the worker nodes. (They are ready to rock 🤘)

Now it’s time to create the deployment YAML file. We declare the configuration that must be there for the application in this manifest, such as the number of replicas, health checks, settings, and so on. You can use the same YAML file that I’m using here to create your Kubernetes deployment.

When you run this YAML file, it will deploy our containerized application to the Kubernetes cluster and it uses the Kubernetes Load Balancer Service to publish our application to the internet so that other people can access our application through the Load Balancer. To deploy our containerized application to the AKS cluster, we need to apply this YAML file to AKS. Using the following command you can apply this file.

kubectl apply -f deployment-file.yaml --record

Here you can see, our deployment and load balancer service are created successfully. You can give the following command to verify the state of the pods.

kubectl get pods

Okay. We verified that our pod is up and running. Let’s see how our services are doing. Run the following command to get the state of the service.

Nice. Our load balancer service is also working fine.

Now, this is the time that all of us waiting for. Which is how to access the deployed application on the public internet. To access the application through the internet, just copy the EXTERNAL-IP and paste it into your browser.

WOLA ✨… Here is our containerized Docker image running on the Azure Kubernetes cluster with the load balancer service.

Okay, now we achieved our goal today 👏🏻, which is to Dockerize a Node JS application and deploy it on the Azure Kubernetes Service.

However, we are not yet finished. If your project is not a professional one, make sure to remove the Azure Resource Group when your deployment is complete, because if you operate this Kubernetes cluster, you will be billed a significant amount of money for the worker nodes. So, if it’s not a professional project, make sure to remove the newly established Resource Group with the command below.

az group delete --name azure-cloud-streak

Conclusion 👋

So, it’s time to bid farewell. Today, we learnt how to construct a Docker image, an Azure Resource Group, an Azure Container Registry, an Azure Kubernetes Service, a Kubernetes deployment with a load balancer service, and much more.

If you find any difficulties when you doing this practical, please put a comment below.

Thank you for reading 🙏🏼.

--

--