Azure Kubernetes Service

Azure Kubernetes Service (AKS) For Beginners

Tuba Mansoor
Latest posts by Tuba Mansoor (see all)

Kubernetes is an open source system started by Google to help orchestrate (deploy, scale and manage) containerized applications. Azure Kubernetes Service, makes working with Kubernetes easier.

Before we learn how to orchestrate containers, let’s discuss a bit about containers.

You can run your applications as containers. Think about containers as isolated processes that have their own directory, user groups, ports etc which run in the host machine. They are like virtual machines, but not the same. Virtual machines have their own operating systems, whereas containers use the operating system of the host machine. Also containers run from images. Think of images as software installers. But images bundle up the software code with its dependencies, because of which containers run the same way on different environments, since they are environment independent to a much larger extent.

The diagram below, helps explains the above said things. The container image will contain the application code and it’s dependencies, but it won’t include the kernel, to interact with the hardware. It will use host system kernel to interact with the hardware. But virtual machines will have their own kernel, to interact with the hardware. Because of this containers are much more faster, light weight & easier to manage than virtual machines. Hence, in the new way of designing applications as microservices, the services are run as containers.

Azure Kubernetes Service

Dockers & containers are terms which are often used together. Docker helps in managing containers. Containers have existed since long in Linux, but it was not easy to manage them. Docker is a software that helps us in creating images, running them as containers, removing containers etc.

Before proceeding I would strongly advise reading more about dockers & containers here. This talks more about how dockers can be used to manage containers, what is docker hub & how to upload images to docker hub etc.

See also  ASP.NET Core: The MVC Request Life Cycle

You might also want to check out this article here, which describes how to run containers as Azure Container Instances or ACIs.

Azure Kubernetes Service (AKS) helps manage your applications running as containers. Azure Kubernetes Service provides many services like load balancing, autoscaling etc.

The AKS cluster contains of a cluster master and multiple nodes. Cluster master deals with the management side of things whereas nodes run the actual workload.

The below diagram is taken from the official Microsoft documents for AKS. We will discuss the important components in AKS.

Azure Kubernetes Service

The cluster master consists of the following components:

kube-apiserver: This API server helps in running management tools like kubectl.

etcd: Used to store state of kubernetes cluster & configuration.

kube-scheduler: Determines which nodes will handle will run the workload.

kube-controller-manager: Manages pod replication etc.

The nodes consists of containers amongst other components. Containers are not run directly but are wrapped in units called pods. Pods can run one or more containers, but generally they run a single instance of a container. Pods are the basic building block of kubernetes.

During deployment, we create a deployment yaml file that lists the pod replica sets that need to be created and other information. We will create a yaml file during our demo.

There are two namespaces that we must be aware of in kubernetes: the default namespace will house our deployments if no namespace is provided & the kube-system namespace will contain system resources.

We have covered the important terminology that is required to execute following demo. For further details visit here which contains official information from Microsoft. This article from Medium also has some excellent information.

Let’s have a quick demo. But before proceeding reading my article on Docker & Containers & ACI. These will help understand the demo better.

Step 1: Creating an asp.net core application

We will need Docker to manage our containers. So install ‘Docker for Windows’ from here.

Then, I will create an asp.net core application using Visual Studio. Check the ‘Enable Docker Support’ checkbox while creating the project.

Azure Kubernetes Service

Run the application and it will run in it’s default mode as follows:

Azure Kubernetes Service


Step 2: Containerizing the application

Let’s create an image from our application. We will write some instructions for the same, in the dockerfile that gets created as below.

Azure Kubernetes Service

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

The above piece of code, tells Docker to use the aspnetcore image from dockerhub and write our application code on top of it as a different layer to create an image. The entry point is specified as aspnetapp.dll.

See also  Docker Tutorial

Once the dockerfile is ready, open powershell or command prompt to type in the commands to create an image. Once the command prompt is open, navigate to the root of our application. Type in the following code.

docker build -t aspnetapp .
Azure Kubernetes Service

This creates an image with the name ‘aspnetapp’, using the instructions in the docker file.

Then to run the image as container we type in the following command:

docker run -d -p 8080:80 --name myaspnetapp aspnetapp
Azure Kubernetes Service

This tells docker to run the image aspnetapp in a container called ‘myaspnetapp’. It also specifies that the left port which is mentioned is the host port, whereas the right port which is mentioned is the container port. You can mention any ports of your choice.

Now to check our containerized application, we goto 8080 port of localhost. We can find our application running there.

Azure Kubernetes Service

Now that we have successfully run our application as a container, we will now push our image to Azure Container registry.

Step 3: Deploying your image to Azure Container Registry

Now that you have created the application image and are able to run your application as container, let’s deploy our image on Azure Container Registry. Once we have deployed our image on Azure, we can use it to run our Kubernetes.

So go to Azure Portal, and create an Azure Container Registry as follows:

Azure Kubernetes Service

Remember you can find your username & password in the ‘access keys’ section. You will need them to push your image to the Azure Container Registry.

We will also create a repository called ‘demo’ as below. We will give it a tag 2.0.

Azure Kubernetes Service

Open powershell and type in the following code to login to your container registry with the access keys as discussed above.

docker login yourContainerRegistryLoginServer
Azure Kubernetes Service
docker tag yourImageName yourLoginServer/yourRegistryName:yourTag
docker push yourLoginServer/yourRegistryName:yourTag
Azure Kubernetes Service

This pushes your application to Azure Container registry.

Now that we have uploaded our image to Azure Container Registry, let’s learn how to use Kubernetes.

Step 4 : Implementing Kubernetes

Create a Kubernetes service in Azure as below:

Azure Kubernetes Service
Azure Kubernetes Service

Install the Azure CLI from here.

Open powershell and type in the following command to install kubernetes cli.

az aks install-cli

Then type in the following command to login to Azure.

az login

Type in the following command to import your aks credentials.

az aks get-credentials -g RESOURCE_NAME -n CLUSTER_NAME
Azure Kubernetes Service

Add a path to kubectl as below:

See also  Angular Tutorial For Beginners - Part 7 : Pipes
Azure Kubernetes Service

Before proceeding you will need to create a secret name as below:

kubectl create secret docker-registry SECRET_NAME --docker-server=REGISTRY_NAME.azurecr.io --docker-username=USERNAME --docker-password=PASSWORD --docker-email=ANY_VALID_EMAIL

You can provide any secret name eg. ‘something.com’. The registry name for me will be ‘demoContainer120’ that I had created earlier. ‘Docker username’ & ‘docker password’ will be the container registry username & password found in the ‘Access keys’ section as below.

Azure Kubernetes Service
Azure Kubernetes Service

Create a yaml file as below. Enter your Azure container registry name & secret name as shown below in red rectangles.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: my-api
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: my-api
        image: democontainer120.azurecr.io/demo:2.0
        ports:
        - containerPort: 80
      imagePullSecrets:
      - name: [SECRET NAME]
---
apiVersion: v1
kind: Service
metadata:
  name: my-api
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: my-api
Azure Kubernetes Service

Navigate to the folder where your yaml file is kept and run the below command to start your deployment.

kubectl create -f [your deployment].yml
Azure Kubernetes Service

Now your cluster is up and running. You can check the number of pods running using the following command. In our deployment file we had specified that we need only one replica set, hence we can see only one pod running.

kubectl get pods
Azure Kubernetes Service

Type in the following command to know the external IP of where your container is running.

kubectl.exe get service/my-api -w
Azure Kubernetes Service

Now if you go that address, you can find your application running as a container.

Azure Kubernetes Service

You can also autoscale your deployment using the below command.

kubectl autoscale deployment my-api --min=3 --max=3 --cpu-percent=80
Azure Kubernetes Service

Now if you use the ‘get pods’ command you will see three pods running.

Azure Kubernetes Service

Now if you delete any one pod, you will find another instance opens up. That is the number of running pods is maintained at 3.

If you want to delete your pods and stop running your cluster, you need to delete your deployment as below:

kubectl delete deployment xyz-deployment

Kubernetes is a huge topic and this article has barely scratched the surface. But I hope it helps you begin your journey with Kubernetes. Below are a few links you might find helpful:

https://azure.microsoft.com/mediahandler/files/resourcefiles/kubernetes-learning-path/Kubernetes%20Learning%20Path%20v4.pdf

https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough-portal

https://kubernetes.io/

Happy learning!