Kubernetes Azure: how to run Kubernetes on Azure

It is possible to create a Kubernetes cluster either through the Azure Web interface or via command line.

Kubernetes Azure: everything you need to know

Let’s see how to perform Kubernetes installation on MS Azure via command line.

Preparing the shell is the first step!

Two options are available: one is to use Azure’s interactive shell, the other is to install Azure’s command-line tools locally. Instructions for each are given below.

1 – Azure Interactive Shell

The Azure portal contains an interactive shell that you can use to communicate with your Kubernetes cluster. To access this shell, go to portal.azure.com log in and click the button at the bottom.

../../_images/cli_start.png

Tag alt = Kubernetes Azure

2 – Command line tools locally

You can access the Azure command line interface through a package that you can install locally.

To do this, first follow the installation instructions in the Azure documentation. Then run the following command to connect your local CLI with your account:

az login

You will need to open a browser and follow the instructions in your terminal to access.

Activate subscription

Azure uses the concept of subscriptions (subscriptions) to manage costs. You can get a list of subscriptions to which your account has access by running:

az account list –refresh –output table

Choose the subscription you want to use to create the cluster and set it as the default. If you have only one subscription, you can ignore this step.

az account set –subscription <SUBSCRIPTION-NAME>

Create a resource group

Azure uses the concept of resource groups to group related resources. We need to create a resource group at a given location in the data center. We will create compute resources within this resource group.

az group create \

   –name=<RESOURCE-GROUP-NAME> \

   –location=centralus \

   –output table

Set a name for the cluster

In the following steps we will run commands that ask you to enter a cluster name. We recommend that you use something descriptive and short. We will refer to this as <CLUSTER-NAME> for the rest of this section.

The next step will create some files on your filesystem, so first create a folder where these files will go. We recommend that you give them the same name as your cluster:

mkdir <CLUSTER-NAME>

cd <CLUSTER-NAME>

Create an SSH key

ssh-keygen -f ssh-key-<CLUSTER-NAME>

You will be asked to add a password, which you can leave blank if you wish. At this point a public key named ssh-key-<CLUSTER-NAME>.pub and a private key named ssh-key-<CLUSTER-NAME> will be created. Make sure they both go into the folder we created earlier and keep them both safe!

Create a virtual network and sub-network

Kubernetes does not have a controller by default that enforces network resources and policies. Networkpolicy resources are important because they define how Kubernetes pods can communicate securely with each other and with the outside world, e.g., the Internet.

To enable it in Azure, we must first create a virtual network with Azure’s network policies enabled.

az network vnet create \

   –resource-group <RESOURCE-GROUP-NAME> \

   –name <VNET-NAME> \

   –address-prefixes 10.0.0.0/8 \

   –subnet-name <SUBNET-NAME> \

   –subnet-prefix 10.240.0.0/16

We will now retrieve the application IDs of the virtual network and subnet we just created and save them in the bash variables.

VNET_ID=$(az network vnet show \

   –resource-group <RESOURCE-GROUP-NAME> \

   –name <VNET-NAME> \

   –query id \

   –output tsv)

SUBNET_ID=$(az network vnet subnet show \

   –resource-group <RESOURCE-GROUP-NAME> \

   –vnet-name <VNET-NAME> \

   –name <SUBNET-NAME> \

   –query id \

   –output tsv)

We will create an Azure Active Directory (Azure AD) service entity to use with the cluster and assign the collaborator role to use with the virtual network. Make sure that SERVICE-PRINCIPAL-NAME is something recognizable, for example, binderhub-sp.

SP_PASSWD=$(az ad sp create-for-rbac \

   –name <SERVICE-PRINCIPAL-NAME> \

   –role Contributor \

   –scopes $VNET_ID \

   –query password \

   –output tsv)

SP_ID=$(az ad sp show \

   –id http://<SERVICE-PRINCIPAL-NAME> \

   –query appId \

   –output tsv)

Create the Kubernetes cluster

At this point, you might be thinking about provisioning your Kubernetes cluster. The following command will create a Kubernetes cluster (link to the What is Kubernetes article) within the resource group we created earlier.

az aks create \

   –name <CLUSTER-NAME> \

   –resource-group <RESOURCE-GROUP-NAME> \

   –ssh-key-value ssh-key-<CLUSTER-NAME>.pub \

   –node-count 3 \

   –node-vm-size Standard_D2s_v3 \

   –service-principal $SP_ID \

   –client-secret $SP_PASSWD \

   –dns-service-ip 10.0.0.10 \

   –docker-bridge-address 172.17.0.1/16 \

   –network-plugin azure \

   –network-policy azure \

   –service-cidr 10.0.0.0/16 \

   –vnet-subnet-id $SUBNET_ID \

   –output table

This should take a few minutes and provide you with a working Kubernetes cluster!

Install kubectl

If you are using the Azure CLI locally, install kubectl, a tool for accessing the Kubernetes API from the command line:

az aks install-cli

Note: kubectl is already installed in Azure Cloud Shell.

Get credentials for kubectl

az aks get-credentials \

   –name <CLUSTER-NAME> \

   –resource-group <RESOURCE-GROUP-NAME> \

   –output table

This command will automatically update the Kubernetes configuration file.

Verify that the cluster is working

kubectl get node

The response should list three running nodes and their Kubernetes versions! Each node should have the status of Ready. Keep in mind that this may take a few moments.

In conclusion

We have seen how to create a Kubernetes cluster in a few simple steps. We always advise you to refer to the official Azure documentation for further details.

Share: Facebook Twitter Linkedin

Comments