Quickstart: Create a Kubernetes cluster with Azure Kubernetes Service (AKS) using Terraform (2023)

  • Article
  • 7 minutes to read

Article tested with the following Terraform and Terraform provider versions:

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

Azure Kubernetes Service (AKS) manages your hosted Kubernetes environment. AKS allows you to deploy and manage containerized applications without container orchestration expertise. AKS also enables you to do many common maintenance operations without taking your app offline. These operations include provisioning, upgrading, and scaling resources on demand.

In this article, you learn how to:

  • Use HCL (HashiCorp Language) to define a Kubernetes cluster
  • Use Terraform and AKS to create a Kubernetes cluster
  • Use the kubectl tool to test the availability of a Kubernetes cluster

Note

The example code in this article is located in the Microsoft Terraform GitHub repo.

(Video) Terraform with Azure Kubernetes Service

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • Configure Terraform: If you haven't already done so, configure Terraform using one of the following options:

    • Configure Terraform in Azure Cloud Shell with Bash
    • Configure Terraform in Azure Cloud Shell with PowerShell
    • Configure Terraform in Windows with Bash
    • Configure Terraform in Windows with PowerShell
  • Azure service principal: If you don't have a service principal, create a service principal. Make note of the appId, display_name, password, and tenant.

  • SSH key pair: Use one of the following articles:

    • Portal
    • Windows
    • Linux/MacOS
  • Kubernetes command-line tool (kubectl): Download kubectl.

Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named providers.tf and insert the following code:

    terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } }}provider "azurerm" { features {}}
  3. Create a file named main.tf and insert the following code:

    # Generate random resource group nameresource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix}resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = random_pet.rg_name.id}resource "random_id" "log_analytics_workspace_name_suffix" { byte_length = 8}resource "azurerm_log_analytics_workspace" "test" { location = var.log_analytics_workspace_location # The WorkSpace name has to be unique across the whole of azure; # not just the current subscription/tenant. name = "${var.log_analytics_workspace_name}-${random_id.log_analytics_workspace_name_suffix.dec}" resource_group_name = azurerm_resource_group.rg.name sku = var.log_analytics_workspace_sku}resource "azurerm_log_analytics_solution" "test" { location = azurerm_log_analytics_workspace.test.location resource_group_name = azurerm_resource_group.rg.name solution_name = "ContainerInsights" workspace_name = azurerm_log_analytics_workspace.test.name workspace_resource_id = azurerm_log_analytics_workspace.test.id plan { product = "OMSGallery/ContainerInsights" publisher = "Microsoft" }}resource "azurerm_kubernetes_cluster" "k8s" { location = azurerm_resource_group.rg.location name = var.cluster_name resource_group_name = azurerm_resource_group.rg.name dns_prefix = var.dns_prefix tags = { Environment = "Development" } default_node_pool { name = "agentpool" vm_size = "Standard_D2_v2" node_count = var.agent_count } linux_profile { admin_username = "ubuntu" ssh_key { key_data = file(var.ssh_public_key) } } network_profile { network_plugin = "kubenet" load_balancer_sku = "standard" } service_principal { client_id = var.aks_service_principal_app_id client_secret = var.aks_service_principal_client_secret }}
  4. Create a file named variables.tf and insert the following code:

    variable "agent_count" { default = 3}# The following two variable declarations are placeholder references.# Set the values for these variable in terraform.tfvarsvariable "aks_service_principal_app_id" { default = ""}variable "aks_service_principal_client_secret" { default = ""}variable "cluster_name" { default = "k8stest"}variable "dns_prefix" { default = "k8stest"}# Refer to https://azure.microsoft.com/global-infrastructure/services/?products=monitor for available Log Analytics regions.variable "log_analytics_workspace_location" { default = "eastus"}variable "log_analytics_workspace_name" { default = "testLogAnalyticsWorkspaceName"}# Refer to https://azure.microsoft.com/pricing/details/monitor/ for Log Analytics pricingvariable "log_analytics_workspace_sku" { default = "PerGB2018"}variable "resource_group_location" { default = "eastus" description = "Location of the resource group."}variable "resource_group_name_prefix" { default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."}variable "ssh_public_key" { default = "~/.ssh/id_rsa.pub"}
  5. Create a file named outputs.tf and insert the following code:

    (Video) How to create AKS Cluster | Setup Azure Kubernetes Cluster(AKS) in Azure Cloud | Kubernetes Cluster

    output "client_certificate" { value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_certificate sensitive = true}output "client_key" { value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_key sensitive = true}output "cluster_ca_certificate" { value = azurerm_kubernetes_cluster.k8s.kube_config[0].cluster_ca_certificate sensitive = true}output "cluster_password" { value = azurerm_kubernetes_cluster.k8s.kube_config[0].password sensitive = true}output "cluster_username" { value = azurerm_kubernetes_cluster.k8s.kube_config[0].username sensitive = true}output "host" { value = azurerm_kubernetes_cluster.k8s.kube_config[0].host sensitive = true}output "kube_config" { value = azurerm_kubernetes_cluster.k8s.kube_config_raw sensitive = true}output "resource_group_name" { value = azurerm_resource_group.rg.name}
  6. Create a file named terraform.tfvars and insert the following code.

    aks_service_principal_app_id = "<service_principal_app_id>"aks_service_principal_client_secret = "<service_principal_password>"

Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.

terraform init

Create a Terraform execution plan

Run terraform plan to create an execution plan.

terraform plan -out main.tfplan

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  • To read more about persisting execution plans and security, see the security warning section.

Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan

Key points:

  • The terraform apply command above assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, call terraform apply without any parameters.

Verify the results

  1. Get the resource group name.

    echo "$(terraform output resource_group_name)"
  2. Browse to the Azure portal.

  3. Under Azure services, select Resource groups and locate your new resource group to see the following resources created in this demo:

    (Video) Deploy Azure Kubernetes Service(AKS) Cluster using Terraform and Azure DevOps YAML Pipeline

    • Solution: By default, the demo names this solution ContainerInsights. The portal will show the solution's workspace name in parenthesis.
    • Kubernetes service: By default, the demo names this service k8stest. (A Managed Kubernetes Cluster is also known as an AKS / Azure Kubernetes Service.)
    • Log Analytics Workspace: By default, the demo names this workspace with a prefix of TestLogAnalyticsWorkspaceName- followed by a random number.
  4. Get the Kubernetes configuration from the Terraform state and store it in a file that kubectl can read.

    echo "$(terraform output kube_config)" > ./azurek8s
  5. Verify the previous command didn't add an ASCII EOT character.

    cat ./azurek8s

    Key points:

    • If you see << EOT at the beginning and EOT at the end, remove these characters from the file. Otherwise, you could receive the following error message: error: error loading config file "./azurek8s": yaml: line 2: mapping values are not allowed in this context
  6. Set an environment variable so that kubectl picks up the correct config.

    export KUBECONFIG=./azurek8s
  7. Verify the health of the cluster.

    kubectl get nodes

    Quickstart: Create a Kubernetes cluster with Azure Kubernetes Service (AKS) using Terraform (1)

Key points:

  • When the AKS cluster was created, monitoring was enabled to capture health metrics for both the cluster nodes and pods. These health metrics are available in the Azure portal. For more information on container health monitoring, see Monitor Azure Kubernetes Service health.
  • Several key values were output when you applied the Terraform execution plan. For example, the host address, AKS cluster user name, and AKS cluster password are output.
  • To view all of the output values, run terraform output.
  • To view a specific output value, run echo "$(terraform output <output_value_name>)".

Clean up resources

Delete AKS resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    (Video) Provisioning Kubernetes clusters on AKS using HashiCorp Terraform | Azure Friday

    terraform plan -destroy -out main.destroy.tfplan

    Key points:

    • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
    • To read more about persisting execution plans and security, see the security warning section.
  2. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan

Delete service principal

Caution

Delete the service principal you used in this demo only if you're not using it for anything else.

  1. Run az ad sp list to get the object ID of the service principal.

    az ad sp list --display-name "<display_name>" --query "[].{\"Object ID\":id}" --output table
  2. Run az ad sp delete to delete the service principal.

    az ad sp delete --id <service_principal_object_id>

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps

Learn more about using Terraform in Azure

(Video) Start from scratch with Azure: Azure Kubernetes Service with Terraform

FAQs

How do you make a Kubernetes cluster in Aks? ›

Create an AKS cluster
  1. Sign in to the Azure portal.
  2. On the Azure portal menu or from the Home page, select Create a resource.
  3. Select Containers > Kubernetes Service.
  4. On the Basics page, configure the following options: ...
  5. Select Next: Node pools when complete.
  6. Keep the default Node pools options.
Nov 1, 2022

How do you deploy Kubernetes cluster with terraform? ›

Kubernetes Cloud Deployments with Terraform
  1. Prerequisites.
  2. Defining the Terraform providers.
  3. Configuring state.
  4. Configuring the providers.
  5. Creating a VPC.
  6. Create the database security group.
  7. Create the RDS instance.
  8. Deploy WordPress to Kubernetes.
Mar 28, 2022

How do you make a Kubernetes cluster step by step? ›

Following are the high-level steps involved in setting up a kubeadm-based Kubernetes cluster.
  1. Install container runtime on all nodes- We will be using cri-o.
  2. Install Kubeadm, Kubelet, and kubectl on all the nodes.
  3. Initiate Kubeadm control plane configuration on the master node.
  4. Save the node join command with the token.

How do I start a Kubernetes cluster? ›

Starting the Kubernetes cluster
  1. Start the server or virtual machine that is running the Docker registry first. This will automatically start the Docker registry. ...
  2. Start the NFS server and wait two minutes after the operating system has started. ...
  3. Start all worker nodes either simultaneously or individually.

How do I create a cluster in terraform? ›

Create the main.tf file
  1. Open your text editor and create a new directory. ...
  2. In the main.tf file, add the provider code. ...
  3. Set up the first resource for the IAM role. ...
  4. Once the role is created, attach these two policies to it: ...
  5. Once the policies are attached, create the EKS cluster. ...
  6. Set up an IAM role for the worker nodes.
May 25, 2022

What tool is used to quickly create Kubernetes clusters? ›

kOps is a popular Kubernetes operations tool. kOps is like a kubectl for clusters. It can help you create, destroy, upgrade and maintain production-grade, highly available Kubernetes cluster.

What is Azure Kubernetes service AKS cluster? ›

Azure Kubernetes Service (AKS) is a managed Kubernetes service in which the master node is managed by Azure and end-users manages worker nodes. Users can use AKS to deploy, scale, and manage Docker containers and container-based applications across a cluster of container hosts.

How do I connect AKS cluster to Azure? ›

kubectl is already installed if you use Azure Cloud Shell.
  1. Install kubectl locally using the az aks install-cli command: Azure CLI Copy. ...
  2. Configure kubectl to connect to your Kubernetes cluster using the az aks get-credentials command. ...
  3. Verify the connection to your cluster using the kubectl get command.
Feb 6, 2023

How do you make a Terraform rancher cluster? ›

To create a Rancher-provisioned cluster with Terraform, go to your Terraform configuration file and define the provider as Rancher 2. You can set up your Rancher 2 provider with a Rancher API key. Note: The API key has the same permissions and access level as the user it is associated with.

How do I create a private AKS cluster in Azure? ›

Options for connecting to the private cluster
  1. Create a VM in the same Azure Virtual Network (VNet) as the AKS cluster.
  2. Use a VM in a separate network and set up Virtual network peering. ...
  3. Use an Express Route or VPN connection.
  4. Use the AKS command invoke feature.
  5. Use a private endpoint connection.
Feb 27, 2023

How do I use Terraform to create Azure Resource Group? ›

  1. Initialize Terraform. Run terraform init to initialize the Terraform deployment. ...
  2. Create a Terraform execution plan. Run terraform plan to create an execution plan. ...
  3. Apply a Terraform execution plan. Run terraform apply to apply the execution plan to your cloud infrastructure. ...
  4. Verify the results.
Aug 24, 2022

Videos

1. How to build and deploy a containerized app to Azure Kubernetes Service (AKS) | Azure Friday
(Microsoft Azure)
2. Deploy App to Azure Kubernetes Services (AKS)
(YogiHosting)
3. Create Azure Kubernetes Cluster using Azure Portal | AKS Cluster | Azure Kubernetes Service | Demo
(A Monk in Cloud ☁️ )
4. Create a RBAC Azure Kubernetes Services (AKS) cluster with Azure Active Directory using Terraform
(Pixel Robots)
5. Terraform - Provisioning a Kubernetes Cluster with an attached Container Registry in Azure
(Patrick Koch)
6. Deploying Azure Kubernetes Service (AKS)
(Blackbox DevOps)

References

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated: 09/15/2023

Views: 6772

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.