terraform-azurerm-aks

repository·main·Indexed 18 days ago

https://github.com/azure/terraform-azurerm-aks

A Terraform module for deploying Azure Kubernetes Service (AKS) clusters with integrated Azure Log Analytics monitoring. It supports advanced configurations including LocalDNS overrides, Kubelet settings, ACI Connector (Virtual Nodes), and Linux OS customizations. Note: This module is deprecated and users are advised to migrate to the AVM module (Azure/avm-res-containerservice-managedcluster/azurerm).

Tokens
24.5K
Snippets
36
Records
122
Agent score
63%

What's inside terraform-azurerm-aks

  1. How LocalDNS configuration works in AKS

    main

    LocalDNS in AKS allows you to customize DNS resolution behavior for pods based on their dnsPolicy. The module supports two primary types of overrides:

    1. VNet DNS Overrides: Targets pods using dnsPolicy: default. These pods use VNet DNS.
    2. Kube DNS Overrides: Targets pods using dnsPolicy: ClusterFirst. These pods use Kubernetes CoreDNS.

    You can enforce LocalDNS usage by setting the mode to Required.

  2. Manage kubernetes_version changes in Terraform

    main
    As of version 7.0.0, the module is configured to ignore changes to kubernetes_version made outside of Terraform. This prevents Terraform from attempting to revert version upgrades performed via the Azure portal or CLI, ensuring the state remains consistent with manual or automated external upgrades.
  3. Handle `kubernetes_version` lifecycle and `azapi` provider requirement

    main

    In v7.x, the module manages kubernetes_version upgrades using null_resource.kubernetes_version_keeper and azapi_update_resource.aks_cluster_post_create.

    Key behaviors:

    • The azurerm_kubernetes_cluster.main resource will now ignore kubernetes_version changes made by Azure (e.g., automatic patch upgrades) to prevent unnecessary Terraform churn.
    • To trigger a manual version upgrade, change the value of var.kubernetes_version. This will trigger a re-creation of the keeper and update resources.
    • Requirement: You must have the azapi provider configured in your Terraform environment.
  4. Manage node pool name changes with `create_before_destroy`

    main

    To prevent downtime during node pool upgrades, azurerm_kubernetes_cluster_node_pool.node_pool now uses create_before_destroy = true.

    Important Implications:

    • Name Suffix: A 4-character random suffix is appended to the node pool name (e.g., nodepool1 becomes nodepool1xxxx). This suffix is deterministic based on the pool's configuration.
    • Naming Strategy: You may need to shorten your existing node pool names to accommodate this suffix.
    • Tracking: The module uses null_resource.pool_name_keeper to track names in case they are changed.
  5. Manage Network Contributor Role Assignments

    main

    Due to a change in how for_each is implemented (moving from a set of strings to a map of objects), the azurerm_role_assignment.network_contributor resource will be re-created.

    Warning: This will cause the role assignment to be removed and re-added, which may result in a brief period where the role assignment is missing.

    Usage Requirements

    If var.create_role_assignment_network_contributor is set to true:

    • You must set a different subnet for every node pool (including the default pool).

    If you cannot provide different subnets for each node pool:

    • You must set var.create_role_assignment_network_contributor to false and manage the role assignments manually.
  6. Set up Azure credentials for testing

    main

    To run tests or pre-commit checks using the provided Docker image, you must set up your Azure Service Principal credentials in your environment variables.

    Linux/macOS:

    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>"

    Windows PowerShell:

    $env:ARM_SUBSCRIPTION_ID="<azure_subscription_id>"
    $env:ARM_TENANT_ID="<azure_subscription_tenant_id>"
    $env:ARM_CLIENT_ID="<service_principal_appid>"
    $env:ARM_CLIENT_SECRET="<service_principal_password>"
  7. Run pre-commit, pr-check, and e2e-tests via Docker

    main

    The repository provides a Docker image mcr.microsoft.com/azterraform:latest to run various validation tasks.

    Pre-commit

    Runs formatting (terraform fmt, gofmt, etc.) and documentation checks. Linux/macOS:

    docker run --rm -v $(pwd):/src -w /src mcr.microsoft.com/azterraform:latest make pre-commit

    Windows PowerShell:

    docker run --rm -v ${pwd}:/src -w /src mcr.microsoft.com/azterraform:latest make pre-commit

    PR-check

    Checks if the code meets pipeline requirements. Run this before committing. Linux/macOS:

    docker run --rm -v $(pwd):/src -w /src mcr.microsoft.com/azterraform:latest make pr-check

    Windows PowerShell:

    docker run --rm -v ${pwd}:/src -w /src mcr.microsoft.com/azterraform:latest make pr-check

    E2E-test

    Runs end-to-end tests. Requires Azure credentials to be passed as environment variables. Linux/macOS:

    docker run --rm -v $(pwd):/src -w /src -e ARM_SUBSCRIPTION_ID -e ARM_TENANT_ID -e ARM_CLIENT_ID -e ARM_CLIENT_SECRET mcr.microsoft.com/azterraform:latest make e2e-test

    Windows PowerShell:

    docker run --rm -v ${pwd}:/src -w /src -e ARM_SUBSCRIPTION_ID -e ARM_TENANT_ID -e ARM_CLIENT_ID -e ARM_CLIENT_SECRET mcr.microsoft.com/azterraform:latest make e2e-test

    Note on Key Vault Firewall: If you need to bypass Key Vault firewall rules during E2E tests, set the TF_VAR_key_vault_firewall_bypass_ip_cidr environment variable.

  8. Deploy the LocalDNS configuration example

    main

    To deploy the LocalDNS configuration example, follow these steps:

    1. Set Azure credentials in your environment:

      export ARM_CLIENT_ID="your-client-id"
      export ARM_CLIENT_SECRET="your-client-secret"
      export ARM_SUBSCRIPTION_ID="your-subscription-id"
      export ARM_TENANT_ID="your-tenant-id"
    2. Initialize and apply Terraform:

      terraform init
      terraform plan
      terraform apply
    3. Connect to the AKS cluster using the outputs from the Terraform deployment:

      az aks get-credentials --resource-group $(terraform output -raw resource_group_name) --name $(terraform output -raw aks_cluster_name)
    export ARM_CLIENT_ID="your-client-id"
    export ARM_CLIENT_SECRET="your-client-secret"
    export ARM_SUBSCRIPTION_ID="your-subscription-id"
    export ARM_TENANT_ID="your-tenant-id"
    
    terraform init
    terraform plan
    terraform apply
    
    az aks get-credentials --resource-group $(terraform output -raw resource_group_name) --name $(terraform output -raw aks_cluster_name)