How to Deploy Microservices in Azure Kubernetes Service (AKS)

Mahfooz Ahamed
4 min readSep 8, 2024

--

Introduction
Microservices architecture is a popular choice for building scalable and maintainable applications. With each service handling a specific business capability, it becomes easier to manage complex systems. But when it comes to deployment, challenges arise, such as scaling, orchestration, and service discovery. This is where Kubernetes comes in.

Azure Kubernetes Service (AKS) offers a fully managed Kubernetes cluster, simplifying container orchestration and providing easy integration with Azure’s ecosystem. In this guide, I’ll walk you through deploying a microservice on AKS step by step.

1. Understanding AKS and Microservices

What is AKS?

AKS is a managed Kubernetes service that helps you deploy, manage, and scale containerized applications. It abstracts away the complexity of running Kubernetes and allows you to focus on your applications. With AKS, you don’t have to worry about the master node setup and maintenance, as it is fully managed by Azure.

Why Microservices on AKS?

Microservices work well with AKS due to Kubernetes’ inherent strengths like:

  • Service Discovery and Load Balancing: Microservices can automatically be exposed via DNS names.
  • Automated Rollouts and Rollbacks: Easily deploy new versions of services with zero downtime.
  • Self-healing: Automatically restarts failed containers, replaces and reschedules them when nodes die.

2. Prerequisites

To follow along, ensure you have:

  • Azure Account: Sign up for a free account.
  • Azure CLI installed: To interact with Azure resources.
  • Docker installed: For containerizing your microservice.
  • kubectl: The Kubernetes command-line tool for managing clusters.
  • Helm: A package manager for Kubernetes.

3. Setting Up AKS

Let’s start by creating an AKS cluster using the Azure CLI.

Step 1: Log into your Azure account

Run the following command to authenticate your CLI session:

az login

This will open a browser window where you can log in with your Azure credentials.

Step 2: Create a Resource Group

Resource groups are used to manage related Azure resources together. Create one for your AKS cluster:

az group create --name MyResourceGroup --location eastus

Step 3: Create an AKS Cluster

Now, let’s create the AKS cluster with 3 nodes:

az aks create --resource-group MyResourceGroup --name MyAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys

This command will provision a 3-node AKS cluster and enable monitoring, which is useful for later steps.

Step 4: Connect to the AKS Cluster

Once the cluster is ready, fetch the credentials to manage it with kubectl:

az aks get-credentials --resource-group MyResourceGroup --name MyAKSCluster

You can verify the connection by running:

kubectl get nodes

This will list the nodes in your AKS cluster.

4. Building and Pushing Docker Images

Before deploying microservices, they need to be containerized using Docker.

Step 1: Write a Dockerfile

Let’s assume we have a simple Node.js microservice. Here’s a Dockerfile for it:

# Use Node.js base image
FROM node:16

# Create app directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

Step 2: Build the Docker Image

Navigate to the microservice’s root folder and run the following command to build the Docker image:

docker build -t myapp:latest .

Step 3: Push the Image to Azure Container Registry (ACR)

Create an ACR in Azure to host your images:

az acr create --resource-group MyResourceGroup --name MyACR --sku Basic

Log in to the ACR:

az acr login --name MyACR

Tag and push your Docker image to ACR:

docker tag myapp:latest myacr.azurecr.io/myapp:latest
docker push myacr.azurecr.io/myapp:latest

5. Deploying Microservices to AKS

Now that the Docker image is pushed to ACR, let’s deploy it to AKS.

Step 1: Create a Kubernetes Deployment YAML

Create a deployment.yaml file:

apiversion:
apps/v1kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myacr.azurecr.io/myapp:latest
ports:
- containerPort: 3000

Step 2: Apply the Deployment

Deploy the microservice to your AKS cluster by running:

kubectl apply -f deployment.yaml

Check if the pods are running:

kubectl get pods

6. Setting Up Ingress with Nginx Controller

To expose your microservices externally, you’ll need an ingress controller.

Step 1: Install the Nginx Ingress Controller

You can use Helm to install the ingress controller:

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm install ingress-nginx ingress-nginx/ingress-nginx

Step 2: Create an Ingress Resource

To route traffic to your service, create an ingress.yaml file:

apiversion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-service
port:
number: 80

Apply the ingress resource:

kubectl apply -f ingress.yaml

7. Configuring Monitoring and Auto-Scaling

AKS comes with built-in monitoring and auto-scaling tools. Let’s set them up.

Step 1: Monitor with Azure Monitor

Azure Monitor automatically tracks metrics like CPU and memory usage. To view these metrics:

az monitor metrics list --resource MyAKSCluster --metric-names "CPU Utilization"

Step 2: Enable Horizontal Pod Autoscaler (HPA)

To automatically scale your microservices based on CPU usage, enable HPA:

kubectl autoscale deployment myapp --cpu-percent=50 --min=1 --max=5

This will automatically add more pods if CPU usage exceeds 50%.

8. Conclusion

Deploying microservices in AKS allows you to leverage Kubernetes’ power to scale, manage, and maintain your services with ease. Azure’s managed Kubernetes service simplifies much of the setup, enabling you to focus on your application’s business logic instead of managing infrastructure.

By following this guide, you now have a fully functional AKS cluster hosting your microservices. You’ve also learned how to build Docker images, push them to Azure, deploy them to Kubernetes, and configure load balancing, monitoring, and auto-scaling.

Happy Coding!!!

--

--

Mahfooz Ahamed
Mahfooz Ahamed

Written by Mahfooz Ahamed

Graduated | Postergraduate Msc Big Data Analytics

No responses yet