使用模板在 Azure 虚拟机规模集上为 Azure 资源配置托管标识
Azure 资源的托管标识是 Microsoft Entra ID 的一项功能。 支持 Azure 资源的托管标识的每个 Azure 服务都受其自己的时间线限制。 在开始之前,请务必查看资源的托管标识的可用性状态以及已知问题。
Azure 资源的托管标识在 Microsoft Entra ID 中为 Azure 服务提供了一个自动托管标识。 可以使用此标识向支持 Microsoft Entra 身份验证的任何服务进行身份验证,这样就无需在代码中插入凭据了。
本文将介绍如何使用 Azure 资源管理器部署模板在 Azure 虚拟机规模集上执行以下 Azure 资源托管标识操作:
- 在 Azure 虚拟机规模集上启用和禁用系统分配托管标识
- 在 Azure 虚拟机规模集上添加和删除用户分配托管标识
先决条件
如果不熟悉 Azure 资源的托管标识,请查阅概述部分。 请务必了解系统分配的托管标识与用户分配的托管标识之间的差异。
如果还没有 Azure 帐户,请先注册试用帐户,然后再继续。
若要执行本文中的管理操作,帐户需要以下基于 Azure 角色的访问控制分配:
注意
无需其他 Microsoft Entra 目录角色分配。
Azure Resource Manager 模板
与 Azure 门户和脚本一样,Azure 资源管理器模板支持部署由 Azure 资源组定义的新资源或修改后的资源。 有多种可用于执行模板编辑和部署的方法(包括本地方法和基于门户的方法),包括:
- 使用 Azure 市场中的自定义模板,这样可以从头开始创建模板,也可以在现有常见模板或快速启动模板的基础之上操作。
- 派生自现有资源组,具体方法是从原始部署或当前部署导出模板。
- 使用本地 JSON 编辑器(例如 VS Code),然后使用 PowerShell 或 CLI 进行上传和部署。
- 使用 Visual Studio Azure 资源组项目同时创建和部署模板。
无论选择哪种方法,在初始部署和重新部署期间,模板语法都是相同的。 在新 VM 或现有 VM 上启用 Azure 资源托管标识的方式相同。 此外,默认情况下,Azure 资源管理器还会对部署执行增量更新。
系统分配的托管标识
在此部分中,将使用 Azure 资源管理器模板启用和禁用系统分配托管标识。
在创建虚拟机规模集期间或在现有的虚拟机规模集上启用系统分配的托管标识
无论是在本地登录到 Azure 还是通过 Azure 门户登录,请使用与包含虚拟机规模集的 Azure 订阅关联的帐户。
要启用系统分配托管标识,请将模板加载到编辑器中,在 resources 节中找到所关注的
Microsoft.Compute/virtualMachinesScaleSets
资源,并在与identity
属性相同的级别添加"type": "Microsoft.Compute/virtualMachinesScaleSets"
属性。 使用以下语法:"identity": { "type": "SystemAssigned" }
完成后,以下各部分应当会添加到模板的“资源”部分,并应类似于下面所示的示例:
"resources": [ { //other resource provider properties... "apiVersion": "2018-06-01", "type": "Microsoft.Compute/virtualMachineScaleSets", "name": "[variables('vmssName')]", "location": "[resourceGroup().location]", "identity": { "type": "SystemAssigned", }, "properties": { //other resource provider properties... "virtualMachineProfile": { //other virtual machine profile properties... } } } ]
从 Azure 虚拟机规模集中禁用系统分配托管标识
如果虚拟机规模集不再需要系统分配托管标识,请执行以下操作:
无论是在本地登录到 Azure 还是通过 Azure 门户登录,请使用与包含虚拟机规模集的 Azure 订阅关联的帐户。
将模板加载到编辑器,并在
resources
部分找到相关的Microsoft.Compute/virtualMachineScaleSets
资源。 如果 VM 只有系统分配的托管标识,则可以将标识类型更改为None
来禁用它。Microsoft.Compute/virtualMachineScaleSets API 版本 2018-06-01
如果 apiVersion 为
2018-06-01
并且 VM 同时具有系统和用户分配的托管标识,请从标识类型中删除SystemAssigned
并保留UserAssigned
以及 userAssignedIdentities 字典值。Microsoft.Compute/virtualMachineScaleSets API 版本 2018-06-01
如果 apiVersion 为
2017-12-01
并且虚拟机规模集同时具有系统和用户分配的托管标识,请从标识类型中删除SystemAssigned
,并保留UserAssigned
以及用户分配托管标识的identityIds
数组。以下示例演示如何从没有用户分配托管标识的虚拟机规模集中删除系统分配托管标识:
{ "name": "[variables('vmssName')]", "apiVersion": "2018-06-01", "location": "[parameters(Location')]", "identity": { "type": "None" } }
用户分配的托管标识
在本部分中,将使用 Azure 资源管理器模板向虚拟机规模集分配用户分配托管标识。
注意
要使用 Azure 资源管理器模板创建用户分配托管标识,请参阅创建用户分配托管标识。
将用户分配的托管标识分配给虚拟机规模集
在
resources
元素下添加以下条目,向虚拟机规模集分配用户分配托管标识。 请务必将<USERASSIGNEDIDENTITY>
替换为你创建的用户分配的托管标识的名称。Microsoft.Compute/virtualMachineScaleSets API 版本 2018-06-01
如果 apiVersion 为
2018-06-01
,则用户分配托管标识以userAssignedIdentities
字典格式存储,并且<USERASSIGNEDIDENTITYNAME>
值必须存储在模板的variables
节中定义的某个变量中。{ "name": "[variables('vmssName')]", "apiVersion": "2018-06-01", "location": "[parameters(Location')]", "identity": { "type": "userAssigned", "userAssignedIdentities": { "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {} } } }
Microsoft.Compute/virtualMachineScaleSets API 版本 2017-12-01
如果
apiVersion
为2017-12-01
或早期版本,则用户分配托管标识存储在identityIds
数组中,并且<USERASSIGNEDIDENTITYNAME>
值必须存储在模板的 variables 节中定义的某个变量中。{ "name": "[variables('vmssName')]", "apiVersion": "2017-03-30", "location": "[parameters(Location')]", "identity": { "type": "userAssigned", "identityIds": [ "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITY>'))]" ] } }
完成后,模板应当类似于以下示例:
Microsoft.Compute/virtualMachineScaleSets API 版本 2018-06-01
"resources": [ { //other resource provider properties... "apiVersion": "2018-06-01", "type": "Microsoft.Compute/virtualMachineScaleSets", "name": "[variables('vmssName')]", "location": "[resourceGroup().location]", "identity": { "type": "UserAssigned", "userAssignedIdentities": { "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {} } }, "properties": { //other virtual machine properties... "virtualMachineProfile": { //other virtual machine profile properties... } } } ]
Microsoft.Compute/virtualMachines API 版本 2017-12-01
"resources": [ { //other resource provider properties... "apiVersion": "2017-12-01", "type": "Microsoft.Compute/virtualMachineScaleSets", "name": "[variables('vmssName')]", "location": "[resourceGroup().location]", "identity": { "type": "UserAssigned", "identityIds": [ "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]" ] }, "properties": { //other virtual machine properties... "virtualMachineProfile": { //other virtual machine profile properties... } } } ]
从 Azure 虚拟机规模集删除用户分配的托管标识
如果虚拟机规模集不再需要用户分配托管标识,请执行以下操作:
无论是在本地登录到 Azure 还是通过 Azure 门户登录,请使用与包含虚拟机规模集的 Azure 订阅关联的帐户。
将模板加载到编辑器,并在
resources
部分找到相关的Microsoft.Compute/virtualMachineScaleSets
资源。 如果虚拟机规模集只有用户分配的托管标识,则可以通过将标识类型更改为None
来禁用它。以下示例演示如何从没有系统分配的托管标识的 VM 中删除所有用户分配的托管标识:
{ "name": "[variables('vmssName')]", "apiVersion": "2018-06-01", "location": "[parameters(Location')]", "identity": { "type": "None" } }
Microsoft.Compute/virtualMachineScaleSets API 版本 2018-06-01
若要从虚拟机规模集中删除单个用户分配的托管标识,请将其从
userAssignedIdentities
字典中删除。如果具有系统分配的标识,请将其保持在
identity
值下的type
值中。Microsoft.Compute/virtualMachineScaleSets API 版本 2017-12-01
要从虚拟机规模集中删除单个用户分配托管标识,请将其从
identityIds
数组中删除。如果具有系统分配的托管标识,请将其保持在
identity
值下的type
值中。