Deploy your MicroService to Azure Container Services (AKS)

Sonu Kumar
5 min readApr 24, 2020

Background

Cloud Computing is a new buzz in the market and getting popularity day by day . Microsoft Azure is one of my favourite Cloud so after wrapping up my head around Microservice Architecture , It’s time to create and deploy back-end microservices to Azure Container Service (AKS). This is my 6th article and here i’ll be describing step by step how to deploy & run microservices to Azure Kubernetes.

Application Architecture & Deployment

The Azure Container Service (ACS) is a cloud-based container deployment and management service that supports popular open-source tools and technologies for container and container orchestration.

Prerequisites:

Steps:

  1. Create an Azure Resource Group to have everything in one place.
  2. Create Azure Container Register (ACR) to push and share our local image.
  3. Add Azure Service Principal to be able to use our ACR with Azure Kubernetes Service (AKS).
  4. Create AKS.
  5. And then, spin out the deployment.

1. Create a resource group:

Log in to Azure from Azure CLI.

az login

Create a Resource Group. Be aware that at the time of writing AKS is not available in all Azure regions, so choose appropriate region.

az group create -l westeurope -n sonukuberg

2. Create an Azure Container Registry:

Probably you’ll want a private registry to upload your docker images, so let’s create an Azure Container Registry instance.

az acr create -g sonukuberg -n sonukubeacr --sku Basic --admin-enabled

3. Login to the Azure Container Registry:

You can login to the Container Registry using the following Azure CLI command:

az acr login -n sonukubeacr

4. Push an image to the Azure Container Registry:

In this step we are going to pull an image from docker hub, and then upload it to the Container Registry created in step 2. Feel free to use your own docker image with a working web application.

docker tag employeeservice:latest sonukubeacr.azurecr.io/sonukube:v1-emplatestdocker push sonukubeacr.azurecr.io/sonukube:v1-emplatest

5. Add Azure Service Principal

Create a service principal and configure its access to Azure resources.

az ad sp create-for-rbac --skip-assignment

Remember the appId and password? -You need those now. Mine are:

"appId": "cfed7ab3-8db9-4bba-86c9-2311d505d2ad", 
"password": "7193c36f-3123-4453-8ee7-2aa7bf071007",

Create a new role assignment (use your appId after --assignee):

az acr show --name sonukubeacr --resource-group sonukuberg --query "id" --output tsv 

az role assignment create --assignee "cfed7ab3-8db9-4bba-86c9-2311d505d2ad" --role Reader --scope <Put value from above query>

And you are done with permissions!

6. Create AKS cluster

Now create the AKS cluster:

az aks create 
--name sonukubeakscluster
--resource-group sonukuberg
--node-count 1
--generate-ssh-keys
--service-principal "cfed7ab3-8db9-4bba-86c9-2311d505d2ad"
--client-secret "7193c36f-3123-4453-8ee7-2aa7bf071007"

Use appId for service-principal and password for client-secret.

7. Install kubectl

To manage the cluster you’ll need to install kubectl so run the following command:

az aks install-cli

8. Get cluster credentials

Get the cluster credentials and check the connectivity so you can start working with the AKS cluster.

az aks get-credentials --name sonukubeakscluster --resource-group sonukuberg
kubectl get nodes

9. Create a kubernetes deployment definition.

Create a file [your deployment].yml with the following contents (replace the secret name):

apiVersion: apps/v1
kind: Deployment
metadata:
name: sonukube-pocdeployment-emp
labels:
app: sonukube
spec:
replicas: 1 #5
template:
metadata:
name: sonukube
labels:
app: sonukube
spec:
containers:
- name: sonukubeemp
image: sonukubeacr.azurecr.io/sonukube:v1-emplatest
imagePullPolicy: IfNotPresent
restartPolicy: Always
selector:
matchLabels:
app: sonukube
---apiVersion: v1
kind: Service
metadata:
name: sonukube-pocservice-emp
spec:
selector:
app: sonukube
ports:
- port: 80
type: LoadBalancer

As you can see the file references the image pushed to the Container Service with the secret created in previous steps.

10. Deploy your service.

Run the following command to deploy your service to AKS:

kubectl create -f [your deployment].yml

11. Get the public IP for your service

To try your service you’ll need to get the public IP (It can take a while).

kubectl get services

Once you get the External IP go ahead and test the service as below:

Hence our job is done !!!

Conclusion

In this article, we have covered how to deploy your first Service to Azure Container Services (AKS) step by step. Implementation is always tough so i tried best to cover every step so that reader of this article can get benefits while getting things implemented.

Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover .

--

--

Sonu Kumar

Software Consultant interested in Microservices / Serverless computing, Middleware / SOA, Event Driven Architecture & Machine Learning.