Azure Kubernetes Service For Beginners

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, the 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 mentioned below helps explain the above-said things. The container image will contain the application code and its dependencies, but it won’t include the kernel, to interact with the hardware. It will use the host system kernel to interact with the hardware. But virtual machines will have their own kernel, to interact with the hardware. Because of this, the containers are much faster, lightweight, and 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 For Beginners
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.

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 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 For Beginners 

The cluster master consists of the following components,

  • kube-apiserver: This API server helps in running management tools like kubectl.
  • etcd: Used to store the 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 consist 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 the 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 For Beginners 

Run the application and it will run in its default mode as below.

Azure Kubernetes Service For Beginners 

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 For Beginners 
  1. FROM microsoft/dotnet:sdk AS build-env  
  2. WORKDIR /app  
  3.   
  4. # Copy csproj and restore as distinct layers  
  5. COPY *.csproj ./  
  6. RUN dotnet restore  
  7.   
  8. # Copy everything else and build  
  9. COPY . ./  
  10. RUN dotnet publish -c Release -o out  
  11.   
  12. # Build runtime image  
  13. FROM microsoft/dotnet:aspnetcore-runtime  
  14. WORKDIR /app  
  15. COPY --from=build-env /app/out .  
  16. ENTRYPOINT ["dotnet""aspnetapp.dll"]  
The above piece of code tells Docker to use the aspnetcore image from the dockerhub and write your application code on top of it as a different layer to create an image. The entry point is specified as aspnetapp.dll.

Once the Dockerfile is ready, open PowerShell or the Command Prompt to type in the commands to create an image. Once the command prompt is open, navigate to the root of your application. Type-in the following code.

docker build -t aspnetapp . 
 
Azure Kubernetes Service For Beginners 
 
This creates an image with the name ‘aspnetapp’, using the instructions in the docker file.

Then, to run the image as the container, we type the following command,

docker run -d -p 8080:80 --name myaspnetapp aspnetapp 
 
Azure Kubernetes Service For Beginners 
 
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 For Beginners 

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 a 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 For Beginners 

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 For Beginners 

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

docker login yourContainerRegistryLoginServer
 
Azure Kubernetes Service For Beginners 

docker tag yourImageName yourLoginServer/yourRegistryName:yourTag
docker push yourLoginServer/yourRegistryName:yourTag 
 
Azure Kubernetes Service For Beginners 
 
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 For Beginners
 
Azure Kubernetes Service For Beginners 

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 log in 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 For Beginners 
 
Add a path to kubectl as below,
 
Azure Kubernetes Service For Beginners

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

  1. 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 For Beginners
 
Azure Kubernetes Service For Beginners 

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

  1. apiVersion: apps/v1beta1  
  2. kind: Deployment  
  3. metadata:  
  4.   name: my-api  
  5. spec:  
  6.   replicas: 1  
  7.   template:  
  8.     metadata:  
  9.       labels:  
  10.         app: my-api  
  11.     spec:  
  12.       containers:  
  13.       - name: my-api  
  14.         image: democontainer120.azurecr.io/demo:2.0  
  15.         ports:  
  16.         - containerPort: 80  
  17.       imagePullSecrets:  
  18.       - name: [SECRET NAME]  
  19. ---  
  20. apiVersion: v1  
  21. kind: Service  
  22. metadata:  
  23.   name: my-api  
  24. spec:  
  25.   type: LoadBalancer  
  26.   ports:  
  27.   - port: 80  
  28.   selector:  
  29.     app: my-api  
Azure Kubernetes Service For Beginners
 
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 For Beginners 
 
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 For Beginners 
 
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 For Beginners 
 
Now if you go that address, you can find your application running as a container.
 
Azure Kubernetes Service For Beginners 

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 For Beginners 
 
Now if you use the ‘get pods’ command you will see three pods running.
 
Azure Kubernetes Service For Beginners 

Now if you delete any one pod, you will find another instance opens up. That is the number of running pods, which 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!

This article was originally published on my website taagung.