Running your first application on Azure Kubernetes Services

Provisioning a Kubernetes cluster to run applications on production is not an easy task, even with someone who has experience with Kubernetes. You need to take care of security (authentication & authorization), cluster upgrade, scalability & availability of your cluster. There for, the managed Kubernetes service is a good candidate when organizations want to run their containerized applications with K8S. Almost all cloud provider has their own k8s services: Google Kubernetes Engine (GKE), Azure Kubernetes Services (AKS), AWS Elastic Kubernetes Services (EKS)…

In this post, I will demonstrate the main steps to provision a Kubernetes cluster with Microsoft AKS and run a simple application on that, let’s check it out.
Prerequisites:

  • Basic knowledge of Kubernetes.
  • An Azure account with an active subscription.

What is Kubernetes?
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

What is Azure Kubernetes Services - AKS?
Azure Kubernetes Service (AKS) offers the quickest way to start developing and deploying cloud-native apps in Azure, datacenters, or at the edge with built-in code-to-cloud pipelines and guardrails. Get unified management and governance for on-premises, edge, and multi-cloud Kubernetes clusters. Interoperate with Azure security, identity, cost management, and migration services.

Some highlighted features of Azure Kubernetes Services

  • Automated management and scalability of Kubernetes clusters for enterprise-grade container orchestration.
  • End-to-end developer productivity with debugging, CI/CD, logging, and automated node maintenance.
  • Advanced identity and access management to monitor and maintain container security for governance at scale.
  • Support for Linux, Windows Server, and IoT resources with AKS deployment on the infrastructure of your choice using Azure Arc.

Reference: https://azure.microsoft.com/en-us/products/kubernetes-service/#overview
How to provision a managed Kubernetes cluster with Azure?

  1. Open your Azure portal (https://portal.azure.com) and search for Kubernetes services.

  1. From the Kubernetes services dashboard, click Create a Kubernetes cluster

  1. Configure the basics information of the cluster


Resource group: kodekloud-aks
Cluster name: my-aks
Region: choose the nearest location.

Kubernetes version: 1.24.6 (latest at the time I write this post).
Other options leave as default values.
Then click Review & Create, wait for the validation process to finish, and click Create to request Azure provision our managed cluster.

  1. We need to wait about 5 mins until the cluster is created, time depends on the region and the configurations of our cluster.

Access your managed Kubernetes cluster.

From your Kubernetes services instance dashboard, click Connect to your cluster (we will use Cloud Shell - an integrated shell on the browser, provided by Azure). Please take note of the commands, we will use them later in the shell.

az account set --subscription {your_subscription_id}
az aks get-credentials --resource-group kodekloud-aks --name my-aks

Click “Open Cloud Shell”, and enter the 2 above commands to connect to your cluster.

We successfully connected to our managed Kubernetes services, let’s try some commands to validate.

trung@Azure:~$ kubectl cluster-info
Kubernetes control plane is running at https://my-aks-dns-b04ef05c.hcp.southeastasia.azmk8s.io:443
CoreDNS is running at https://my-aks-dns-b04ef05c.hcp.southeastasia.azmk8s.io:443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://my-aks-dns-b04ef05c.hcp.southeastasia.azmk8s.io:443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy

The cluster is up and running well, with CoreDNS and a metrics server also (we can use kubectl top node/pod to monitor the consumed resources).

Running your application on AKS

We will use the k8s command to run a simple web server (httpd) by creating a deployment named my-web-server with 3 replicas.

trung@Azure:~$ kubectl create deploy my-web-server --image=httpd:alpine --replicas=3 
deployment.apps/my-web-server created
trung@Azure:~$ kubectl get deploy
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
my-web-server   3/3     3            3           33s
trung@Azure:~$ kubectl get pod
NAME                             READY   STATUS    RESTARTS   AGE
my-web-server-745f5f5fb6-jspvg   1/1     Running   0          50s
my-web-server-745f5f5fb6-p7s7d   1/1     Running   0          50s
my-web-server-745f5f5fb6-rpzqg   1/1     Running   0          50s

Expose your application with Azure Load Balancer

We already created our web server, let’s create a service to expose it outside.

trung@Azure:~$ kubectl expose deploy/my-web-server --type=LoadBalancer --port=80 --name=my-web-server-svc
service/my-web-server-svc exposed

trung@Azure:~$ kubectl get svc
NAME                TYPE           CLUSTER-IP   EXTERNAL-IP     PORT(S)        AGE
kubernetes          ClusterIP      10.0.0.1     <none>          443/TCP        21m
my-web-server-svc   LoadBalancer   10.0.8.206   20.198.213.73   80:30776/TCP   37s

Azure will responsible for provisioning a Load Balancer and assigning a public IP to our service, in my case it is 20.198.213.73 (don’t try to access this, I will delete the resources before publishing the post).

Yeah, it worked, we accomplished the target to provision a managed Kubernetes cluster, connect to it using Cloud Shell, create a web server using httpd image and publish it to the world using LoadBalancer type service.

What a long journey, hope you enjoy it and learn some valuable knowledge.

Happy learning,
Trung.