Kubernetes cluster deployment on Azure

Kubernetes Docker Azure AKS

In this project walkthrough, I’ll be showing you how to setup a kubernetes cluster on Azure. The Kubernetes cluster will be hosting a simple website inside of a couple of docker containers.

The goal of this project is to learn how to deploy a Kubernetes cluster onto Azure using their Kubernetes service called AKS (Azure Kubernetes Service).

By the end of this project walkthrough, you’ll have full access to a Kubernetes cluster running on Azure and it will be hosting an application of your choice.



For this project, I’ll be using this docker image that I’ve created myself: https://hub.docker.com/r/davida01/website4testing



What is Kubernetes?

Kubernetes is an open-source platform used for automating the deployment, scaling and management of containerized applications.

Kubernetes is mainly used by large enterprise companies for managing complex IT environments with high traffic. One of the many reasons why they use Kubernetes is because it ensures their services are available and functional even when they receive very large amounts of traffic. In simple terms Kubernetes makes their IT-infrastructure more robust and less prone to outages. Another reason why Kubernetes is popular by large enterprises is because Kubernetes is using container technology to host applications like Docker for example. Containerizing applications make them more secure because of the isolation, more portable when switching server is necessary and saves time during application deployment. The way Kubernetes works is that it will create a so called cluster that can be described as a group of working nodes that contain pods which contains containerized applications. Kubernetes will also host a so called load balancer which is in full control of deciding which container of the cluster should receive the incoming traffic. In theory if one container fails for whatever reason, the load balancer will then send incoming traffic to a functioning container. Meanwhile the load balancer sends the incoming traffic to a functioning container, the control plane will also make sure the dysfunctional container gets restarted in order to recover from the failure.

What is AKS?

The well known cloud platform Azure by Microsoft offers a service called AKS designed to simplify the experience for Kubernetes users. AKS stands for Azure Kubernetes Service, the main purpose of AKS is to make the deployment, management and maintanance of a Kubernetes cluster easier, faster and cheaper. Usally when you are setting up a Kubernetes cluster on the cloud without using any dedicated Kubernetes services, you must setup and manage the Control Plane manually as well paying for it. When using Azure, both the setup and the managemnet of Control Plane will be taken care of automatically by Azure and on top of that you don’t have to pay for the Control Plane (if you choose the free tier package). When using AKS you only have to pay for the Kubernetes working nodes, which are the virtual machines hosting your applications.

Prerequisites:

  • An Azure account
  • Azure CLI
  • Kubectl
  • A Docker image

Step 1: Authenticate to Azure

In order to login to your Azure account using Azure CLI, enter this command:

az login

If you haven’t configured you’re subscription-ID already, do so by entering this command:

az account set --subscription "<subscription-id>"



Step 2: Create a resource group and an AKS cluster

To create a resource group in Azure, enter this command:

az group create --name aks-rg --location austriaeast

To create a Kubernetes AKS cluster, enter this command:

az aks create --resource-group aks-rg --name my-aks-cluster --node-count 1 --generate-ssh-keys



Step 3: Connect to AKS Cluster

To connect to your newly created AKS Cluster, enter this command:

az aks get-credentials --resource-group aks-rg --name my-aks-cluster

A great way to check if you have sucessfully connected to your AKS cluster is by testing if you are able to retrive a list of all available Kubernetes working nodes.


To retrive an overview of all available working nodes, enter this command:

kubectl get nodes



Step 4: Create a Kubernetes manifest file

The last step before deploying the application onto the cluster is to create a .yaml configuration file called manifest file. You can name the .yaml file whatever you want, I’ll be naming mine deployment.yaml.


Here’s how my manifest file looks like:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: website4testing-deployment
  labels:
    app: website4testing
spec:
  replicas: 4
  selector:
    matchLabels:
      app: website4testing
  template:
    metadata:
      labels:
        app: website4testing
    spec:
      containers:
      - name: website4testing-container
        image: davida01/website4testing:latest
        ports:
        - containerPort: 80 
        resources:
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "250m"
            memory: "256Mi"

---
apiVersion: v1
kind: Service
metadata:
  name: website4testing-service
spec:
  type: LoadBalancer 
  selector:
    app: website4testing
  ports:
    - protocol: TCP
      port: 80         
      targetPort: 80   



Step 5: Deploy the Application

To deploy the application onto the AKS cluster, enter this command:

kubectl apply -f deployment.yaml



Step 6: Retrive important information about cluster

To retrive an overview of all the IP addresses invloved in your cluster, enter this command:

kubectl get service

To retrive a list of all the available pods of your AKS cluster, enter this command:

kubectl get pods



Step 7: Delete deployment and AKS cluster

To delete the deployment, enter this command:

kubectl delete -f deployment.yaml

To delete the AKS cluster, enter this command:

az aks delete --resource-group aks-rg --name my-aks-cluster --yes --no-wait



Summary

In this project walkthrough, we have:


  • Used Azure-CLI and Kubectl in order to deploy a Kubernetes cluster onto Azure by only using the terminal.
  • Learnt about what a Kubernetes manifest file is and how to write one.
  • Deployed a Kubernetes cluster hosting a static website on Azure using AKS
  • Learnt the basics of Kubernetes and gained some practical experience on top of it.