使用 ARM 和 PowerShell 配置 Azure 环境






2.87/5 (7投票s)
使用 ARM 和 PowerShell 配置 Azure 环境
引言
在将现有应用程序迁移到云或将新开发的应用程序部署到 Azure 的场景中,我们会提出一个部署架构,其中包含相当数量的资源(VM、网站、数据库等),需要在云环境中创建和部署。手动逐个创建资源不仅耗时,还会增加人力成本。我们可以考虑一种替代方法来优化流程并降低成本。本文介绍如何部署 Azure 资源和配置 Azure 环境,以及如何使用 Azure 资源管理器和 PowerShell 管理 Azure 资源。
背景
Azure 资源管理器
本文将引导您使用 Azure 资源管理器模板和 PowerShell cmdlet 在 Azure 中部署资源。我们将首先创建一个 ARM 模板来创建和部署虚拟机。然后,我们将引导您了解如何在 Azure 环境中查看和删除资源。
什么是 Azure 资源管理器?
ARM 是一个 Rest API,可以使用不同的工具将所有资源作为一组进行部署、管理和监控,并允许自动化在 Azure 中部署和配置混合基础架构。它可以在整个开发生命周期中重复部署解决方案,并确保资源以一致的状态部署。ARM 提供安全性、审计和标记功能,帮助您在部署后管理资源。它将访问控制应用于资源组中的所有服务,因为基于角色的访问控制 (RBAC) 原生集成到管理平台中。我们使用模板进行部署,该模板可以用于不同的环境,例如测试、暂存和生产环境。
对于您使用 PowerShell、Azure CLI、Azure 门户、REST API 和 Visual Studio 等工具通过 Azure 执行的任务,ARM 提供了一致的管理层。使用 ARM 模板,我们可以将完整的开发、测试或生产环境作为一组或解决方案进行部署和管理资源。ARM 支持创建自动化帐户、运行手册、凭据和变量,就像 VM 一样。每次运行模板时,都会创建或修改这些资源。
使用代码
创建 ARM 模板
- VM 创建模板:VirtualMachineTemplate.json
{ "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "newStorageAccountName": { "type": "string", "metadata": { "Description": "The name of the storage account where the VM disk is stored." } }, "adminUsername": { "type": "string", "metadata": { "Description": "The name of the administrator account on the VM." } }, "adminPassword": { "type": "securestring", "metadata": { "Description": "The administrator account password on the VM." } }, "dnsNameForPublicIP": { "type": "string", "metadata": { "Description": "The name of the public IP address used to access the VM." } } }, "variables": { "location": "Central US", "imagePublisher": "MicrosoftWindowsServer", "imageOffer": "WindowsServer", "windowsOSVersion": "2012-R2-Datacenter", "OSDiskName": "osdisk1", "nicName": "myvmnic", "addressPrefix": "10.0.0.0/16", "subnetName": "sn1", "subnetPrefix": "10.0.0.0/24", "storageAccountType": "Standard_LRS", "publicIPAddressName": "mypublicip", "publicIPAddressType": "Dynamic", "vmStorageAccountContainerName": "vhds", "vmName": "MyWindowsVM", "vmSize": "Standard_A0", "virtualNetworkName": "myvnet", "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]", "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]" }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "name": "[parameters('newStorageAccountName')]", "apiVersion": "2015-06-15", "location": "[variables('location')]", "properties": { "accountType": "[variables('storageAccountType')]" } }, { "apiVersion": "2016-03-30", "type": "Microsoft.Network/publicIPAddresses", "name": "[variables('publicIPAddressName')]", "location": "[variables('location')]", "properties": { "publicIPAllocationMethod": "[variables('publicIPAddressType')]", "dnsSettings": { "domainNameLabel": "[parameters('dnsNameForPublicIP')]" }}}, { "apiVersion": "2016-03-30", "type": "Microsoft.Network/virtualNetworks", "name": "[variables('virtualNetworkName')]", "location": "[variables('location')]", "properties": { "addressSpace": { "addressPrefixes": [ "[variables('addressPrefix')]" ]}, "subnets": [ { "name": "[variables('subnetName')]", "properties": { "addressPrefix": "[variables('subnetPrefix')]" } } ] } }, { "apiVersion": "2016-03-30", "type": "Microsoft.Network/networkInterfaces", "name": "[variables('nicName')]", "location": "[variables('location')]", "dependsOn": [ "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]", "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]" ], "properties": { "ipConfigurations": [ { "name": "ipconfig1", "properties": { "privateIPAllocationMethod": "Dynamic", "publicIPAddress": { "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]" }, "subnet": { "id": "[variables('subnetRef')]" } } } ] } }, { "apiVersion": "2015-06-15", "type": "Microsoft.Compute/virtualMachines", "name": "[variables('vmName')]", "location": "[variables('location')]", "dependsOn": [ "[concat('Microsoft.Storage/storageAccounts/', parameters('newStorageAccountName'))]", "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]" ], "properties": { "hardwareProfile": { "vmSize": "[variables('vmSize')]" }, "osProfile": { "computername": "[variables('vmName')]", "adminUsername": "[parameters('adminUsername')]", "adminPassword": "[parameters('adminPassword')]" }, "storageProfile": { "imageReference": { "publisher": "[variables('imagePublisher')]", "offer": "[variables('imageOffer')]", "sku": "[variables('windowsOSVersion')]", "version": "latest" }, "osDisk": { "name": "osdisk", "vhd": { "uri": "[concat('http://',parameters('newStorageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]" }, "caching": "ReadWrite", "createOption": "FromImage" } }, "networkProfile": { "networkInterfaces": [ { "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]" } ] } } } ]}
2.VM 参数模板:Parameter.json
param( { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json", "contentVersion": "1.0.0.0", "parameters": { "newStorageAccountName": { "value": "xxxxxxxxxxx" }, "adminUserName": { "value": "xxxxxxxxx" }, "adminPassword": { "value": "xxxxxxxxxxx" }, "dnsNameForPublicIP": { "value": "xxxxxxxxxxx" } } }
3.将这些模板保存到本地路径,该路径需要作为参数传递给 PowerShell cmdlet。
执行 PowerShell Cmdlet 的先决条件
要执行 ARM PowerShell cmdlet,需要满足以下系统先决条件
- DotNet Framework 版本 4.5 及以上
- PowerShell 版本 4.0 及以上
Azure Power Shell
使用 PowerShell 部署 Azure 资源
- 登录到 Azure:打开“Windows PowerShell”命令提示符,并使用以下 cmdlet 登录到 Azure 平台。
LogIn-AzureRmAccount or Add-AzureRmAccount
输入上述命令后,将提示登录屏幕以验证最终用户。我们需要提供 Azure 订阅 ID 和密码。
2.创建资源组:Azure 资源组是一个逻辑容器,其中包含您要作为一组管理的资源。
New-AzureRmResourceGroup -Name TestRG1 -Location "South Central US"
输出格式如下
ResourceGroupName : TestRG1 Location : southcentralus ProvisioningState : Succeeded Tags : ResourceId : /subscriptions/{guid}/resourceGroups/TestRG1
3.在 Azure 门户中部署资源
以下 cmdlet 用于在 Azure 中部署资源
New-AzureRmResourceGroupDeployment –ResourceGroupName ‘NameOfResourceGroup’ –TemplateFile ‘PathOfVMTemplateFile’ –TemplateParameterFile ‘PathOfVmParametersTemplateFile’
成功部署后,PowerShell 屏幕中将显示以下消息。
我们可以修改 json 模板以扩展资源数量,并通过执行单个 cmdlet 部署多个资源。
当我们登录到 Azure 环境时,可以看到资源已启动并正在运行。
4.使用单个 ARM 模板创建多个资源
以下 ARM 模板可以重复使用,以便一次创建多个 VM Azure。
{ "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "newStorageAccountName": { "type": "string", "defaultValue": "newstorageacfortest", "metadata": { "Description": "The name of the storage account where the VM disk is stored." } }, "adminUsername": { "type": "string", "defaultValue": "TestAdminUser", "metadata": { "Description": "The name of the administrator account on the VM." } }, "adminPassword": { "type": "securestring", "defaultValue": "Priyadarshini12#", "metadata": { "Description": "The administrator account password on the VM." } }, "numberOfInstances": { "type": "int", "defaultValue": 3, "metadata": { "description": "Number of VMs to deploy" } }, "multipleVMNames": { "type": "array", "defaultValue": [ "27WindowsVM", "WebSite", "TestApi" ], "metadata": { "description": "Multiple VM Names array" } }, "dnsNameForPublicIP": { "type": "string", "defaultValue": "dnsnameforiptest", "metadata": { "Description": "The name of the public IP address used to access the VM." } } }, "variables": { "location": "Japan East", "imagePublisher": "MicrosoftWindowsServer", "imageOffer": "WindowsServer", "windowsOSVersion": "2012-R2-Datacenter", "OSDiskName": "osdisk1", "nicName": "8myvmnic", "addressPrefix": "10.0.0.0/16", "subnetName": "sn1", "subnetPrefix": "10.0.0.0/24", "storageAccountType": "Standard_LRS", "publicIPAddressName": "8mypublicip", "publicIPAddressType": "Dynamic", "vmStorageAccountContainerName": "vhds", "vmName": "27WindowsVM,WebSite,TestApi", "vmSize": "Standard_A0", "virtualNetworkName": "myvnet8", "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]", "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]" }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "name": "[parameters('newStorageAccountName')]", "apiVersion": "2015-06-15", "location": "[variables('location')]", "properties": { "accountType": "[variables('storageAccountType')]" } }, { "apiVersion": "2016-03-30", "type": "Microsoft.Network/publicIPAddresses", "name": "[concat(variables('publicIPAddressName'), copyindex())]", "location": "[variables('location')]", "copy": { "name": "ipLoop", "count": "[parameters('numberOfInstances')]" }, "properties": { "publicIPAllocationMethod": "[variables('publicIPAddressType')]", "dnsSettings": { "domainNameLabel": "[concat(parameters('dnsNameForPublicIP'),copyindex())]" } } }, { "apiVersion": "2016-03-30", "type": "Microsoft.Network/virtualNetworks", "name": "[variables('virtualNetworkName')]", "location": "[variables('location')]", "properties": { "addressSpace": { "addressPrefixes": [ "[variables('addressPrefix')]" ] }, "subnets": [ { "name": "[variables('subnetName')]", "properties": { "addressPrefix": "[variables('subnetPrefix')]" } } ]} }, { "apiVersion": "2016-03-30", "type": "Microsoft.Network/networkInterfaces", "name": "[concat(variables('nicName'), copyindex())]", "location": "[variables('location')]", "copy": { "name": "nicLoop", "count": "[parameters('numberOfInstances')]" }, "dependsOn": [ "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]" ], "properties": { "ipConfigurations": [ { "name": "ipconfig1", "properties": { "privateIPAllocationMethod": "Dynamic", "subnet": { "id": "[variables('subnetRef')]" } }} ]} }, { "apiVersion": "2015-06-15", "type": "Microsoft.Compute/virtualMachines", "name": "[parameters('multipleVMNames')[copyIndex()]]", "location": "[variables('location')]", "copy": { "name": "vmLoop", "count": "[parameters('numberOfInstances')]" }, "dependsOn": [ "[concat('Microsoft.Storage/storageAccounts/', parameters('newStorageAccountName'))]", "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'), copyindex())]" ], "properties": { "hardwareProfile": { "vmSize": "[variables('vmSize')]" }, "osProfile": { "computerName": "[parameters('multipleVMNames')[copyIndex()]]", "adminUsername": "[parameters('adminUsername')]", "adminPassword": "[parameters('adminPassword')]" }, "storageProfile": { "imageReference": { "publisher": "[variables('imagePublisher')]", "offer": "[variables('imageOffer')]", "sku": "[variables('windowsOSVersion')]", "version": "latest" }, "osDisk": { "name": "osdisk", "vhd": { "uri": "[concat('http://',parameters('newStorageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'), copyIndex() ,'.vhd')]" }, "caching": "ReadWrite", "createOption": "FromImage" } }, "networkProfile": { "networkInterfaces": [ { "id": "[resourceId('Microsoft.Network/networkInterfaces',concat(variables('nicName'), copyindex()))]" }] } } } ] }
查找资源组中的资源
用于查找资源组中的资源的 Cmdlet
Find-AzureRmResource –ResourceGroupNameContains ‘XXXXXXXX’
使用 PowerShell 删除资源
从 Azure 中删除资源的 Cmdlet
Remove-AzureRmResource –Name ‘XXXXXXXXX’ –ResourceGroupName ‘XXXXXXXX’ –ResourceType ‘XXXXXXXXXXXXX’
我们可以在 Azure 门户中检查删除操作的状态。
结论: ARM 是一个非常有效的 API,可用于部署和管理资源。我们可以使用 ARM 模板和 PowerShell cmdlet 配置和管理 Azure 环境的资源。我们不仅可以在 SDLC 的不同阶段重复执行它们,还可以在不同的环境中(如开发、测试、生产)部署 Azure 资源,从而降低成本和时间。