次の方法で共有


クイックスタート: Terraform を使用して Azure Kubernetes Service (AKS) クラスターのコンテナー バックアップを構成する

このクイックスタートでは、Terraform を使用して Azure Kubernetes Service (AKS) クラスターのコンテナー バックアップを構成する方法について説明します。

Azure Backup for AKS は、AKS クラスターのバックアップをすばやく構成できる、クラウドネイティブかつエンタープライズ対応で、アプリケーション中心のバックアップ サービスです。

Note

この記事に記載されている、クラスターをデプロイして AKS バックアップで保護する方法に関する手順は、評価のみを目的としています。

運用環境に対応したクラスターをデプロイして、詳細なバックアップ設定を使用する前に、ベースライン参照アーキテクチャを理解して、ビジネス要件にどの程度合致しているかを検討することをお勧めします。

前提条件

AKS バックアップを構成する前に確認する必要がある事項:

Azure アカウントにログインする

Azure アカウントにログインし、次のいずれかの方法を使用して認証します。

Terraform は、Azure CLI を使用した Azure への認証のみをサポートしています。 Azure PowerShell を使用した認証はサポートされていません。 そのため、Terraform の作業を行うときには Azure PowerShell モジュールを使用できますが、まず Azure に対する認証を行う必要があります。

Terraform コードを実装する

AKS バックアップ フロー用の Terraform コードを実装するには、次のスクリプトを実行します。

  1. サンプルの Terraform コードのテストに使用できるディレクトリを作成し、それを現在のディレクトリにします。

  2. 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>"
    }
    
  3. 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
        }
      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 Account 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
      ]
    }
    
  4. 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"
    }
    
  5. 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 クラスターのデプロイ、Backup ボールトの作成、Kubernetes クラスターのバックアップの構成を行う方法について説明しました。

各項目の詳細情報