Edit

Share via


Quickstart: Create an Azure Kubernetes Fleet Manager resource using Terraform

Get started with Azure Kubernetes Fleet Manager by using Terraform to create a Fleet resource.

Prerequisites

If you don't have an Azure account, create a free account before you begin.

Create a Fleet Manager resource

You can create a Fleet Manager resource to later group your AKS clusters as member clusters. If the Fleet Manager hub is enabled, other preview features are enabled, such as Kubernetes object propagation to member clusters. For more information, see the conceptual overview of Fleet Manager types, which provides a comparison of different Fleet Manager configurations.

Important

Once a Fleet Manager resource has been created, it's possible to upgrade a Fleet Manager resource without a hub cluster to one with a hub cluster. For Fleet Manager resources with a hub cluster, once private or public has been selected it cannot be changed.

To create a Fleet Manager resource without a hub cluster, implement the following Terraform code

Implement the Terraform code

  • Create a directory you can use to test the sample Terraform code and make it your current directory.

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

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

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    resource "azurerm_resource_group" "fleet_rg" {
      name     = random_pet.rg_name.id
      location = var.resource_group_location
    }
    
    resource "random_string" "fleet_name" {
      length  = 63
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_kubernetes_fleet_manager" "fleet" {
    
      location            = azurerm_resource_group.fleet_rg.location
      name                = coalesce(var.fleet_name, random_string.fleet_name.result)
      resource_group_name = azurerm_resource_group.fleet_rg.name
    }
    
  • 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 "fleet_name" {
      type        = string
      description = "Name of the fleet resource. If left blank, this value is randomly generated."
      default     = ""
    }
    
  • Create a file named outputs.tf and insert the following code:

    output "resource_group_name" {
      value = azurerm_resource_group.fleet_rg.name
    }
    
    output "fleet_name" {
      value = azurerm_kubernetes_fleet_manager.fleet.name
    }
    

Important

If you're using the 4.x azurerm provider, you must explicitly specify the Azure subscription ID to authenticate to Azure before running the Terraform commands.

One way to specify the Azure subscription ID without putting it in the providers block is to specify the subscription ID in an environment variable named ARM_SUBSCRIPTION_ID.

For more information, see the Azure provider reference documentation.

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.

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 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. Verify results using either Azure CLI or Azure PowerShell.

    1. Get the Azure resource group name.

      resource_group_name=$(terraform output -raw resource_group_name)
      
    2. Get the Fleet Manager name.

      batch_name=$(terraform output -raw fleet_name)
      
    3. Run az fleet show to view the Azure Kubernetes Fleet Manager.

      az fleet show --resource-group $resource_group_name --name $fleet_name
      

Clean up resources

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

  1. 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.
  2. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan
    

Next steps