共用方式為


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

本快速入門說明如何使用 Azure Bicep 設定 Azure Kubernetes Service (AKS) 叢集的保存庫備份。

AKS 的 Azure 備份 是雲端原生、企業就緒、以應用程式為中心的備份服務,可讓您快速設定 AKS 叢集的備份。 Azure 備份 可讓您使用多個選項來備份 AKS 叢集,例如 Azure 入口網站、PowerShell、CLI、Azure Resource Manager、Bicep 等等。 本快速入門說明如何使用 Bicep 檔案和 Azure PowerShell 來備份 AKS 叢集。 如需開發 Bicep 檔案的詳細資訊,請參閱 Bicep 檔

Bicep 是以宣告方式部署 Azure 資源的語言。 您可以使用 Bicep (而不是 JSON) 來開發 Azure Resource Manager 範本 (ARM 範本)。 Bicep 語法可降低複雜性,並改善開發體驗。 Bicep 將 ARM 範本 JSON 以清楚易懂的方式加以簡化,以提供所有 JSON 範本功能。 在部署期間,Bicep CLI 會將 Bicep 檔案轉換成 ARM 範本 JSON。 Bicep 檔案會陳述 Azure 資源和資源屬性,而不需要撰寫一連串的程式設計命令來建立資源。

在 ARM 範本中有效的資源類型、API 版本和屬性,在 Bicep 檔案中也是有效。

必要條件

若要設定您的環境以進行 Bicep 開發,請參閱安裝 Bicep 工具 (部分機器翻譯)。

注意

如文章所詳述,安裝最新 Azure PowerShell 模組和 Bicep CLI。

檢閱範本

此範本可讓您設定 AKS 叢集的備份。 在此範本中,我們會使用 AKS 叢集的備份原則建立備份保存庫,其排程為四小時和七天的保留期間。

@description('Location for the resource group')
param resourceGroupLocation string
@description('Name of the resource group for AKS and Backup Vault')
param resourceGroupName string
@description('Name of the resource group for storage account and snapshots')
param backupResourceGroupName string
@description('Location for the backup resource group')
param backupResourceGroupLocation string
@description('AKS Cluster name')
param aksClusterName string
@description('DNS prefix for AKS')
param dnsPrefix string
@description('Node count for the AKS Cluster')
param nodeCount int
@description('Name of the Backup Vault')
param backupVaultName string
@description('Datastore type for the Backup Vault')
param datastoreType string
@description('Redundancy type for the Backup Vault')
param redundancy string
@description('Backup policy name')
param backupPolicyName string
@description('Name of the Backup Extension')
param backupExtensionName string
@description('Type of Backup Extension')
param backupExtensionType string
@description('Name of the Storage Account')
param storageAccountName string

var backupContainerName = 'tfbackup'

resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  location: resourceGroupLocation
  name: resourceGroupName
}

resource backupRg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  location: backupResourceGroupLocation
  name: backupResourceGroupName
}

resource aksCluster 'Microsoft.ContainerService/managedClusters@2023-05-01' = {
  location: resourceGroupLocation
  name: aksClusterName
  properties: {
    dnsPrefix: dnsPrefix
    agentPoolProfiles: [
      {
        name: 'agentpool'
        count: nodeCount
        vmSize: 'Standard_D2_v2'
        type: 'VirtualMachineScaleSets'
        mode: 'System'
      }
    ]
    identity: {
      type: 'SystemAssigned'
    }
    networkProfile: {
      networkPlugin: 'kubenet'
      loadBalancerSku: 'standard'
    }
  }
  dependsOn: [
    rg
    backupRg
  ]
}

resource backupVault 'Microsoft.DataProtection/backupVaults@2023-01-01' = {
  location: resourceGroupLocation
  name: backupVaultName
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    dataStoreType: datastoreType
    redundancy: redundancy
  }
  dependsOn: [
    aksCluster
  ]
}

resource backupPolicy 'Microsoft.DataProtection/backupVaults/backupPolicies@2023-01-01' = {
  name: '${backupVaultName}/${backupPolicyName}'
  properties: {
    backupRepeatingTimeIntervals: ['R/2024-04-14T06:33:16+00:00/PT4H']
    defaultRetentionRule: {
      lifeCycle: {
        duration: 'P7D'
        dataStoreType: 'OperationalStore'
      }
    }
  }
  dependsOn: [
    backupVault
  ]
}

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-05-01' = {
  location: backupResourceGroupLocation
  name: storageAccountName
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  dependsOn: [
    aksCluster
  ]
}

resource backupContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2021-04-01' = {
  name: '${storageAccount.name}/default/${backupContainerName}'
  properties: {
    publicAccess: 'None'
  }
  dependsOn: [
    storageAccount
  ]
}

resource backupExtension 'Microsoft.KubernetesConfiguration/extensions@2023-05-01' = {
  name: '${aksClusterName}/${backupExtensionName}'
  properties: {
    extensionType: backupExtensionType
    configurationSettings: {
      'configuration.backupStorageLocation.bucket': backupContainerName
      'configuration.backupStorageLocation.config.storageAccount': storageAccountName
      'configuration.backupStorageLocation.config.resourceGroup': backupResourceGroupName
      'configuration.backupStorageLocation.config.subscriptionId': subscription().subscriptionId
      'credentials.tenantId': subscription().tenantId
    }
  }
  dependsOn: [
    backupContainer
  ]
}

output aksClusterId string = aksCluster.id
output backupVaultId string = backupVault.id

部署範本

若要部署此範本,請將它儲存在 GitHub 或您慣用的位置,然後在殼層視窗中貼上下列 PowerShell 腳本。 若要貼上程式碼,請以滑鼠右鍵按一下 Shell 視窗,然後選取 [貼上]

$projectName = Read-Host -Prompt "Enter a project name (limited to eight characters) that is used to generate Azure resource names"
$location = Read-Host -Prompt "Enter the location (for example, centralus)"

$resourceGroupName = "${projectName}rg"
$templateUri = "templateURI"

New-AzResourceGroup -Name $resourceGroupName -Location $location
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -projectName $projectName 

下一步