本快速入門說明如何使用 Terraform 設定 Azure Kubernetes Service (AKS) 叢集的保存庫備份。
適用於 AKS 的 Azure 備份是雲端原生、企業就緒、以應用程式為中心的備份服務,可讓您快速設定 AKS 叢集的備份。
必要條件
設定 AKS 備份之前,必須確定的事項:
本快速入門假設您已有 Kubernetes 概念的基本知識。 如需詳細資訊,請參閱 [Azure Kubernetes Services (AKS) 的 Kubernetes 核心概念][kubernetes-concepts]。
您需要具有有效訂用帳戶的 Azure 帳戶。 如果您沒有帳戶,可免費建立帳戶。
安裝及設定 Terraform。
注意
請確定所使用的 Terraform 版本為 3.99 或更新版本
使用 random_pet 為 Azure 資源群組名稱建立隨機值。
使用 azurerm_resource_group 建立 Azure 資源群組。
登入 Azure 帳戶
登入您的 Azure 帳戶,並使用下列其中一種方法進行驗證:
Terraform 只能支援使用 Azure CLI 向 Azure 進行驗證。 不支援使用 Azure PowerShell 進行驗證。 因此,雖然您可以在執行 Terraform 工作時使用 Azure PowerShell 模組,但您必須先向 Azure 進行驗證。
實作 Terraform 程式碼
若要實作 AKS 備份流程的 Terraform 程式碼,請執行下列指令碼:
注意
建立可用來測試範例 Terraform 程式碼的目錄,並將其設為您目前的目錄。
建立名為
providers.tf的檔案,並插入下列程式碼:terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "3.99.0" } } } provider "azurerm" { features {} subscription_id = "<azure_subscription_id>" tenant_id = "<azure_subscription_tenant_id>" }建立名為
main.tf的檔案,並插入下列程式碼:#Get Subscription and Tenant Id from Config data "azurerm_client_config" "current" { } #Create a Resource Group where Backup Vault and AKS Cluster will be created resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = var.resource_group_name } #Create a Resource Group where Storage Account and Snapshots related to backup will be created resource "azurerm_resource_group" "backuprg" { location = var.backup_resource_group_location name = var.backup_resource_group_name } #Create an AKS Cluster resource "azurerm_kubernetes_cluster" "akscluster" { resource_group_name = azurerm_resource_group.rg.name name = var.aks_cluster_name location = azurerm_resource_group.rg.location dns_prefix = var.dns_prefix identity { type = "SystemAssigned" } default_node_pool { name = "agentpool" vm_size = "Standard_D2_v2" node_count = var.node_count } network_profile { network_plugin = "kubenet" load_balancer_sku = "standard" } depends_on = [azurerm_resource_group.rg,azurerm_resource_group.backuprg] } #Create a Backup Vault resource "azurerm_data_protection_backup_vault" "backupvault" { name = var.backupvault_name resource_group_name = resource.azurerm_resource_group.rg.name location = resource.azurerm_resource_group.rg.location datastore_type = var.datastore_type redundancy = var.redundancy identity { type = "SystemAssigned" } depends_on = [azurerm_kubernetes_cluster.akscluster] } #Create a Backup Policy with 4 hourly backups and 7 day retention duration resource "azurerm_data_protection_backup_policy_kubernetes_cluster" "policy" { name = var.backuppolicy_name resource_group_name = var.resource_group_name vault_name = var.backupvault_name backup_repeating_time_intervals = ["R/2024-04-14T06:33:16+00:00/PT4H"] default_retention_rule { life_cycle { duration = "P7D" data_store_type = "OperationalStore" } } depends_on = [resource.azurerm_data_protection_backup_vault.backupvault] } #Create a Trusted Access Role Binding between AKS Cluster and Backup Vault resource "azurerm_kubernetes_cluster_trusted_access_role_binding" "trustedaccess" { kubernetes_cluster_id = azurerm_kubernetes_cluster.akscluster.id name = "backuptrustedaccess" roles = ["Microsoft.DataProtection/backupVaults/backup-operator"] source_resource_id = azurerm_data_protection_backup_vault.backupvault.id depends_on = [resource.azurerm_data_protection_backup_vault.backupvault, azurerm_kubernetes_cluster.akscluster] } #Create a Backup Storage Account provided in input for Backup Extension Installation resource "azurerm_storage_account" "backupsa" { name = "tfaksbackup1604" resource_group_name = azurerm_resource_group.backuprg.name location = azurerm_resource_group.backuprg.location account_tier = "Standard" account_replication_type = "LRS" depends_on = [azurerm_kubernetes_cluster_trusted_access_role_binding.trustedaccess] } #Create a Blob Container where backup items will stored resource "azurerm_storage_container" "backupcontainer" { name = "tfbackup" storage_account_name = azurerm_storage_account.backupsa.name container_access_type = "private" depends_on = [azurerm_storage_account.backupsa] } #Create Backup Extension in AKS Cluster resource "azurerm_kubernetes_cluster_extension" "dataprotection" { name = var.backup_extension_name cluster_id = azurerm_kubernetes_cluster.akscluster.id extension_type = var.backup_extension_type configuration_settings = { "configuration.backupStorageLocation.bucket" = azurerm_storage_container.backupcontainer.name "configuration.backupStorageLocation.config.storageAccount" = azurerm_storage_account.backupsa.name "configuration.backupStorageLocation.config.resourceGroup" = azurerm_storage_account.backupsa.resource_group_name "configuration.backupStorageLocation.config.subscriptionId" = data.azurerm_client_config.current.subscription_id "credentials.tenantId" = data.azurerm_client_config.current.tenant_id "configuration.backupStorageLocation.config.useAAD" = true "configuration.backupStorageLocation.config.storageAccountURI" = azurerm_storage_account.backupsa.primary_blob_endpoint } depends_on = [azurerm_storage_container.backupcontainer] } #Assign Role to Extension Identity over Storage Account resource "azurerm_role_assignment" "extensionrole" { scope = azurerm_storage_account.backupsa.id role_definition_name = "Storage Blob Data Contributor" principal_id = azurerm_kubernetes_cluster_extension.dataprotection.aks_assigned_identity[0].principal_id depends_on = [azurerm_kubernetes_cluster_extension.dataprotection] } #Assign Role to Backup Vault over AKS Cluster resource "azurerm_role_assignment" "vault_msi_read_on_cluster" { scope = azurerm_kubernetes_cluster.akscluster.id role_definition_name = "Reader" principal_id = azurerm_data_protection_backup_vault.backupvault.identity[0].principal_id depends_on = [azurerm_kubernetes_cluster.akscluster,resource.azurerm_data_protection_backup_vault.backupvault] } #Assign Role to Backup Vault over Snapshot Resource Group resource "azurerm_role_assignment" "vault_msi_read_on_snap_rg" { scope = azurerm_resource_group.backuprg.id role_definition_name = "Reader" principal_id = azurerm_data_protection_backup_vault.backupvault.identity[0].principal_id depends_on = [azurerm_kubernetes_cluster.akscluster,resource.azurerm_data_protection_backup_vault.backupvault] } #Assign Role to AKS Cluster over Snapshot Resource Group resource "azurerm_role_assignment" "cluster_msi_contributor_on_snap_rg" { scope = azurerm_resource_group.backuprg.id role_definition_name = "Contributor" principal_id = try(azurerm_kubernetes_cluster.akscluster.identity[0].principal_id,null) depends_on = [azurerm_kubernetes_cluster.akscluster,resource.azurerm_kubernetes_cluster.akscluster,resource.azurerm_resource_group.backuprg] } #Create Backup Instance for AKS Cluster resource "azurerm_data_protection_backup_instance_kubernetes_cluster" "akstfbi" { name = "example" location = azurerm_resource_group.backuprg.location vault_id = azurerm_data_protection_backup_vault.backupvault.id kubernetes_cluster_id = azurerm_kubernetes_cluster.akscluster.id snapshot_resource_group_name = azurerm_resource_group.backuprg.name backup_policy_id = azurerm_data_protection_backup_policy_kubernetes_cluster.policy.id backup_datasource_parameters { excluded_namespaces = [] excluded_resource_types = [] cluster_scoped_resources_enabled = true included_namespaces = [] included_resource_types = [] label_selectors = [] volume_snapshot_enabled = true } depends_on = [ resource.azurerm_data_protection_backup_vault.backupvault, azurerm_data_protection_backup_policy_kubernetes_cluster.policy, azurerm_role_assignment.extensionrole, azurerm_role_assignment.vault_msi_read_on_cluster, azurerm_role_assignment.vault_msi_read_on_snap_rg, azurerm_role_assignment.cluster_msi_contributor_on_snap_rg ] }建立名為
variables.tf的檔案,並插入下列程式碼:variable "aks_cluster_name" { type = string default = "Contoso_AKS_TF" description = "Name of the AKS Cluster." } variable "backup_extension_name" { type = string default = "azure-aks-backup" description = "Name of the AKS Cluster Extension." } variable "backup_extension_type" { type = string default = "microsoft.dataprotection.kubernetes" description = "Type of the AKS Cluster Extension." } variable "dns_prefix" { type = string default = "contoso-aks-dns-tf" description = "DNS Name of AKS Cluster made with Terraform" } variable "node_count" { type = number description = "The initial quantity of nodes for the node pool." default = 3 } variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "backup_resource_group_name" { type = string default = "Contoso_TF_Backup_RG" description = "Location of the resource group." } variable "backup_resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "resource_group_name" { type = string default = "Contoso_TF_RG" description = "Location of the resource group." } variable "cluster_id" { type = string default = "/subscriptions/c3d3eb0c-9ba7-4d4c-828e-cb6874714034/resourceGroups/Contoso_TF_RG/providers/Microsoft.ContainerService/managedClusters/Contoso_AKS_TF" description = "Location of the resource group." } variable "backupvault_name" { type = string default = "BackupVaultTF" description = "Name of the Backup Vault" } variable "datastore_type" { type = string default = "OperationalStore" } variable "redundancy" { type = string default = "LocallyRedundant" } variable "backuppolicy_name" { type = string default = "aksbackuppolicytfv1" }建立名為
outputs.tf的檔案,並插入下列程式碼:output "aks_resource_group" { value = azurerm_resource_group.rg.name } output "snapshot_resource_group" { value = azurerm_resource_group.backuprg.name } output "kubernetes_cluster_name" { value = azurerm_kubernetes_cluster.akscluster.name } output "backup_vault_name" { value = azurerm_data_protection_backup_vault.backupvault.name } output "backup_instance_id" { value = azurerm_data_protection_backup_instance_kubernetes_cluster.akstfbi.id }
初始化 Terraform
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點︰
-
-upgrade參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。
建立 Terraform 執行計畫
執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
-
terraform plan命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。 - 選用的
-out參數可讓您指定計畫的輸出檔。 使用-out參數可確保您所檢閱的方案就是所套用的方案。
套用 Terraform 執行計畫
執行 terraform apply 將執行計畫套用至您的雲端基礎結構。
terraform apply main.tfplan
重點︰
- 範例
terraform apply命令假設您之前已執行過terraform plan -out main.tfplan。 - 如果您為
-out參數指定了不同的檔案名稱,請在呼叫terraform apply時使用該檔案名稱。 - 若您未使用
-out參數,請呼叫terraform apply,不需要使用參數。
對 Azure 上的 Terraform 進行疑難排解
當您在 Azure 上使用 Terraform 時,可能會遇到常見問題。 了解如何進行疑難排解。
後續步驟
在本快速入門中,您已了解如何部署 Kubernetes 叢集、建立備份保存庫,以及設定 Kubernetes 叢集的備份。
深入了解:
AKS 備份概觀。 使用 Azure CLI 還原 AKS 叢集。 如何使用適用於 AKS 的 Azure 備份。