你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。
本文包含一个示例 Bicep 文件,用于开始使用 Azure Chaos Studio,其中包括:
- 将资源作为目标载入(例如虚拟机)
- 在目标上启用功能(例如虚拟机关闭)
- 创建 Chaos Studio 试验
- 分配执行试验所需的权限
生成虚拟机关闭试验
在此示例中,我们将创建一个包含单个目标资源和单个虚拟机关闭故障的混沌试验。 可以通过引用错误库和建议的角色分配来修改试验。
先决条件
此示例假定:
- Chaos Studio 资源提供程序已注册到 Azure 订阅
- 你在 Chaos Studio 支持的区域中已有资源组
- 已在资源组中部署虚拟机
查阅 Bicep 文件
@description('The existing virtual machine resource you want to target in this experiment')
param targetName string
@description('Desired name for your Chaos Experiment')
param experimentName string
@description('Desired region for the experiment, targets, and capabilities')
param location string = resourceGroup().location
// Define Chaos Studio experiment steps for a basic Virtual Machine Shutdown experiment
param experimentSteps array = [
{
name: 'Step1'
branches: [
{
name: 'Branch1'
actions: [
{
name: 'urn:csci:microsoft:virtualMachine:shutdown/1.0'
type: 'continuous'
duration: 'PT10M'
parameters: [
{
key: 'abruptShutdown'
value: 'true'
}
]
selectorId: 'Selector1'
}
]
}
]
}
]
// Reference the existing Virtual Machine resource
resource vm 'Microsoft.Compute/virtualMachines@2023-03-01' existing = {
name: targetName
}
// Deploy the Chaos Studio target resource to the Virtual Machine
resource chaosTarget 'Microsoft.Chaos/targets@2024-01-01' = {
name: 'Microsoft-VirtualMachine'
location: location
scope: vm
properties: {}
// Define the capability -- in this case, VM Shutdown
resource chaosCapability 'capabilities' = {
name: 'Shutdown-1.0'
}
}
// Define the role definition for the Chaos experiment
resource chaosRoleDefinition 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
scope: vm
// In this case, Virtual Machine Contributor role -- see https://learn.microsoft.com/azure/role-based-access-control/built-in-roles
name: '9980e02c-c2be-4d73-94e8-173b1dc7cf3c'
}
// Define the role assignment for the Chaos experiment
resource chaosRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(vm.id, chaosExperiment.id, chaosRoleDefinition.id)
scope: vm
properties: {
roleDefinitionId: chaosRoleDefinition.id
principalId: chaosExperiment.identity.principalId
principalType: 'ServicePrincipal'
}
}
// Deploy the Chaos Studio experiment resource
resource chaosExperiment 'Microsoft.Chaos/experiments@2024-01-01' = {
name: experimentName
location: location // Doesn't need to be the same as the Targets & Capabilities location
identity: {
type: 'SystemAssigned'
}
properties: {
selectors: [
{
id: 'Selector1'
type: 'List'
targets: [
{
id: chaosTarget.id
type: 'ChaosTarget'
}
]
}
]
steps: experimentSteps
}
}
部署 Bicep 文件
将该 Bicep 文件在本地计算机上另存为
chaos-vm-shutdown.bicep
。使用 Azure CLI 或 Azure PowerShell 部署 Bicep 文件,从而将
exampleRG
替换为包含要面向的虚拟机的现有资源组。az deployment group create --resource-group exampleRG --template-file chaos-vm-shutdown.bicep
出现提示时,输入以下值:
- targetName:资源组中要面向的现有虚拟机的名称
- experimentName:混沌试验的所需名称
模板应在几分钟内部署。 部署完成后,导航到 Azure 门户中的 Chaos Studio,选择“试验”,然后查找由模板创建的试验。 选择它,然后启动试验。