在部署的比例集实例

时间:2017-10-18 07:20:40

标签: api powershell azure azure-vm-scale-set

目前我正在使用自定义脚本扩展程序在我的azure虚拟机服务器上按需运行脚本作为我们软件解决方案的一部分,我们的其他开发团队正在将应用程序迁移到规模集,我不再能够部署根据需要对比例集实例进行自定义脚本扩展。我发现在规模集实例上运行自定义脚本扩展的唯一解决方案是用它重新配置部署模板,这种方法对我不好,因为脚本应该按需运行并经常更改并且每次更新模板都是糟糕的做法。

是否有需要像常规虚拟机那样按需配置自定义脚本扩展?

powersity示例,用于在vm上进行常规按需脚本部署:

Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup `
-VMName myVM `
-Location myLocation `
-FileUri myURL `
-Run 'myScript.ps1' `
-Name DemoScriptExtension

2 个答案:

答案 0 :(得分:1)

我找到了使用PowerShell和ARM JSON模板的解决方法(我使用的是Powershell版本5.1)。在json模板中commandToExecute下的virtualMachineProfile中,指定一个几乎总是变化的值,它将在每次部署模板时强制命令重新执行。您将在我的模板中看到已向' -Date ', deployment().name添加了commandToExecutedeployment().name的值在我的New-AzureRmResourceGroupDeployment命令中指定为:

-Name $($(Get-Date -format "MM_dd_yyyy_HH_mm"))

部署名称基于日期和时间,每分钟会有所不同。

PowerShell命令:

New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile $PathToJsonTemplate -TemplateParameterFile $PathToParametersFile -Debug -Name $($(Get-Date -format "MM_dd_yyyy_HH_mm")) -force

脚本中virtualMachineProfile下的自定义脚本扩展部分如下所示(请注意commandToExecute):

"virtualMachineProfile": {
                    "extensionProfile": {
                        "extensions": [
                            {
                                "type": "Microsoft.Compute/virtualMachines/extensions",
                                "name": "MyExtensionName",
                                "location": "[parameters('location')]",
                                "properties": {
                                    "publisher": "Microsoft.Compute",
                                    "type": "CustomScriptExtension",
                                    "typeHandlerVersion": "1.8",
                                    "autoUpgradeMinorVersion": true,
                                    "settings": {
                                        "fileUris": [
                                            "[concat(parameters('customScriptExtensionSettings').storageAccountUri, '/scripts/MyScript.ps1')]"
                                        ],
                                        "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File MyScript.ps1', ' -Date ', deployment().name)]"
                                    },
                                    "protectedSettings": {
                                        "storageAccountName": "[parameters('customScriptExtensionSettings').storageAccountName]",
                                        "storageAccountKey": "[listKeys(variables('accountid'),'2015-05-01-preview').key1]"
                                    }
                                }
                            },

这将允许您在已部署的虚拟机规模集上更新自定义脚本扩展。我希望这会有所帮助!

答案 1 :(得分:0)

  

无论如何都要在比例集上配置自定义脚本扩展   像普通虚拟机那样按需实例?

目前,Azure 支持此功能。

我们只能使用VMSS自定义脚本在规模集配置时安装软件。

有关VMSS扩展的更多信息,请参阅此link