If you have some experience with Docker, the next step would be to explore how to deploy docker images in a more manageable way using a platform such as Kubernetes. Kubernetes is an open-source platform for managing containerized workloads and services. In simple terms, it’s a platform to deploy and manage dockerized applications.
Installing a K8 cluster manually is not an easy task. That’s where major cloud providers have implemented this as a service offering. For example Azure has AKS[1] (Azure Kubernetes Service), AWS got EKS[2] (Elastic Kubernetes Service) and Google gives GKE[3] Google Kubernetes Engine. This article is focused on AKS, but Kubernetes concepts and APIs would be the same among all of these cloud providers.
A Kubernetes cluster usually has 2 or more machines, one master node, and 1+ worker nodes. It can be installed manually given that you have 2 machines. The easiest way to try out K8 is using Minikube[5]. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine. Since it is a single node cluster, you can not experiment cluster scaling with Minikube and might not be suitable for production deployment.
As I mentioned before Installing and managing a K8 cluster manually is not an easy task. That’s why it is worth it to use a managed service. In this case, it will be AKS, what is special in AKS is, you don’t have to pay for the master node, but only for the worker nodes.
The following sections are organized in the below order to demonstrate K8s concepts and how you may try out them in AKS.
You may need the following tools to continue with this hands-on section
Note that I have tried this on a Ubuntu OS. If you are using Ubuntu, the following commands will help you to install az-CLI and kubectl. If not please proceed with the above links from official documentation according to your OS. For AKS and ACR you have to have an Azure account, of course, you can start with a free trial.
If you are new to azure I recommend using the Azure portal UI to create the resource group, ACR and AKS. Since it is interactive, you will see what resources are being created in the portal, which is good for a beginner. However, you can use the following commands to do the same with az CLI.
#create a resource group
az group create -g SusiTestResGrp –location=westus
#create acr
az acr create -g SusiTestResGrp –location westus –name vssubscriptionacr –sku Basic
#create aks (2 nodes)
az aks create -g SusiTestResGrp –name=vssubscriptionaks –node-vm-size=Standard_D1 –generate-ssh-keys
Note: ACR is a managed private Docker Registry service from Azure, you may use docker hub[13] too if you are using any other cloud vendor or even with AKS. But to make it simple I’m using ACR here.
Once deployed, the AKS cluster would look like below in Azure portal.
AKS cluster view in Azure portal
Allow AKS pull access to ACR
Kubernetes deploys images by pulling those from a container registry. In our case AKS has to pull them from ACR, therefore we need to provide this permission in Azure.
export CLIENT_ID=$(az aks show -g SusiTestResGrp -n vssubscriptionaks — query “servicePrincipalProfile.clientId” — output tsv); echo $CLIENT_ID
export ACR_ID=$(az acr show -g SusiTestResGrp -n vssubscriptionacr — query “id” — output tsv); echo $ACR_ID
az role assignment create — assignee $CLIENT_ID — role acrpull — scope $ACR_ID
Configure kubectl to use the aks we created
az aks get-credentials -g SusiTestResGrp -n vssubscriptionaks
Once tthe above command is executed it will save the credentials for kubectl, such that next kubectl commands would have access to our AKS cluster
As we have our ACR and AKS setup now, let’s push an image to the ACR first and then deploy it in AKS. Here I assume you already have your docker images or you have Dockerfile to build the images you want. In my case, I’m building a docker image called fftoxml-service.
docker tag fftoxml-service vssubscriptionacr.azurecr.io/integration/fftoxml-service:1.0
docker push vssubscriptionacr.azurecr.io/integration/fftoxml-service:1.0
Here ‘integration’ is the namespace I have used, with namespaces, you can allow sharing a single registry across multiple projects and organize your images. The image below is my ACR having the fftoxml-service I have pushed with a few other images which I have already pushed earlier.
ACR in Azure
You can use the following command to create a deployment in Kubernetes and expose the service (in default namespace). K8s too support namespaces, it helps different projects, teams to share a Kubernetes cluster.
In Addition, the Kubernetes namespace provides the scope for Pods, Services, and Deployments in the cluster. If you want to deploy to a different namespace you may use the — namespace yournamespace with the below commands.
kubectl create deployment fftoxml-service — image=vssubscriptionacr.azurecr.io/integration/fftoxml-service:1.0
kubectl expose deployment fftoxml-service — type=LoadBalancer — port 8081 — target-port 8081
Note: When the Service type is set to LoadBalancer like above, K8s will automatically create a cloud network load balancer. This provides an externally-accessible IP address that sends traffic to the correct port on your cluster nodes
You can use Azure portal, KubernetesDashboard, or kubectl commands to view the deployed cluster. The kebectl get all command will provide pod, service, and deployment details
kubectl get all command output
Once your experiments are done it’s a good idea to clean up the resources if you are no longer using them, unless you will be billed for the cloud resources you have used. I think this is common for any cloud platform.
To clean up everything in one command, you can use the az group delete command. But, be careful with this command as it will delete all resources inside the resource group along with the resource group itself.