Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
- Read the conceptual overview of Fleet Manager, which provides an explanation of fleets and member clusters referenced in this document.
- An Azure account with an active subscription. Create an account for free.
- Install and configure Terraform.
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.tfand 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.tfand 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.tfand 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.tfand 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
-upgradeparameter 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 plancommand 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
-outparameter allows you to specify an output file for the plan. Using the-outparameter 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 applycommand assumes you previously ranterraform plan -out main.tfplan. - If you specified a different filename for the
-outparameter, use that same filename in the call toterraform apply. - If you didn't use the
-outparameter, callterraform applywithout any parameters.
Verify the results
Verify results using either Azure CLI or Azure PowerShell.
Get the Azure resource group name.
resource_group_name=$(terraform output -raw resource_group_name)Get the Fleet Manager name.
batch_name=$(terraform output -raw fleet_name)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:
Run terraform plan and specify the
destroyflag.terraform plan -destroy -out main.destroy.tfplanKey points:
- The
terraform plancommand 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
-outparameter allows you to specify an output file for the plan. Using the-outparameter ensures that the plan you reviewed is exactly what is applied.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan
Next steps
Azure Kubernetes Service