azure-kubernetes-service-aks

Azure Kubernetes Service (AKS)

Azure cloud and Kubernetes (K8s) are very important technology.  In this post  we are going to combine both and give a high-level overview of Kubernetes using Azure Kubernetes Service (AKS). We are going to show how to deploy an  application and even load balance it.

Introduction to Kubernetes

Kubernetes is a container orchestrator. This means that it runs your containers, usually Docker containers. Kubernetes can be configured with yaml files, which means that you can store all your configuration in your source control. The smallest unit in Kubernetes is not a container, instead, it uses pods. A pod can contain one or more containers. Additionally, K8s can be easily extended with your own objects. more info look to here.

Kubernetes Features:
  • Service Discovery and Load Balancing
  • Self-healing
  • Automated Deployments
  • Certificate Management
  • Declarative Configuration

Service Discovery and Load Balancing

Load balancing and especially service discovery has always been complicated and required some skills to set up. Both can be achieved in Kubernetes with a Service. This service takes all requests for an application (e.g. a microservice) and load balances this request to an available pods. Further down, we are going to show how to deploy an application with three pods and use the Service to load balance between those pods.

Self-healing

Self-healing means if an application crashes, it gets automatically restarted. Kubernetes does this with health checks. You can provide an URL, often /health and K8s will check if this URL returns a request with the status code 200. If the code is not 200, Kubernetes restarts the pod and marks it as unavailable during the restart. Therefore, no user will be routed to the restarting pods which means that from a user’s perspective everything looks fine.

Automated Deployments

Kubernetes supports two deployment modes out-of-the-box: rolling deployments and blue-green deployments. Requests are only routed to new pods, once they are marked as running. K8s checks this by using readiness probes. They work the same way as health probes, except that they are only used to check if a pod is ready to serve requests.

The rolling deployment mode starts a new pod and once this one is running, it deletes an old one. This process is repeated until all old pods are replaced. You can configure how many pods you want to replace at the same time. This deployment mode is the default.

The blue-green deployment starts all pods of the new version. Once all are running, Kubernetes switches the traffic from all old pods to all new ones. Afterwards, K8s deletes the old pods.

Certificate Management

When a new application is deployed, a new certificate needs to be ordered and then installed. Kubernetes does all that for you. In K8s you can run a certificate manager, for example, let’s encrypt which creates certificates during the deployment and applies them to your application.

Declarative Configuration

Every configuration is done in yaml in a declarative way. This means that you can check-in your files in source control. A declarative configuration means that you tell Kubernetes what you want and it takes care of achieving this. For example, run 10 copies of my application and load balance all incoming traffic. K8s then creates a service and starts 10 pods of your application.

using Azure Kubernetes Service (AKS)

Azure Kubernetes Service is a managed service for Kubernetes.  Kubernetes consists of two parts, the control plane, and the worker node. The control plane does all the tasks which are necessary to manage the Kubernetes cluster. The worker nodes are running applications and everything needed for that, like load balancing. AKS manages the control plane for you, this means that you don’t have to care what’s going on in the background. You can create an AKS cluster and just use it for your applications.

How to setup AKS

obs! check here:

In the Azure Portal, search for aks and select Kubernetes service.

azure-kubernetes-service-aks-1.jpg
search for aks
Then  press to the create button to create a kubernetes Service.

Next, select a resource group, provide a name and a region. For the node size, I have taken Standard Ds2 V2 . Also decrease the count to one, which will also save costs. In a production environment, you should use at least three nodes.

azure-kubernetes-service-aks-2.png

Create a Kubernetes cluster by pressing to  Review +Create button.

azure-kubernetes-service-aks-3.png

 

Click Create button on the last tab to start the deployment.

Start the AKS deployment

The deployment should be finished in a couple of minutes.

azure-kubernetes-service-aks-4.png
kubernetes aks Service: microservice-aks is created

You can test the different information by pressing to the input, output and template to see all information.

Press to the “Go to Resouces” button to see detail of you microservice-aks kubernets service as following:

azure-kubernetes-service-aks-more-6.png

Accessing to the AKS Cluster

First you need download and install Azure CLI module from  here.

you can access your new AKS cluster using PowerShell and using the following command:

az login

 A browser shall be opened then enter your username and password when a browser window is opened.

After you are successfully logged in then connect to  your AKS cluster (microserviceaks), by running the following command on Widnows Powershell.

az aks get-credentials --resource-group microservice-aks_group --name microservice-aks

Then you can see:

Merged "microservice-aks" as current context in C:\Users\default1\.kube\config

This means that a config file has been created C:\Users\default1\.kube\ this config file needed to start octant dashboard, after octant is installed (see bellow)

The next step is install Octant ( Octant is an open source developer-centric web interface for Kubernetes that lets you inspect a Kubernetes cluster and its applications.). In windows start command tool as administrator and  install by using Chocolatey by pasting of following:

>choco install octant --confirm

Use Octant because Azure can’t deploy the Kubernetes dashboard.

Then, start ocant from the windows start by writing octant.

Then it will automatically forward your request and open the dashboard.

azure-kubernetes-service-aks-5.png

To learn more about Octant look here.

Deploy the first Application

To deploy your first application to Kubernetes, you have to define a Service and a deployment. The Service will act as an ingress controller and does the load balancing and the deployment will deploy the defined container with the desired replication count.

You can define both objects inside a single yaml file. First, let’s create the service. In Azure portal  under microservice-aks left menus, click on the Services and Ingress and in the right side press to Create menu as following image:

azure-kubernetes-service-aks-more-7.png

and choose Create with YAML then an editor shall be opened, then past the following code to this Editor and after, press to add button  under of Editor

apiVersion: v1
kind: Service
metadata:
  name: productmicroservice
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: productmicroservice

In the above, service defines itself as load balancer and redirects to port 80 on pods with the label productmicroservice (as a Service name). selector is a label, and by this way Service knows to which pod it should forward a request.

 

azure-kubernetes-service-aks-8.png

As we see in the above figure that the Service: productmicroservice has been create by the above yaml script.

Next step is to create the Deployment object.

apiVersion: apps/v1
kind: Deployment
metadata:
name: productmicroservice
spec:
replicas: 3
selector:
matchLabels:
app: productmicroservice
template:
metadata:
labels:
app: productmicroservice
spec:
containers:
- name: productmicroservice
image: mehzan07/productmicroservice
ports:
- containerPort: 80
Here mehzan07/productmicroservice is a docker image in the my docker hub.
The code above defines a Deployment with the name productmicroservice and sets the label productmicroservice. Next, it configures three replicas, which means that three pods will be created and in the container section, it defines the container and image (mehzan07/productmicroservice)   should  be downloaded and on the port: 80 should be run.

the image should be exist in the docker-hub  with latest tag, if it is not exist you can push the image to your docker-hub.

The above is a yaml file, save it as poductsrv.yml and then run it with kubectl as the following command:

kubectl apply -f path/productsrv.yml

create the productsrv as yml file (first create notepad and after open notpad++ and save as yml type file) in the path C:\Utvecklingprogram\Kubernetes

open cmd command in windows and run the following command.

kubectl apply -f C:\Utvecklingprogram\Kubernetes\productsrv.yml

If command is succeed then you can get :
deployment.apps/productmicroservice created

In case  kubectl is not installed you can install it with the following Powershell command:

Install-Script -Name 'install-kubectl' -Scope CurrentUser -Force
install-kubectl.ps1 [-DownloadLocation <path>]

With applaying the poductsrv.yml  file,  a new Service shall be displayed: productmicroservice, in the Kubernetes dashboard in Octant dashboard.

 

azure-kubernetes-service-aks-19.png
productmicroservice on Octant dashboard External port: 20.166.217.101

In this Service you can see Name, labels, Types, ClusterIP, External IP and Ports. The External IP shall be used to test the application.

The last step click on the deployment, then you can see three pods are running.

azure-kubernetes-service-aks-20.png
3 pods are running

As you can see that all pods are  running and  all running on the same node. In a production environment, they would be running on different nodes to ensure high-availability.

How to Test the Deployment

You can test by two way:

1-  From the Service: take external IP address:(20.166.217.101) and open browser with  URL: 20.166.217.101:80, then you will se Swagger UI is loaded as following image:

azure-kubernetes-service-aks-23.png
Swagger UI is loaded on Ocatant dashboard

2. The second way is: Start Port Forward by clicking on  the service or one of the pods as follow:

azure-kubernetes-service-aks-22.png

Then click on the Start Port Forward then you can see the following image

azure-kubernetes-service-aks-21.png

By pressing on the  url =http://localhost:56664,  browser shall display the Swagger UI as following:

azure-kubernetes-service-aks-24.png
Swagger UI loaded with Port forward on a pod.

Notice 2: In case Swagger UI not loaded (website can’t be access, or like this) then one reason can be Swagger UI configuration in your Startup.cs file,  is only for Development environment. Check Startup.cs  in the Configure() method and change it as following code:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Swagger should be out of Dev Environment, should be load in both production and Dev environment
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Product API V1");
c.RoutePrefix = string.Empty;
});

In the end Cleanup all resources

When you are finished,  delete all created resources. AKS creates three additional resource groups. Make sure to delete them too.

Delete all resource groups

Conclusion

In this post we gone threw an overview of Kubernetes using Azure Kubernetes Service. Kubernetes is for helping you to run  container and manages deployments and load balancing.

This post was a very simple Microservice application and we haven’t talked about any downsides of Kubernetes and more ..

In my next post, I will describe Helm Kubernetes

This post is part of “Kubernetes step by step”.

Back to home page

.

Leave a Reply

Your email address will not be published. Required fields are marked *