At the end of October 2017, Microsoft announced the release of Azure Kubernetes Service (AKS), its hosted version of Kubernetes. If you’re new to AKS and curious about how to get a proof of concept (PoC) set up in your environment, read on. In this AKS tutorial, you’re going to learn, step-by-step, how to get an Azure Kubernetes cluster built with AKS.
What is AKS?
Before Kubernetes hosting services like AKS, you had no choice but to deploy a self-hosted Azure Kubernetes cluster. This meant you had to manage the servers, pick a networking protocol, have scripts that would configure the master/worker nodes, and manage the infrastructure. This was a burden for most people because they simply wanted Kubernetes to do one thing: orchestrate Docker containers. This is where AKS shines.
AKS is a managed Kubernetes platform that eliminates the need to maintain the infrastructure that the Kubernetes master node lives on. Proceed to other articles on monitoring AKS and comparing AKS to AWS EKS and GKE.
Step 0: Prerequisites
To successfully follow the steps in this blog post, you will need:
- An Azure subscription. You can get a 30-day free trial here.
- An installed version of Azure CLI. You can find one here. You will need at least version 1.2.1.
Step 1: Configuring the Azure CLI
The Azure CLI is the command line interface that allows a user to interact with Azure services without needing to use the Azure portal. The AZ CLI provides a quick, easy, and efficient way to create, list, and modify resources on the fly at a terminal.
In this section, you will configure the AZ CLI for authentication to Azure. Authentication to Azure is needed for interacting with Azure services at the command line. After the authentication with AZ CLI is set up, you will need to choose an Azure subscription based on what’s available in the Microsoft account. Depending on the Microsoft account being used, it may or may not have multiple subscriptions. If there’s only one subscription within the account that is being used to authenticate to Azure with AZ CLI, that subscription will be set to default.
Authenticating with “az login”
Open up a terminal and type “az login,” which is used to start the authentication process.
A sign in page will appear.
Type in the Microsoft email being used to authenticate to Azure, and click the blue Next button.
Enter the password for the Microsoft email as shown in the screenshot below, then click the blue Sign in button.
If the password was correct, a successful login web page will appear.
When you go back to the terminal, JSON data should show the default Azure subscription information, as illustrated below:
Choosing a Subscription
If there is more than one subscription available in the Azure account, you’ll need to choose one to use. Running az account list –all will show you the account’s available subscriptions. You can select and set a default subscription by running az account set –subscription name_or_id_of_subscription.
Step 2: Creating a Resource Group for Your Azure Kubernetes Cluster
In the previous section, you learned how to authenticate to Azure with Azure CLI. Now that the authentication process is complete, it’s time to create a resource group for the AKS cluster. A resource group is a set location that stores the Azure resources being created.
Creating a Resource Group
The AZ CLI resource that will be used is az group. To create a resource group, run the following command:
az group create --location azure_region --name name_of_resource_group
The two parameters used are:
--name = The name of the resource group you’re creating.
--location = The Azure region being used. For a full list of available regions, check out the list here.
Step 3: Configuring the AKS Cluster
In the previous section, you created a resource group to store the AKS cluster. Now it’s time to create the AKS cluster itself.
Picking a Kubernetes API Version
As previously discussed, Azure helps administer the Kubernetes cluster in AKS. With that, AKS manages the Kubernetes API. The Kubernetes API sits on the master node, and no one outside of Microsoft has access to it. As a result, you cannot pick any Kubernetes API version you’d like; you must choose one that is supported by Azure. The available Kubernetes API versions may differ depending on the region that the AKS cluster will be residing in.
To learn what Kubernetes API versions are available in each region, run the following AZ CLI command:
az aks get-versions --location region_name
Using Azure AKS to Deploy Azure Kubernetes Services
If you take a look at the AZ CLI documentation for creating an AKS cluster, you’ll see a ton of parameters, most of which are optional. After looking at the documentation, go back to the terminal.
The command below will show the most important parameters to get an AKS cluster up and running with Kubernetes API version 1.17.0. The AZ CLI resource that will be used is az aks. Enter the correct resource group name and AKS cluster name, then run the command line below to create an AKS cluster with one worker node:
az aks create --resource-group name_of_resource_group --name name_of_aks_cluster --node-count 1 --kubernetes-version 1.17.0
The parameters used are:
--resource-group = The name of the resource group that you created in the previous section.
--name = The name of the AKS cluster.
name-count 1 = The number of worker nodes that will be running. In a dev environment, one node is fine. For redundancy and clustering purposes, all production environments should have at least two nodes.
--kubernetes-version = The Kubernetes API version that will be used in the AKS cluster.
As shown in the screenshot below, multiple JSON blocks will be printed to the terminal with all of the AKS information.
Using AZ CLI to Confirm that the AKS Cluster Exists
Now that the AKS cluster is created, you can see if the AKS cluster is up and running by using AZ CLI. Run the following command to show the existing AKS cluster JSON output on a terminal:
az aks show --name name_of_aks_cluster --resource-group name_of_resource_group
Step 4: Configuring the Kubernetes Configuration (Kubeconfig) Locally
In the previous section, you learned about the Kubernetes API version offerings from Azure, what components of AKS Azure manages for you, how to create an AKS cluster, and how to confirm that the AKS cluster exists. In this section, you will take that setup a step further and begin configuring it.
Cloning the Kubeconfig
Once the cluster is created, it can be managed locally on a terminal—much like everything you’ve done with Azure CLI so far. To manage the AKS cluster locally, a kubeconfig, the Kubernetes configuration of the AKS cluster, will need to be cloned to a local machine. The AZ CLI resource that will be used is az aks.
Run the command below to copy the kubeconfig to a local computer:
az aks get-credentials --name name_of_aks_cluster --resource-group name_of_resource_group
The output will be similar to what is shown in the screenshot below, letting you know that the kubeconfig has been stored in the default location.
Accessing AKS from Localhost
The kubectl commands allow a user to interact with the Kubernetes API to manage deployments, pods, nodes, and all other Kubernetes resources. To get started with kubectl, run the following command to ensure that you can view the nodes:
kubectl get nodes
To learn more about kubectl, visit this link for a guide to getting started.
Step 5: Deploying a Demo Application
Now it’s time to create a Kubernetes deployment. To do so, you can use one of the existing examples in GitHub. Here, we’ll use the example of spinning up an Nginx deployment. Nginx is an open-source web server.
Run the command below to create an Nginx deployment:
kubectl create -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/controllers/nginx-deployment.yaml
The Kubernetes deployment will be created as shown in the screenshot below.
Once the deployment is created, use kubectl to check on the deployments by running this command:
kubectl get deployments
The output will be the Nginx deployment, as shown in the screenshot below.
Congratulations! You have successfully created an AKS cluster and deployed an application.
If you want to be successful in orchestrating containers, Kubernetes is the way to go. Not only is it the most popular tool for this job, it is also the most efficient platform for the container scaling needed in development and production environments.
This blog post examined what AKS is and why using it benefits any organization that wants to manage and scale in the container and orchestration world. We demonstrated using the Azure CLI to create and deploy containers, deploy a Kubernetes AKS cluster in Azure, and retrieve the Kubernetes configuration (kubeconfig) from Azure. Finally, we deployed a demo Nginx application to the Kubernetes cluster. Armed with this new knowledge, you should now be able to successfully deploy a Kubernetes cluster with Azure Kubernetes Service.