Azure  

Automating AKS Cluster Deployment with Terraform

Deploying and managing Kubernetes clusters on Azure (AKS) can be quite difficult. So, Infrastructure as Code (IaC) tools like Terraform can let you automate the entire deployment, which will make it even more efficient, repeatable, and less error-prone. This post will show you how to get the AKS Database created by Terraform instead.

Prerequisites

Before you begin, make sure you have the following.

  • Azure Account: You'll need an active Azure subscription. If you don't have one, you can sign up for a free trial.
  • Azure CLI: The Azure Command-Line Interface is required to authenticate with Azure. Install it following the instructions here.
  • Terraform: Install Terraform on your local machine. You can download it from the official website here.
  • Text Editor: A good text editor (like VS Code) will make working with Terraform files much easier. The VSCode Terraform extension is highly recommended.

Setting up the Terraform Configuration

  • Create a Project Directory: Create a new directory for your Terraform project.
    mkdir aks-terraform
    cd aks-terraform
    
  • Create the Terraform File: Inside the directory, create a file named main.tf. This file will contain the configuration for your AKS cluster.
  • Configure the Azure Provider: First, you need to configure the Azure provider. This tells Terraform how to authenticate with Azure.
    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 3.0" # Or the latest version
        }
      }
    }
    
    provider "azurerm" {
      features {}
      
      # The provider block is used to configure the Azure provider.
      # The 'features' block is required for Terraform 3.0 and later.
      # Authentication is handled outside the Terraform configuration,
      # typically using the Azure CLI (az login).
    }
    
  • Create a Resource Group: A resource group is a logical container for your Azure resources.
        resource "azurerm_resource_group" "aks_rg" {
          name     = "aks-resource-group" # Replace with your desired resource group name
          location = "East US"             # Replace with your desired Azure region
        }
        
  • Create the AKS Cluster: Now, let's define the AKS cluster. This is the core of the configuration.
        resource "azurerm_kubernetes_cluster" "aks_cluster" {
          name                = "my-aks-cluster" # Replace with your desired cluster name
          location            = azurerm_resource_group.aks_rg.location
          resource_group_name = azurerm_resource_group.aks_rg.name
          dns_prefix          = "myakscluster" # Replace with your desired DNS prefix
    
          default_node_pool {
            name           = "default"
            node_count     = 3             # Number of nodes in the default node pool
            vm_size        = "Standard_DS2_v2" # Size of the virtual machines
            os_disk_size_gb = 30
          }
    
          identity {
            type = "SystemAssigned"
          }
    
          tags = {
            Environment = "Production"
          }
        }
        
  • Create the Kubeconfig Output: To connect to your AKS cluster with kubectl, you'll need the Kubeconfig file. Terraform can output this configuration.
        output "kube_config" {
          value = azurerm_kubernetes_cluster.aks_cluster.kube_config_raw
          sensitive = true #  Mark the output as sensitive
        } 

Here's the complete main.tf file.

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0" # Or the latest version
    }
  }
}

provider "azurerm" {
  features {}
  # The provider block is used to configure the Azure provider.
  # The 'features' block is required for Terraform 3.0 and later.
  # Authentication is handled outside the Terraform configuration,
  # typically using the Azure CLI (az login).
}

resource "azurerm_resource_group" "aks_rg" {
  name     = "aks-resource-group" # Replace with your desired resource group name
  location = "East US"             # Replace with your desired Azure region
}

resource "azurerm_kubernetes_cluster" "aks_cluster" {
  name                = "my-aks-cluster" # Replace with your desired cluster name
  location            = azurerm_resource_group.aks_rg.location
  resource_group_name = azurerm_resource_group.aks_rg.name
  dns_prefix          = "myakscluster" # Replace with your desired DNS prefix

  default_node_pool {
    name           = "default"
    node_count     = 3             # Number of nodes in the default node pool
    vm_size        = "Standard_DS2_v2" # Size of the virtual machines
    os_disk_size_gb = 30
  }

  identity {
    type = "SystemAssigned"
  }

  tags = {
    Environment = "Production"
  }
}

output "kube_config" {
  value     = azurerm_kubernetes_cluster.aks_cluster.kube_config_raw
  sensitive = true #  Mark the output as sensitive
}

Deploying the AKS Cluster

  • Authenticate with Azure: Before you can deploy, you need to authenticate with Azure. Open a terminal and use the Azure CLI.
    az login
  • Initialize Terraform: In your aks-terraform directory, initialize the Terraform project. This downloads the necessary provider plugins.
    terraform init
  • Plan the Deployment: The Terraform plan command shows you what Terraform will do before it makes any changes.
    terraform plan
  • Apply the Configuration: The terraform apply command creates the resources defined in your main.tf file.
    terraform apply -y
  • Get the Kubeconfig: Once the deployment is complete, Terraform will output the Kubeconfig data (if you've included the output block).
    terraform output kube_config > kubeconfig.yaml
  • Configure kubectl: Set the KUBECONFIG environment variable so that kubectl can find your kubeconfig file.
    export KUBECONFIG=./kubeconfig.yaml
  • Verify Connectivity: Now you can use Kubectl to connect to your AKS cluster.
    kubectl get nodes

Conclusion

Terraform helps to automate the creation and deployment of AKS very easily. Some of the benefits from it are an increase in efficiency, improved consistency, reduced errors and automated infrastructure. You can also modify the networking, and scaling of pods as per your requirements.

Hope you learned something new today, feel free to add your feedback in the comments.