共用方式為


快速入門:使用 Azure CLI 設定 Azure Kubernetes Service (AKS) 叢集的保存庫備份

本快速入門說明如何使用 Azure CLI 為 Azure Kubernetes Service (AKS) 叢集配置保護備份。

適用於 AKS 的 Azure 備份是雲端原生、企業就緒、以應用程式為中心的備份服務,可讓您快速設定 AKS 叢集的備份。

開始之前

設定 AKS 叢集的保存庫備份之前,請確定符合下列必要條件:

建立備份保存庫

若要建立備份保存庫,請執行下列命令:

az dataprotection backup-vault create --resource-group $backupvaultresourcegroup --vault-name $backupvault --location $region --type SystemAssigned --storage-settings datastore-type="VaultStore" type="GeoRedundant"

新建立的保存庫具有設定為全域備援的記憶體設定,因此儲存在保存庫層中的備份將可在 Azure 配對區域中使用。 保存庫建立完成後,請建立備份原則來保護 AKS 叢集。

建立備份原則

使用 命令 az dataprotection backup-policy get-default-policy-template擷取原則範本。

az dataprotection backup-policy get-default-policy-template --datasource-type AzureKubernetesService > akspolicy.json

我們會更新備份原則的預設範本,並新增保留規則,以在保存庫層中保留每天第一次成功的備份,持續 30 天。


az dataprotection backup-policy retention-rule create-lifecycle  --count 30 --retention-duration-type Days --copy-option ImmediateCopyOption --target-datastore VaultStore --source-datastore OperationalStore > ./retentionrule.json

az dataprotection backup-policy retention-rule set --lifecycles ./retentionrule.json --name Daily --policy ./akspolicy.json > ./akspolicy.json

原則 JSON 具有所有必要的值之後,請繼續從原則物件建立新的原則。

az dataprotection backup-policy create -g testBkpVaultRG --vault-name TestBkpVault -n mypolicy --policy akspolicy.json

準備 AKS 叢集以進行備份

保存庫和原則建立完成後,您必須執行下列必要條件,讓 AKS 叢集做好備份準備:

  1. 建立儲存體帳戶和 Blob 容器

    AKS 的備份會將 Kubernetes 資源儲存在 Blob 容器中作為備份。 若要讓 AKS 叢集準備好進行備份,您必須在叢集中安裝延伸模組。 此延伸模組需要儲存體帳戶和 Blob 容器作為輸入。

    若要建立新的儲存體帳戶,請執行下列命令:

    az storage account create --name $storageaccount --resource-group $storageaccountresourcegroup --location $region --sku Standard_LRS
    

    儲存體帳戶建立完成後,請執行下列命令,在其中建立 Blob 容器:

    az storage container create --name $blobcontainer --account-name $storageaccount --auth-mode login
    
  2. 安裝備份延伸模組

    備份延伸模組必須安裝在 AKS 叢集中,才能執行任何備份和還原作業。 備份延伸模組會在叢集中建立命名空間 dataprotection-microsoft,並使用相同的命名空間來部署其資源。 延伸模組需要儲存體帳戶和 Blob 容器作為安裝的輸入。

    az k8s-extension create --name azure-aks-backup --extension-type microsoft.dataprotection.kubernetes --scope cluster --cluster-type managedClusters --cluster-name $akscluster --resource-group $aksclusterresourcegroup --release-train stable --configuration-settings blobContainer=$blobcontainer storageAccount=$storageaccount storageAccountResourceGroup=$storageaccountresourcegroup storageAccountSubscriptionId=$subscriptionId
    

    在延伸模組安裝期間,系統會在 AKS 叢集的節點集區資源群組中建立使用者身分識別。 若要讓延伸模組存取儲存體帳戶,您必須為此身分識別提供儲存體 Blob 資料參與者角色。 若要指派必要的角色,請執行下列命令:

    az role assignment create --assignee-object-id $(az k8s-extension show --name azure-aks-backup --cluster-name $akscluster --resource-group $aksclusterresourcegroup --cluster-type managedClusters --query aksAssignedIdentity.principalId --output tsv) --role 'Storage Blob Data Contributor' --scope /subscriptions/$subscriptionId/resourceGroups/$storageaccountresourcegroup/providers/Microsoft.Storage/storageAccounts/$storageaccount
    
  3. 啟用信任存取

    若要讓備份保存庫與 AKS 叢集連線,您必須啟用信任存取,因為它可讓備份保存庫能夠直接看到 AKS 叢集。

    若要啟用信任存取,請執行下列命令:

    az aks trustedaccess rolebinding create --cluster-name $akscluster --name backuprolebinding --resource-group $aksclusterresourcegroup --roles Microsoft.DataProtection/backupVaults/backup-operator --source-resource-id /subscriptions/$subscriptionId/resourceGroups/$backupvaultresourcegroup/providers/Microsoft.DataProtection/BackupVaults/$backupvault
    

設定 AKS 叢集的保存庫備份

有了已建立的備份保存庫和備份原則,以及處於備妥以進行備份狀態的 AKS 叢集,您現在可以開始備份 AKS 叢集。

準備要求

備份的設定會以兩個步驟執行:

  1. 準備備份設定,以定義要使用 az dataprotection backup-instance initialize-backupconfig 命令備份哪些叢集資源。 命令會產生 JSON,您可以視需要更新以定義 AKS 叢集的備份設定。

    az dataprotection backup-instance initialize-backupconfig --datasource-type AzureKubernetesService > aksbackupconfig.json
    
  2. 使用 az dataprotection backup-instance initialize 命令搭配相關的保存庫、原則、AKS 叢集、備份設定和快照集資源群組來準備相關要求。

    az dataprotection backup-instance initialize --datasource-id /subscriptions/$subscriptionId/resourceGroups/$aksclusterresourcegroup/providers/Microsoft.ContainerService/managedClusters/$akscluster --datasource-location $region --datasource-type AzureKubernetesService --policy-id /subscriptions/$subscriptionId/resourceGroups/$backupvaultresourcegroup/providers/Microsoft.DataProtection/backupVaults/$backupvault/backupPolicies/$backuppolicy --backup-configuration ./aksbackupconfig.json --friendly-name ecommercebackup --snapshot-resource-group-name $snapshotresourcegroup > backupinstance.json
    

現在,使用此命令的 JSON 輸出來設定 AKS 叢集的備份。

指派必要的權限並驗證

備妥要求后,您必須先執行下列命令來驗證所需的角色是否已指派給所涉及的資源:

az dataprotection backup-instance validate-for-backup --backup-instance ./backupinstance.json --ids /subscriptions/$subscriptionId/resourceGroups/$backupvaultresourcegroup/providers/Microsoft.DataProtection/backupVaults/$backupvault

如果驗證失敗,而且缺少某些權限,您可以執行下列命令來指派這些權限:

az dataprotection backup-instance update-msi-permissions command.
az dataprotection backup-instance update-msi-permissions --datasource-type AzureKubernetesService --operation Backup --permissions-scope ResourceGroup --vault-name $backupvault --resource-group $backupvaultresourcegroup --backup-instance backupinstance.json

指派許可權之後,請使用先前的 backup 命令驗證重新驗證,然後繼續設定備份:

az dataprotection backup-instance create --backup-instance  backupinstance.json --resource-group $backupvaultresourcegroup --vault-name $backupvault

下一步