In this section, we will cover a recipe using Microsoft Azure Kubernetes Service (AKS) in order to create a Kubernetes cluster on the Microsoft Azure Cloud.
Getting ready
All the operations mentioned here require a Microsoft Azure subscription. If you don't have one already, go to https://portal.azure.comand create a free account.
How to do it…
This section will take you through how to configure a Kubernetes cluster on Microsoft Azure. This section is further divided into the following subsections to make this process easier:
- Installing the command-line tools to configure Azure services
- Provisioning a managed Kubernetes cluster on AKS
- Connecting to AKS clusters
Installing the command-line tools to configure Azure services
In this recipe, we will get the Azure CLI tool called az and kubectl installed.
Let's perform the following steps:
- Install the necessary dependencies:
$ sudo apt-get update && sudo apt-get install -y libssl-dev \
libffi-dev python-dev build-essential
- Download and install the az CLI tool:
$ curl -L https://aka.ms/InstallAzureCli | bash
- Verify the az version you're using:
$ az --version
- Install kubectl, if you haven't installed it already:
$ az aks install-cli
If all commands were successful, you can start provisioning your AKS cluster.
Provisioning a managed Kubernetes cluster on AKS
Let's perform the following steps:
- Log in to your account:
$ az login
- Create a resource group named k8sdevopscookbook in your preferred region:
$ az group create --name k8sdevopscookbook --location eastus
- Create a service principal and take note of your appId and password for the next steps:
$ az ad sp create-for-rbac --skip-assignment
{
"appId": "12345678-1234-1234-1234-123456789012",
"displayName": "azure-cli-2019-05-11-20-43-47",
"name": "http://azure-cli-2019-05-11-20-43-47",
"password": "12345678-1234-1234-1234-123456789012",
"tenant": "12345678-1234-1234-1234-123456789012"
- Create a cluster. Replace appId and password with the output from the preceding command:
$ az aks create --resource-group k8sdevopscookbook \
--name AKSCluster \
--kubernetes-version 1.15.4 \
--node-vm-size Standard_DS2_v2 \
--node-count 3 \
--service-principal <appId> \
--client-secret <password> \
--generate-ssh-keys
Cluster creation will take around 5 minutes. You will see "provisioningState": Succeeded" when it has successfully completed.
Connecting to AKS clusters
Let's perform the following steps:
- Gather some credentials and configure kubectl so that you can use them:
$ az aks get-credentials --resource-group k8sdevopscookbook \
--name AKSCluster
- Verify your Kubernetes cluster:
$ kubectl get nodes
Now, you have a three-node GKE cluster up and running.
How it works…
This recipe showed you how to quickly provision an AKS cluster using some common options.
In step 3, the command starts with az aks create, followed by -g or --resource-group, so that you can select the name of your resource group. You can configure the default group using az configure --defaults group=k8sdevopscookbook and skip this parameter next time.
We used the --name AKSCluster parameter to set the name of the managed cluster to AKSCluster. The rest of the parameters are optional; --kubernetes-version or -k sets the version of Kubernetes to use for the cluster. You can use the az aks get-versions --location eastus --output table command to get the list of available options.
We used --node-vm-size to set the instance type for the Kubernetes worker nodes. If this isn't set, the default is Standard_DS2_v2.
Next, we used --node-count to set the number of Kubernetes worker nodes. If this isn't set, the default is 3. This can be changed using the az aks scale command.
Finally, the --generate-ssh-keys parameter is used to autogenerate the SSH public and private key files, which are stored in the ~/.ssh directory.
There's more…
Although Windows-based containers are now supported by Kubernetes, to be able to run Windows Server containers, you need to run Windows Server-based nodes. AKS nodes currently run on Linux OS and Windows Server-based nodes are not available in AKS. However, you can use Virtual Kubelet to schedule Windows containers on container instances and manage them as part of your cluster. In this section, we will take a look at the following:
- Deleting your cluster
- Viewing Kubernetes Dashboard
Deleting your cluster
To delete your cluster, use the following command:
$ az aks delete --resource-group k8sdevopscookbook --name AKSCluster
This process will take a few minutes and, when finished, you will receive confirmation of this.
Viewing Kubernetes Dashboard
To view Kubernetes Dashboard, you need to follow these steps:
- To start Kubernetes Dashboard, use the following command:
$ az aks browse --resource-group k8sdevopscookbook --name AKSCluster
- If your cluster is RBAC-enabled, then create Clusterrolebinding:
$ kubectl create clusterrolebinding kubernetes-dashboard \
--clusterrole=cluster-admin \
--serviceaccount=kube-system:kubernetes-dashboard
- Open a browser window and go to the address where the proxy is running. In our example, this is http://127.0.0.1:8001/.
See also
- Microsoft AKS FAQ: https://docs.microsoft.com/en-us/azure/aks/faq
- Repository of the open source core of AKS on GitHub: https://github.com/Azure/aks-engine