Reading time: 15 minutes
Kubernetes is an open source project from Google that orchestrates\manages containerized environments – Docker, etc. Azure Kubernetes Service (AKS) is a cloud managed Kubernetes service that lets you quickly deploy and manage clusters using Azure resources (Storage, Network, etc). In this tutorial, you will deploy an AKS cluster using the Azure CLI (from the Cloud Shell). The final application will be a multi-container application that includes a python web front end and a backend Redis instance that is run in the cluster – Azure Voting App.
This application deployment ties into the concept of “microservices”. Basically, splitting up large monolithic systems or applications into smaller pieces in order to de-couple the components and build a robust solution that can quickly scale horizontally (The new way). Instead of into vertical silos (the old way)
- Microsoft Azure tenant
Ready, set, go!
Login to Portal.azure.com
Select the Cloud Shell button on the Portal menu (located in the upper-right corner)- use the bash shell command line:
Setup the Azure Cloud Shell (First time use)
When you open the Cloud Shell for the first time, you will be prompted for the environment (Click -> Bash) and also with a message saying “You have no storage mounted” to create and mount a storage account. As you are aware, Azure uses resource groups as a container for cost analysis and a logical grouping of system and storage resource providers (This ties into powering the Azure Resource Manager aka ARM).
You have two options for setting up the storage account for the Cloud Shell:
- Use the default storage account options:
Change to the appropriate Subscription (In my case, I only have one subscription available). Then click the “Create storage” button
You will notice that the storage account and resource group are automatically created.
2. You can create a custom name for the resource group, storage account, and file share settings using the “Show advanced settings” option (This is my preferred choice – since I like to use a custom naming convention for all of my Azure resources). Then click Create storage.
Create a new resource group
First let’s create the resource group. Type the following command into the command line. Feel free to change the location to a closer Azure datacenter.
az group create --name RG-Kubernetes-test --location westus2
The following example output shows the resource group created successfully:
Create the AKS cluster
Next we’ll use the az aks create command to create an AKS cluster. The following example creates a cluster named AKS-Cluster-test with one node. Azure Monitor for containers is also enabled using the –enable-addons monitoring parameter.
az aks create --resource-group RG-Kubernetes-test --name AKS-Cluster-test --node-count 1 --enable-addons monitoring --generate-ssh-keys
Wait a few minutes for the command to complete and return JSON-formatted information about the cluster.
Now you can open the RG-Kubernetes-test resource group and see the AKS Cluster that we created called “AKS-Cluster-test”
Connect to the cluster
To manage a Kubernetes cluster, we use kubectl, the Kubernetes command-line client. Luckily, the kubectl client is already installed in the Azure Cloud Shell.
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
Let’s verify that the connection to the cluster was established by using the kubectl get command – which returns a list of the cluster nodes.
kubectl get nodes
Create and run the test application
This manifest includes two Kubernetes deployments – one for the sample Azure Vote Python applications, and the other for a Redis instance. Two Kubernetes Services are also created – an internal service for the Redis instance, and an external service to access the Azure Vote application from the internet.
Copy and paste the following content:
spec: replicas: 1
- name: azure-vote-back
- containerPort: 6379
- port: 6379
spec: replicas: 1
- name: azure-vote-front
- containerPort: 80
- name: REDIS
- port: 80
Deploy the application using the kubectl apply command and specify the name of the YAML manifest file:
kubectl apply -f azure-vote.yaml
Test the application
Check on the status of the application deployment by running the following command:
kubectl get service azure-vote-front --watch
Once the EXTERNAL-IP address changes from pending to an actual public IP address, use CTRL-C to stop the kubectl watch process.
The following example output shows a valid public IP address assigned to the service:
Finally the moment of truth. Copy and paste the external-IP (Public IP address) information into a browser:
My favorite way to view my Kubernetes setup is using the Kubernetes dashboard. Come on, I’m a Windows guy :-). However, this does not work from the Azure Cloud Shell unfortunately without some tweaking of the security.
Get the credentials for the cluster that we created by running the following command:
az aks get-credentials --resource-group RG-Kubernetes-test --name AKS-Cluster-test
Grant the Azure Cloud Shell system account permission to the Kubernetes dashboard by running the following command. ***Note: This is for testing and is not recommended for Production environments :
kubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard
Finally view the Kubernetes Dashboard by running the following aks browse command (it should automatically open in a browser window):
az aks browse --resource-group RG-Kubernetes-test --name AKS-Cluster-test
If you click the Pods link, then you can see the front and back end pod pools. Notice that were are only running a single pod in each node pool (shown as 1/1):
We can easily scale horizontally by using the Scale option (Top of dashboard):
Then adding more pods to the replica set. A pod is a single process or job running in the cluster- (the analogy comes from a pod of whales or pea pod)
Cleanup our deployment
It’s good practice to cleanup Azure resources after you are finished with your testing. Why? Because even as you move on to do other things in Azure (forgetting about our Kubernetes test deployment), the cost keeps adding up in the background (time is literally money).
The fastest way to cleanup our deployment is to delete the Resource Group that we created earlier called “RG-Kubernetes-test”. Go back to the Cloud Shell and type the following:
az group delete -n RG-Kubernetes-test
Go to the Azure portal page -> Resources Groups (Blade) -> Delete resource group
Type the name of the Resource group -> Delete
Learn more about Kubernetes: