Deploying an Application on Kubernetes.

Introduction
Kubernetes is an open-source technology used to autonomously deploy, scale, and manage applications that use containers. It is a popular container orchestration solution since it enables the control of several containers as a single entity as an alternative to managing each container separately.

This is a walkthrough of deploying an application to Azure Kubernetes Service (AKS)

What will be covered?

  • Azure Kubernetes Service

  • Azure Container Registry

  • Docker Container

  • Azure CLI

  • Kubectl

  • Simple Node app

Overview
A note keeper app written in node js will be containerized and pushed to ACR, after which one Kubernetes cluster will be created on AKS, and a deployment and a service will be created with 4 pod replicas via a manifest file.

Prerequisites

  • Terminal and Azure CLI installed on local machine - some of the steps for this project (such as connecting to kubernetes cluster will be done using Azure CLI).

  • Docker installed on local machine - which will be used to build an image and push to a registry (Azure Container Registry in this demo).

  • Azure account with active subscription.

  • Code Editor or IDE - which will be used to write code that will be containerized.

  • Terraform installed - an IAC tool that will be used to provision infrastructure on Azure.

Note: The required services can be created using either terraform, Azure portal or Azure CLI.

Getting Started

  • Testing sample app locally

    • Write the code or fork and clone app from Github

    • Navigate to application folder on terminal using cd <foldername>

    • Install dependencies using npm install

    • Run app to test using node <filename> or npm <filename>

Test on local

  • Login to azure using az login on terminal.

Login on azure CLI

  • Provision infrastructure on Azure with terraform using the following commands: terraform init terraform fmt terraform validate terraform plan terraform apply(note that a terraform file has a .tf extension)

terraform init

format terraform file

terraform plan

terraform apply

Head to azure portal to confirm if infrastructure is provisioned

ACR dashboard

AKS dashboard

Containerize the application

  • After resources have been provisioned on azure, we need to login to ACR using credentials (which includes login server, username and password) from container registry => settings => access keys

  • Navigate to your project directory via terminal and login using the details from the step above docker login <login server> --username <username> --password <password>

  • Build docker image using this command docker build -t <name>:<tag> . where . is the current directory.

build image

  • Check the image exists using docker images command ( this lists all available images, the first image is my newly created image).

    List of docker images

  • Push the image to ACR using docker push <name>:<tag>

docker push

  • Going over to azure portal => container registry, to verify the push is successful.

image on azure portal

  • I created a container using my image (exercising how to forward ports, run container in a detached mode, give container a specified name and run the app using the forwarded port on my local machine)

docker run

localhost

Connect to Cluster

  • kubectl will be used to manage the Kubernetes cluster. Run the command az aks get-credentials --resource-group <resourcegroupname> --name <clustername> to configure kubectl and connect to the cluster we previously created and also verify connection using kubectl get nodes to return a list of the cluster nodes.

configure kubectl

  • Kubernetes Deployment: A Deployment is one of the Kubernetes objects that is used to manage Pods via ReplicaSets in a declarative way. It provides updates, control as well as rollback functionalities. This deployment file will be used to:

  • Create a deployment (which automatically creates a replicaset), service and pods.

deployment

  • Lists all objects created at once using kubectl get command

All objects

  • To get more details about the deployment, use kubectl describe deployments <deploymentname> command

describe

  • Scale down replica set from 2 to 4

scale down

  • And also see the change in the events log

describe

event

  • Deleting a pod and showing how a deployment always ensures the desired number of pods is always present.

delete pod

  • Head to azure portal => kubernetes cluster => kubernetes resources => services and ingresses to view ports and pods.

view service

view ports

view pods

  • The app can be accessed using the external IP of the service.

Note keeper app

Challenges encountered during this project

  • Creating a new Azure account due to credit card issues

  • I encountered the ImagePullBackOff and ErrImagePull error which was as a result of not properly connecting my Azure container registry to my Kubernetes cluster; this was detected while debugging.

debug

  • Changing my demo app from a note-keeper app to a basic node app and back to my note-keeper app due to ImagePullBackOff and ErrImagePull error.

Conclusion
This tutorial shows how to:

  • Create a Docker image for a node application

  • Push the image to ACR.

  • Deploy on Kubernetes

The End.

Thank you for reading