/ TERRAFORM, AZURE

Deploying an Azure Kubernetes Service (AKS) Cluster with Terraform

Deploying an Azure Kubernetes Service (AKS) Cluster with Terraform

Azure Kubernetes Service (AKS) is a fully managed Kubernetes service offered by Microsoft Azure. It simplifies deploying and managing containerized applications using Kubernetes. This article will demonstrate creating an AKS cluster using Terraform, a widespread Infrastructure as Code (IaC) tool.

Prerequisites

  1. Install Terraform on your local machine.
  2. Install Azure CLI and sign in to your Azure account with az login.
  3. Make sure you have an active Azure subscription.

Table of Contents:


Create Terraform Configuration Files

First, create a new directory for your Terraform configuration files:

$ mkdir aks-terraform
$ cd aks-terraform

Next, create the main Terraform configuration file main.tf and the variables file variables.tf.

The main.tf file:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_kubernetes_cluster" "example" {
  name                = var.cluster_name
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  dns_prefix          = var.dns_prefix

  default_node_pool {
    name       = "default"
    node_count = var.node_count
    vm_size    = var.vm_size
  }

  identity {
    type = "SystemAssigned"
  }

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

output "kubeconfig" {
  value = azurerm_kubernetes_cluster.example.kube_config_raw
  sensitive = true
}

The variables.tf file:

variable "resource_group_name" {
  description = "Name of the resource group"
  default     = "my-aks-rg"
}

variable "location" {
  description = "Azure region for the resources"
  default     = "East US"
}

variable "cluster_name" {
  description = "Name of the AKS cluster"
  default     = "my-aks-cluster"
}

variable "dns_prefix" {
  description = "DNS prefix for the AKS cluster"
  default     = "myaks"
}

variable "node_count" {
  description = "Number of nodes in the AKS cluster"
  default     = 2
}

variable "vm_size" {
  description = "Size of the VMs in the AKS cluster"
  default     = "Standard_DS2_v2"
}

Initialize Terraform

Run the terraform init command to initialize your Terraform project and download the necessary provider plugins:

$ terraform init

Apply the Terraform Configuration

Run the terraform apply to create the resources defined in your configuration:

$ terraform apply

Review the changes and type “yes” when prompted to apply them. This step might take a few minutes to complete. Once done, Terraform will output the kubeconfig information.

Connect to the AKS Cluster

Save the kubeconfig output from the previous step to a file, and set the KUBECONFIG environment variable to use it:

$ echo "$(terraform output kubeconfig)" > kubeconfig.yaml
$ export KUBECONFIG=kubeconfig.yaml

Now, you can use kubectl to interact with your AKS cluster:

$ kubectl get nodes

This command will show the nodes in your AKS cluster.

Clean Up Resources

When you no longer need the AKS cluster, you can destroy the resources using Terraform:

$ terraform destroy

Type “yes” when prompted to confirm that you want to destroy the resources.

Conclusion

This tutorial taught you how to create an Azure Kubernetes Service (AKS) cluster using Terraform. By leveraging Infrastructure as Code, you can maintain consistent environments, collaborate more effectively, and ensure repeatability across deployments. In addition, you can further enhance your Terraform configuration by adding more resources and customizing your AKS cluster to suit your application requirements.

Dive Deeper: Recommended Reads

Expand your knowledge of Infrastructure as Code and Terraform with our insightful collection of articles! Dive into a range of topics that will help you master the art of managing infrastructure:

Embrace the power of Terraform and Infrastructure as Code with this comprehensive collection of articles, and enhance your skills in deploying, managing, and maintaining your infrastructure.

faizan

Faizan Bashir

Principal Engineer | Architecting and building distributed applications in the Cloud | Adventurer

Read More