Create an AKS Cluster in Azure with Terraform

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

In this lab, using the Azure portal, you will configure the Cloud Shell and download and run the lab setup script. Next, you will import the resource group. Then, you will add your AKS, variable, and outputs to the configuration. Lastly, you will deploy your Kubernetes cluster resources and verify that the cluster is up and healthy.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up Cloud Shell and the Lab Environment

In the Portal

  1. Click the Cloud Shell icon.
  2. Select Bash at the prompt.
  3. Click Show Advanced Settings.
  4. Set the Cloud Shell region to the same location as the resource group.
  5. Select the existing resource group, and select Use Existing for the Storage Account.
  6. In the File share section, choose Create new and enter terraform.
  7. Click Create Storage.
  8. Download the lab_7_setup.sh script at https://raw.githubusercontent.com/ACloudGuru/advanced-terraform-with-azure/main/lab_aks_cluster/lab_7_setup.sh.
  9. Add execute permissions to the script.
  10. Run the lab_7_setup.sh script.
Import the Resource Group

In the Cloud Shell

  1. In the Cloud Shell, review the providers.tf.
  2. Make note of the resource group and label and then close the file.
  3. Run the az group list command to get the subscription ID.
  4. Import your resource group into Terraform using the resource name, label, and subscription ID.
  5. After the import, add the name and location of your resource group to the providers.tf file, so it looks like the code below:
    resource "azurerm_resource_group" "k8s" {
        name     = "<RESOURCE_GROUP>"
        location = "<LOCATION>"
    }   
  6. Save the file.
  7. Create an SSH key pair.
Add the AKS Config, Variables, and Outputs to the Configuration

In the Cloud Shell

  1. Create the aks.tf file.
  2. Define the Kubernetes cluster using the azurerm_kubernetes_cluster resource with a label of k8s. Create the configuration and use variables for the following fields:
    1) cluster name
    2) dns prefix
    3) ssh key pair
    4) node count
    5) client id
    6) client secret
    7) resource group location
  3. Save the changes.
  4. Create the variables.tf file.
  5. Define the following input variables:
    1) cluster name
    2) dns prefix
    3) ssh key pair
    4) node count
    5) client id
    6) client secret
    7) resource group location
  6. Save the changes.
  7. Create a file called output.tf.
  8. Define the following output variables:
    1) resource group name
    2) client key
    3) client certificate
    4) client ca certificate
    5) cluster username
    6) cluster password
    7) kube config
    8) host
  9. Save the changes.
Deploy and Verify the Kubernetes Cluster Is Running
  1. Apply your configuration.
  2. Copy your kube_config to a file called azurek8s.
  3. Export your kube_config to an environment variable.
  4. Check the health of your cluster and make sure it is in a status of READY to finish the lab.

Additional Resources

You are being asked to stand up a Kubernetes cluster in Azure so the developers for a digital comics site can test the applications and microservices that they created for a future production move off of the old monolithic server cluster they are currently running on. You decide AKS is the best way to move forward, so you will now define your Kubernetes cluster configuration and output the connection information once the cluster has been deployed with Terraform in Azure. Once deployed, you will need to confirm your cluster is up and running.

To get started, log in to the Azure portal using the credentials provided. Be sure to use an incognito or private browser window to ensure you’re using the lab account rather than your own.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?