Stupid Simple Kubernetes — Create Azure Infrastructure for Microservices

Everything you need to know to create production-ready Azure infrastructure for Microservices deployment using Azure Kubernetes Service (AKS)

Czako Zoltan
5 min readJun 8, 2020
Example Project

In the previous article, we learned about the basic concepts used in Kubernetes, its hardware structure, about the different software components like Pods, Deployments, StatefulSets, Services and we saw how to communicate between services and with the outside world.

This article will be much more practical, we will create all the necessary configuration files to deploy multiple microservices in different languages use MongoDB as data storage. We will also learn about Azure Kubernetes Service and we will present the infrastructure used to deploy our services.

The code used in this article can be found in my StupidSimpleKubernetes-AKS git repository. If you like it, please leave a start!

NOTE: the scripts provided are cloud agnostic, so the tutorial can be followed using other types of cloud providers or using a local cluster with Minikube.

Requirements

Before starting this tutorial, please make sure that you have installed Docker and Azure CLI. Kubectl will be installed with Docker. (if not, please install it from here).

You will also need an Azure Account. Azure offers 30 days or 170$ free trial, which will be more than enough for our tutorial.

Through this tutorial, we will use Visual Studio Code, but this is not mandatory.

Creating a Production Ready Azure Infrastructure for Microservices

To have a fast setup, I’ve created an ARM Template, which will automatically spin up all the Azure resources needed for this tutorial. You can read more about ARM Templates here.

We will run all the scripts in the Terminal of VS Code.

The first step is to log in to your Azure account from the VS Code Terminal. For this run az login. This will open a new tab in your default browser, where you can enter your credentials.

For the Azure Kubernetes Service, we need to set up a Service Principal. For this, I’ve created a PowerShell script called create-service-principal.ps1. Just run this script in the VS Code Terminal or PowerShell.

After running the code, it will return a JSON response with the following structure:

Service Principal

Based on this information you will have to update the ARM Template to use your Service Principal. For this, please copy the appId from the returned JSON to the clientId in the ARM Template. Also, copy the password and paste it to the ARM Template’s secret field.

In the next step, you should create a new Resource Group called “StupidSimpleKubernetes” in your Azure Portal and import the ARM template to it.

To import the ARM template, in the Azure Portal click on the Create a resource button, search for Template Deployment and select Build your own template in the editor. Copy and paste the template code from our git repository to the Azure template editor. Now you should see something like in the following picture:

ARM Template Deployment

Hit the save button, select the StupidSimpleKubernetes resource group, and hit Purchase. This will take a while and it will create all the necessary Azure resources for a production-ready microservices infrastructure.

You can also apply the ARM Template using the Azure CLI, by running the following command in the root folder of our git repository:

ARM Template Deployment using Azure CLI

After the ARM Template Deployment is done, we should have the following Azure resources:

Azure resources

The next step is to authorize our Kubernetes service to pull images from the Container Registry. For this, select the container registry, select the Access Control (IAM) menu option from the left menu, click on the Add button, and select Role Assignment.

AcrPull Role Assignment

In the right menu search for the correct Service Principal (use the display name from the returned JSON object — see the Service Principal image above).

After this step, our Kubernetes Service will be able to pull the right Docker images from the Azure Container Registry. We will store all our custom Docker images in this Azure Container Registry.

We are almost ready, in the last step, we will set up the NGNIX Ingress Controller and add a RecordSet to our DNS, this way assigning a human-readable hostname to our services instead of using the IP:PORT of the Load Balancer.

To set up the NGINX Ingress Controller, please run the following two commands, in the root folder of the repository, one-by-one:

This will create a new public IP, which can be seen in the Azure Portal:

If we take a look over the details of this new Public IP resource, we can see that it does NOT have any DNS name.

To assign a human-readable DNS name to this Public IP, please run the following PowerShell script (just replace the IP address with the correct IP address from your Public IP resource):

This assigns a DNS name to the public IP of your NGINX Ingress Controller.

At this step, we are ready to deploy our Microservices to the Azure Kubernetes Cluster.

Conclusions

In this tutorial, we learned how to create a production ready Azure infrastructure to deploy our microservices. We use an ARM Template to automatically setup the Azure Kubernetes Service, the Azure Container Registry, the Azure Load Balancer, an Azure File Storage (which will be used for persistent data storage) and to add a DNS Zone. We applied some configuration files to authorize Kubernetes for pulling Docker images from the Azure Container Registry, to configure the NGINX Ingress Controller, and to set up a DNS Hostname for our Ingress Controller.

In the next article we will write the necessary scripts to dockerize our NodeJS, NestJs, and MongoDB services, to create pods using Kubernetes Deployment scripts, and to set up the necessary Kubernetes Services to communicate between the pods and with the external world.

You can learn more about the basic concepts used in Kubernetes in my previous article.

If you want more “Stupid Simple” explanations, please follow me on Medium!

There is another ongoing “Stupid Simple AI” series. The first two articles can be found here: SVM and Kernel SVM and KNN in Python.

Thank you for reading this article!

--

--

Czako Zoltan

I'm an experienced Full-Stack Developer, with experience in multiple domains including Backend, Frontend, DevOps, IoT, and Artificial Intelligence.