你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure Bicep 为 Azure Kubernetes 服务 (AKS) 群集配置保管备份

本快速入门介绍如何使用 Azure Bicep 为 Azure Kubernetes 服务 (AKS) 群集配置保管备份。

适用于 AKS 的 Azure 备份是一种以应用程序为中心的企业级云原生备份服务,让你能够快速配置 AKS 群集的备份。通过 Azure 备份,你可以采用多种方式(例如使用 Azure 门户、PowerShell、CLI、Azure 资源管理器 (ARM)、Bicep 等)备份 AKS 群集。 本快速入门介绍如何使用 Bicep 文件和 Azure PowerShell 备份 AKS 群集。 有关开发 Bicep 文件的详细信息,请参阅 Bicep 文档

Bicep 是用来以声明方式部署 Azure 资源的一种语言。 可以使用 Bicep 而非 JSON 来开发 Azure 资源管理器模板(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 窗口中。 若要粘贴代码,请右键单击 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 

后续步骤