Provisioning Azure Kubernetes Service

In this section, we will provision Azure Kubernetes Service using Azure CLI. Azure Kubernetes Service can also be provisioned from the Azure portal and Azure PowerShell:

  1. Log into Azure using the following command:
az login  
  1. Select an appropriate subscription using the following command:
az account set -subscription <<xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx>>  
  1. The Azure Kubernetes Service features are available from the az aks command.
  2. Create a new resource group to host Azure Kubernetes Service using the following command:
az group create --name "akdemo" --location "west Europe"  
  1. Once the resource group is created, we can create the Kubernetes cluster using the following command:
az aks create --resource-group akdemo --name AKSPortalCluster --node-count 2 --enable-addons monitoring --generate-ssh-keys

  1. This command takes a while to create the Kubernetes cluster.
  2. After the cluster is provisioned, it is possible to download the Kubectl CLI tool using the Azure CLI and configure it to work without the cluster.
  3. The command to download the Kubectl tool is az aks install-cli:

It is important to note that the Kubectl tool should be in the search path to execute it on the command line by its name. If it's not in the path, then the entire path of the Kubectl tool should be provided every time it needs to be executed. An ideal approach is to add it to the search path using the following command:

set PATH=%PATH%;C:\Users\citynextadmin\.azure-kubectl  

After executing this command, it is possible to execute the kubectl command:

At this stage, the Kubectl tool is able to be executed but it is still not connected to our recently provisioned cluster. Kubectl needs credentials configured such that it can be authenticated by the cluster. Please note that we did not provide any credential information when creating the cluster and the credentials were generated by Azure Kubernetes Service when provisioning the cluster.

These credentials can be downloaded, and Kubectl can be configured using the command provided by Azure Kubernetes Service on the Azure CLI:

The next step is to create pods on the cluster. For this, we will create a new YAML file and submit it to the API server using the commands available in the Kubectl CLI tool.

The YAML file should be saved on a local machine such that it can be referenced from the command line. It is not possible to explain the YAML file in this book, but there are other books that cover YAML and Kubernetes configurations in detail. The sample YAML is listed here:

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: azure-vote-back 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: azure-vote-back 
  template: 
    metadata: 
      labels: 
        app: azure-vote-back 
    spec: 
      containers: 
      - name: azure-vote-back 
        image: redis 
        resources: 
          requests: 
            cpu: 100m 
            memory: 128Mi 
          limits: 
            cpu: 250m 
            memory: 256Mi 
        ports: 
        - containerPort: 6379 
          name: redis 
--- 
apiVersion: v1 
kind: Service 
metadata: 
  name: azure-vote-back 
spec: 
  ports: 
  - port: 6379 
  selector: 
    app: azure-vote-back 
--- 
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: azure-vote-front 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: azure-vote-front 
  template: 
    metadata: 
      labels: 
        app: azure-vote-front 
    spec: 
      containers: 
      - name: azure-vote-front 
        image: microsoft/azure-vote-front:v1 
        resources: 
          requests: 
            cpu: 100m 
            memory: 128Mi 
          limits: 
            cpu: 250m 
            memory: 256Mi 
        ports: 
        - containerPort: 80 
        env: 
        - name: REDIS 
          value: "azure-vote-back" 
--- 
apiVersion: v1 
kind: Service 
metadata: 
  name: azure-vote-front 
spec: 
  type: LoadBalancer 
  ports: 
  - port: 80 
  selector: 
    app: azure-vote-front

The file was saved as aksdemo.yaml. This YAML file is responsible for creating two types of pod. Each of these types should be used to create a single instance. It short, there should be two pods created, one for each type. Both the pod specifications are available in the YAML file within the template section.

The YAML file can be submitted using the apply command:

At this stage, nodes can be queried as follows:

Pods can be queried as shown in the following screenshot:

Once the pods are created, they should show a Running status. However, its not only the pods that needed to be created. There are more resources to create on the cluster as shown in next screenshot.

This includes two services. One service, of the ClusterIP type, is used for internal communication between pods, and the other one, of the LoadBalancer type, is used for enabling external requests to reach to the pod. The configuration of services is available in the YAML file as kind: Service. There are two deployments as well; deployments are resources that manage the deployment process. The configuration of services is available in the YAML file as kind: Deployment.

Two replica sets are also created, one for each pod type. Replica sets were previously known as replication controllers, and they ensure that the desired number of pod instances are always maintained within the cluster. The configuration of services is available in the YAML file as the spec element: