Quickstart: Create an Azure Kubernetes Service (AKS) cluster by using Terraform
Azure Kubernetes Service (AKS) is a managed Kubernetes service that lets you quickly deploy and manage clusters. In this quickstart, you:
- Deploy an AKS cluster using Terraform. The sample code is fully encapsulated such that it automatically creates a service principal and SSH key pair (using the AzAPI provider).
- Run a sample multi-container application with a web front-end and a Redis instance in the cluster.
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.
In this article, you learn how to:
- Create a random value for the Azure resource group name using random_pet.
- Create an Azure resource group using azurerm_resource_group.
- Access the configuration of the AzureRM provider to get the Azure Object ID using azurerm_client_config.
- Create a Kubernetes cluster using azurerm_kubernetes_cluster.
- Create an AzAPI resource azapi_resource.
- Create an AzAPI resource to generate an SSH key pair using azapi_resource_action.
Prerequisites
Kubernetes command-line tool (kubectl): Download kubectl.
Login to your Azure Account
Terraform and Azure authentication scenarios
Terraform only supports authenticating to Azure via the Azure CLI. Authenticating using Azure PowerShell isn't supported. Therefore, while you can use the Azure PowerShell module when doing your Terraform work, you first need to authenticate to Azure using the Azure CLI.
This article explains how to authenticate Terraform to Azure for the following scenarios. For more information about options to authenticate Terraform to Azure, see Authenticating using the Azure CLI.
- Authenticate via a Microsoft account using Cloud Shell (with Bash or PowerShell)
- Authenticate via a Microsoft account using Windows (with Bash or PowerShell)
- Authenticate via a service principal:
- If you don't have a service principal, create a service principal.
- Authenticate to Azure using environment variables or authenticate to Azure using the Terraform provider block
Authenticate to Azure via a Microsoft account
A Microsoft account is a username (associated with an email and its credentials) that is used to sign in to Microsoft services - such as Azure. A Microsoft account can be associated with one or more Azure subscriptions, with one of those subscriptions being the default.
The following steps show you how:
- Sign in to Azure interactively using a Microsoft account
- List the account's associated Azure subscriptions (including the default)
- Set the current subscription.
Open a command line that has access to the Azure CLI.
Run az login without any parameters and follow the instructions to sign in to Azure.
az login
Key points:
- Upon successful sign in,
az login
displays a list of the Azure subscriptions associated with the logged-in Microsoft account, including the default subscription.
- Upon successful sign in,
To confirm the current Azure subscription, run az account show.
az account show
To view all the Azure subscription names and IDs for a specific Microsoft account, run az account list.
az account list --query "[?user.name=='<microsoft_account_email>'].{Name:name, ID:id, Default:isDefault}" --output Table
Key points:
- Replace the
<microsoft_account_email>
placeholder with the Microsoft account email address whose Azure subscriptions you want to list. - With a Live account - such as a Hotmail or Outlook - you might need to specify the fully qualified email address. For example, if your email address is
admin@hotmail.com
, you might need to replace the placeholder withlive.com#admin@hotmail.com
.
- Replace the
To use a specific Azure subscription, run az account set.
az account set --subscription "<subscription_id_or_subscription_name>"
Key points:
- Replace the
<subscription_id_or_subscription_name>
placeholder with the ID or name of the subscription you want to use. - Calling
az account set
doesn't display the results of switching to the specified Azure subscription. However, you can useaz account show
to confirm that the current Azure subscription has changed. - If you run the
az account list
command from the previous step, you see that the default Azure subscription has changed to the subscription you specified withaz account set
.
- Replace the
Create a service principal
Automated tools that deploy or use Azure services - such as Terraform - should always have restricted permissions. Instead of having applications sign in as a fully privileged user, Azure offers service principals.
The most common pattern is to interactively sign in to Azure, create a service principal, test the service principal, and then use that service principal for future authentication (either interactively or from your scripts).
To create a service principal, sign in to Azure. After authenticating to Azure via a Microsoft account, return here.
If you're creating a service principal from Git Bash, set the
MSYS_NO_PATHCONV
environment variable. (This step isn't necessary if you're using Cloud Shell.)export MSYS_NO_PATHCONV=1
Key points:
- You can set the
MSYS_NO_PATHCONV
environment variable globally (for all terminal sessions) or locally (for just the current session). As creating a service principal isn't something you do often, the sample sets the value for the current session. To set this environment variable globally, add the setting to the~/.bashrc
file.
- You can set the
To create a service principal, run az ad sp create-for-rbac.
az ad sp create-for-rbac --name <service_principal_name> --role Contributor --scopes /subscriptions/<subscription_id>
Key points:
- You can replace the
<service-principal-name>
with a custom name for your environment or omit the parameter entirely. If you omit the parameter, the service principal name is generated based on the current date and time. - Upon successful completion,
az ad sp create-for-rbac
displays several values. TheappId
,password
, andtenant
values are used in the next step. - The password can't be retrieved if lost. As such, you should store your password in a safe place. If you forget your password, you can reset the service principal credentials.
- For this article, a service principal with a Contributor role is being used. For more information about Role-Based Access Control (RBAC) roles, see RBAC: Built-in roles.
- The output from creating the service principal includes sensitive credentials. Be sure that you don't include these credentials in your code or check the credentials into your source control.
- For more information about options when creating a service principal with the Azure CLI, see the article Create an Azure service principal with the Azure CLI.
- You can replace the
Specify service principal credentials in environment variables
Once you create a service principal, you can specify its credentials to Terraform via environment variables.
Edit the
~/.bashrc
file by adding the following environment variables.export ARM_SUBSCRIPTION_ID="<azure_subscription_id>" export ARM_TENANT_ID="<azure_subscription_tenant_id>" export ARM_CLIENT_ID="<service_principal_appid>" export ARM_CLIENT_SECRET="<service_principal_password>"
To execute the
~/.bashrc
script, runsource ~/.bashrc
(or its abbreviated equivalent. ~/.bashrc
). You can also exit and reopen Cloud Shell for the script to run automatically.. ~/.bashrc
Once the environment variables have been set, you can verify their values as follows:
printenv | grep ^ARM*
Key points:
- As with any environment variable, to access an Azure subscription value from within a Terraform script, use the following syntax:
${env.<environment_variable>}
. For example, to access theARM_SUBSCRIPTION_ID
value, specify${env.ARM_SUBSCRIPTION_ID}
. - Creating and applying Terraform execution plans makes changes on the Azure subscription associated with the service principal. This fact can sometimes be confusing if you're logged into one Azure subscription and the environment variables point to a second Azure subscription. Let's look at the following example to explain. Let's say you have two Azure subscriptions: SubA and SubB. If the current Azure subscription is SubA (determined via
az account show
) while the environment variables point to SubB, any changes made by Terraform are on SubB. Therefore, you would need to log in to your SubB subscription to run Azure CLI commands or Azure PowerShell commands to view your changes.
Specify service principal credentials in a Terraform provider block
The Azure provider block defines syntax that allows you to specify your Azure subscription's authentication information.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = "<azure_subscription_id>"
tenant_id = "<azure_subscription_tenant_id>"
client_id = "<service_principal_appid>"
client_secret = "<service_principal_password>"
}
# Your code goes here
Caution
The ability to specify your Azure subscription credentials in a Terraform configuration file can be convenient - especially when testing. However, it isn't advisable to store credentials in a clear-text file that can be viewed by non-trusted individuals.
Implement the Terraform code
Note
The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform.
See more articles and sample code showing how to use Terraform to manage Azure resources
Create a directory in which to test the sample Terraform code and make it the current directory.
Create a file named
providers.tf
and insert the following code:terraform { required_version = ">=1.0" required_providers { azapi = { source = "azure/azapi" version = "~>1.5" } azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } time = { source = "hashicorp/time" version = "0.9.1" } } } provider "azurerm" { features {} }
Create a file named
ssh.tf
and insert the following code:resource "random_pet" "ssh_key_name" { prefix = "ssh" separator = "" } resource "azapi_resource_action" "ssh_public_key_gen" { type = "Microsoft.Compute/sshPublicKeys@2022-11-01" resource_id = azapi_resource.ssh_public_key.id action = "generateKeyPair" method = "POST" response_export_values = ["publicKey", "privateKey"] } resource "azapi_resource" "ssh_public_key" { type = "Microsoft.Compute/sshPublicKeys@2022-11-01" name = random_pet.ssh_key_name.id location = azurerm_resource_group.rg.location parent_id = azurerm_resource_group.rg.id } output "key_data" { value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey }
Create a file named
main.tf
and insert the following code:# Generate random resource group name resource "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_pet" "azurerm_kubernetes_cluster_name" { prefix = "cluster" } resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { prefix = "dns" } resource "azurerm_kubernetes_cluster" "k8s" { location = azurerm_resource_group.rg.location name = random_pet.azurerm_kubernetes_cluster_name.id resource_group_name = azurerm_resource_group.rg.name dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id identity { type = "SystemAssigned" } default_node_pool { name = "agentpool" vm_size = "Standard_D2_v2" node_count = var.node_count } linux_profile { admin_username = var.username ssh_key { key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey } } network_profile { network_plugin = "kubenet" load_balancer_sku = "standard" } }
Create a file named
variables.tf
and insert the following code:variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "resource_group_name_prefix" { type = string 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 "node_count" { type = number description = "The initial quantity of nodes for the node pool." default = 3 } variable "msi_id" { type = string description = "The Managed Service Identity ID. Set this value if you're running this example using Managed Identity as the authentication method." default = null } variable "username" { type = string description = "The admin username for the new cluster." default = "azureadmin" }
Create a file named
outputs.tf
and insert the following code:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "kubernetes_cluster_name" { value = azurerm_kubernetes_cluster.k8s.name } 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 }
Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
terraform init -upgrade
Key points:
- The
-upgrade
parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
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 example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
Verify the results
Get the Azure resource group name.
resource_group_name=$(terraform output -raw resource_group_name)
Run az aks list to display the name of the new Kubernetes cluster.
az aks list \ --resource-group $resource_group_name \ --query "[].{\"K8s cluster name\":name}" \ --output table
Get the Kubernetes configuration from the Terraform state and store it in a file that kubectl can read.
echo "$(terraform output kube_config)" > ./azurek8s
Verify the previous command didn't add an ASCII EOT character.
cat ./azurek8s
Key points:
- If you see
<< EOT
at the beginning andEOT
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
- If you see
Set an environment variable so that kubectl picks up the correct config.
export KUBECONFIG=./azurek8s
Verify the health of the cluster.
kubectl get nodes
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.
Deploy the application
A Kubernetes manifest file defines a cluster's desired state, such as which container images to run.
In this quickstart, you use a manifest to create all the objects needed to run the Azure Vote application. This manifest includes two Kubernetes deployments:
- The sample Azure Vote Python applications.
- A Redis instance.
Two Kubernetes Services are created:
- An internal service for the Redis instance.
- An external service to access the Azure Vote application from the internet.
Create a file named
azure-vote.yaml
and insert the following code: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: nodeSelector: "kubernetes.io/os": linux containers: - name: azure-vote-back image: mcr.microsoft.com/oss/bitnami/redis:6.0.8 env: - name: ALLOW_EMPTY_PASSWORD value: "yes" 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: nodeSelector: "kubernetes.io/os": linux containers: - name: azure-vote-front image: mcr.microsoft.com/azuredocs/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
Key points:
- For more information about YAML manifest files, see Deployments and YAML manifests.
Run kubectl apply to deploy the application.
kubectl apply -f azure-vote.yaml
Test the application
When the application runs, a Kubernetes service exposes the application front end to the internet. This process can take a few minutes to complete. Run kubectl get service with the
--watch
argument to monitor progress.kubectl get service azure-vote-front --watch
The EXTERNAL-IP output for the
azure-vote-front
service initially shows as pending. Once the EXTERNAL-IP address displays an IP address, useCTRL-C
to stop thekubectl
watch process.To see the Azure Vote app in action, open a web browser to the external IP address of your service.
Clean up resources
Delete AKS resources
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroy
flag.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.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan
Delete service principal
Get the service principal ID.
sp=$(terraform output -raw sp)
Run az ad sp delete to delete the service principal.
az ad sp delete --id $sp
Troubleshoot Terraform on Azure
Troubleshoot common problems when using Terraform on Azure
Next steps
Tilbakemeldinger
Send inn og vis tilbakemelding for