Back to Blog

Creating Kubernetes Clusters with AKS

Complete tutorial about az aks create in Azure CLI. Learn Kubernetes, AKS, node pools.

Creating Kubernetes Clusters with AKS

Creating Kubernetes Clusters with AKS

Introduction

Azure Kubernetes Service (AKS) is a managed container orchestration service that simplifies the deployment, management, and operations of Kubernetes clusters in Azure. With AKS, you can quickly create a Kubernetes cluster, allowing you to run containerized applications efficiently. The service abstracts away much of the complexity involved in managing Kubernetes, enabling developers to focus on building and deploying applications rather than managing infrastructure.

Using the az aks create command, you can create a Kubernetes cluster that is capable of handling production workloads, scaling automatically based on demand and integrating seamlessly with other Azure services. Some of the key use cases include deploying microservices, running CI/CD pipelines, and managing containerized applications.

This tutorial will guide you through the process of creating Kubernetes clusters using AKS, setting up node pools, and managing your cluster with practical examples.

Prerequisites

Before you begin, ensure you have the following:

  1. Azure CLI: Installed and updated to version 2.35.0 or later.

    • Check your version with az --version.
    • If not installed, follow the instructions here.
  2. Azure Subscription: An active Azure subscription is required. You can create a free account here.

  3. Permissions: Ensure you have sufficient permissions (Owner or Contributor role) in the Azure subscription to create resources.

  4. Authentication: Authenticate to Azure CLI using az login to access your Azure account.

Fundamental Concepts

  • Kubernetes: An open-source platform for automating the deployment, scaling, and operations of application containers.
  • AKS: A managed Kubernetes service that simplifies the process of deploying and managing Kubernetes clusters.
  • Node Pool: A group of nodes within a Kubernetes cluster that all have the same configuration. You can scale node pools independently to meet the demands of your applications.

Command Syntax

The syntax for creating a Kubernetes cluster using AKS is as follows:

az aks create --resource-group <resource_group_name> --name <cluster_name> --node-count <num_nodes> --node-vm-size <vm_size> --generate-ssh-keys
Parameter Description
--resource-group The name of the resource group to create the AKS cluster in.
--name The name of the AKS cluster.
--node-count The number of nodes to create in the cluster.
--node-vm-size The size of the virtual machines for the nodes.
--generate-ssh-keys Automatically generate SSH keys for the cluster.
--attach-acr Attach an Azure Container Registry for pulling images (optional).

Practical Examples

Example 1: Create a Basic AKS Cluster

az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --node-vm-size Standard_DS2_v2 --generate-ssh-keys

This command creates a basic AKS cluster named myAKSCluster with 3 nodes of the size Standard_DS2_v2 in the resource group myResourceGroup.

Example 2: Create AKS Cluster with ACR Integration

az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --node-vm-size Standard_DS2_v2 --generate-ssh-keys --attach-acr myContainerRegistry

In this example, the cluster is also attached to an Azure Container Registry named myContainerRegistry for seamless image pulling.

Example 3: Create AKS Cluster with Additional Node Pool

az aks nodepool add --resource-group myResourceGroup --cluster-name myAKSCluster --name mynodepool --node-count 2 --node-vm-size Standard_DS2_v2

This command adds an additional node pool named mynodepool with 2 nodes to the existing AKS cluster.

Example 4: Scale AKS Cluster Nodes

az aks scale --resource-group myResourceGroup --name myAKSCluster --node-count 5

This command scales the number of nodes in the existing AKS cluster to 5.

Example 5: Get AKS Cluster Credentials

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

Retrieve the credentials to manage the AKS cluster using kubectl.

Example 6: List Nodes in AKS Cluster

kubectl get nodes

This command lists all the nodes in the AKS cluster, showing their status and other details.

Example 7: Create a Kubernetes Deployment

kubectl create deployment myApp --image=myContainerRegistry/myApp:latest

Deploy an application using a container image from the Azure Container Registry.

Example 8: Expose the Application via a LoadBalancer

kubectl expose deployment myApp --type=LoadBalancer --port=80 --target-port=8080

Expose your application to the internet by creating a LoadBalancer service.

Real-World Use Cases

Scenario 1: Microservices Architecture

A company can deploy a microservices-based application using AKS, where each microservice runs in its own container. By using multiple node pools, the company can scale individual services based on their specific loads, optimizing resource usage and minimizing costs.

Scenario 2: CI/CD Pipeline

By integrating AKS with Azure DevOps or GitHub Actions, developers can automate the deployment of applications to their Kubernetes cluster. Using triggers based on code changes, teams can ensure that their applications are always up to date without manual intervention.

Scenario 3: Hybrid Applications

Organizations can leverage AKS in hybrid cloud scenarios, where part of their application runs on-premises while the other part runs in Azure. This allows for flexibility and scalability while maintaining control over sensitive data.

Best Practices

  1. Use Managed Identities: Leverage Azure Managed Identities for seamless authentication to Azure services from your AKS cluster.
  2. Implement RBAC: Use Role-Based Access Control (RBAC) to manage access to your Kubernetes resources.
  3. Monitor Performance: Utilize Azure Monitor to keep an eye on your AKS cluster's performance and health.
  4. Define Resource Limits: Specify CPU and memory limits for your containers to avoid resource contention.
  5. Regularly Update AKS: Ensure your AKS version is up to date to benefit from new features, security updates, and performance improvements.

Common Errors

  1. Error: "Insufficient quota"

    • Cause: You may have exceeded the resource quota for your Azure subscription.
    • Solution: Request a quota increase from the Azure portal.
  2. Error: "Failed to create the AKS cluster"

    • Cause: Misconfiguration in the command parameters.
    • Solution: Double-check the parameters and ensure all required values are correct.
  3. Error: "SSH key not found"

    • Cause: The SSH key was not generated or is missing.
    • Solution: Use the --generate-ssh-keys parameter or specify the path to your existing SSH keys.
  4. Error: "Unauthorized"

    • Cause: Insufficient permissions to create or manage the AKS resources.
    • Solution: Ensure you have the correct roles assigned in your Azure subscription.

Related Commands

Command Description
az aks get-credentials Get access credentials for the AKS cluster.
az aks nodepool add Add a new node pool to an existing AKS cluster.
az aks scale Scale the number of nodes in an AKS cluster.
az aks delete Delete an AKS cluster and its associated resources.

Automation Script

Here's a complete bash script to automate the creation of an AKS cluster:

#!/bin/bash

# Variables
RESOURCE_GROUP="myResourceGroup"
CLUSTER_NAME="myAKSCluster"
NODE_COUNT=3
NODE_VM_SIZE="Standard_DS2_v2"
ACR_NAME="myContainerRegistry"

# Create resource group
az group create --name $RESOURCE_GROUP --location eastus

# Create AKS cluster
az aks create \
  --resource-group $RESOURCE_GROUP \
  --name $CLUSTER_NAME \
  --node-count $NODE_COUNT \
  --node-vm-size $NODE_VM_SIZE \
  --generate-ssh-keys \
  --attach-acr $ACR_NAME

# Get AKS credentials
az aks get-credentials --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME

# Output cluster nodes
kubectl get nodes

Conclusion

In this tutorial, you learned how to create and manage Kubernetes clusters using Azure Kubernetes Service (AKS) with the az aks create command. You explored practical examples, real-world use cases, and best practices for using AKS effectively. By utilizing AKS, you can streamline the deployment and management of your containerized applications in the cloud.

Next Steps

  • Experiment with deploying applications to your AKS cluster.
  • Explore advanced features like monitoring, scaling, and security configurations.
  • Join the Azure Kubernetes Service community for support and updates.

References