Azure的VM提供了一种管理工具叫Azure VM Extension。它实现了一些管理虚拟机所需要的重要功能,比如:重设密码、设置RDP参数、以及许多其他关键的功能,并且Azure VM一直在添加新的Extension。默认情况下,创建的Windows VM会安装以下几个基本的VM Extension,包括IaaSDiagnostics,VMAccess和BGInfo。Linux的VM,如果开启Diagnostics功能,默认会安装LinuxDiagnostic的Extension。
通过Azure CLI可以获得Azure在某个区域提供的的Extension和相关信息。可以通过如下命令:
azure vm extension-image list
本文将介绍,通过PowerShell命令,给Azure的Linux VM添加CustomerScriptForLinux的Extension。
1 准备Azure VM
Azure VM在添加Extension时,必须是运行状态,同时,其Windows Azure Agent必须已经安装。可以通过如下命令查看是否有WAagent程序在运行:
[root@hwiscsi02 waagent]# ps -ef | grep "waagent -daemon" | grep -v grep root 935 1 0 08:29 ? 00:00:01 python /usr/sbin/waagent -daemon
2 准备PowerShell环境
//登陆Azure China
$mycred = Get-Credential -UserName admin@weiheng.partner.onmschina.cn -Message hello Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $mycred //定义Resource Group、VM和Location变量 $RGName = 'hwiscsi' $VmName = 'hwiscsi02' $Location = 'China East' //定义Extension相关信息 $ExtensionName = 'CustomScriptForLinux' $Publisher = 'Microsoft.OSTCExtensions' $version = '1.5' $PublicConf = '{"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"], "commandToExecute": "sh extension.sh" }'
$PrivateConf = '{ "storageAccountName": "hwarm", "storageAccountKey": "xxxxxxxx==" }' //执行Set-AzureRmVMExtension命令,安装extension: Set-AzureRmVMExtension -ResourceGroupName $RGName -VMName $VmName -Location $Location ` -Name $ExtensionName -Publisher $Publisher ` -ExtensionType $ExtensionName -TypeHandlerVersion $Version ` -Settingstring $PublicConf -ProtectedSettingString $PrivateConf
执行完后,屏幕输出如下:
执行成功。
或者采用Azure CLI命令:
azure vm extension set hwiscsi hwiscsi02 CustomScriptForLinux Microsoft.OSTCExtensions 1.5 -i '{"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"], "commandToExecute": "sh extension.sh"}' info: Executing command vm extension set + Looking up the VM "hwiscsi02 + Installing extension "CustomScriptForLinux", VM: "hwiscsi" info: vm extension set command OK
3 检查VM内的信息
在VM中,可以在/var/log/azure/Microsoft.OSTCExtensions.CustomScriptForLinux/1.5.2.0查看相关的日志。
2016/09/11 14:27:04 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]sequence number is 0 2016/09/11 14:27:04 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]setting file path is/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/config/0.settings 2016/09/11 14:27:04 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]JSON config: { 2016/09/11 14:27:04 "runtimeSettings": [ 2016/09/11 14:27:04 { 2016/09/11 14:27:04 "handlerSettings": { 2016/09/11 14:27:04 "publicSettings": {"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"],"commandToExecute":"sh extension.sh"} 2016/09/11 14:27:04 } 2016/09/11 14:27:04 } 2016/09/11 14:27:04 ] 2016/09/11 14:27:04 } 2016/09/11 14:27:04 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Install,success,0,Install succeeded 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]sequence number is 0 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]setting file path is/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/config/0.settings 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]JSON config: { 2016/09/11 14:27:09 "runtimeSettings": [ 2016/09/11 14:27:09 { 2016/09/11 14:27:09 "handlerSettings": { 2016/09/11 14:27:09 "publicSettings": {"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"],"commandToExecute":"sh extension.sh"} 2016/09/11 14:27:09 } 2016/09/11 14:27:09 } 2016/09/11 14:27:09 ] 2016/09/11 14:27:09 } 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]set most recent sequence number to 0 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:sh extension.sh 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Enable,transitioning,0,Launching the script... 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]sequence number is 0 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]setting file path is/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/config/0.settings 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]JSON config: { 2016/09/11 14:27:09 "runtimeSettings": [ 2016/09/11 14:27:09 { 2016/09/11 14:27:09 "handlerSettings": { 2016/09/11 14:27:09 "publicSettings": {"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"],"commandToExecute":"sh extension.sh"} 2016/09/11 14:27:09 } 2016/09/11 14:27:09 } 2016/09/11 14:27:09 ] 2016/09/11 14:27:09 } 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Will try to download files, number of retries = 10, wait SECONDS between retrievals = 20s 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:sh extension.sh 2016/09/11 14:27:09 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Downloading,transitioning,0,Downloading files... 2016/09/11 14:27:10 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]No azure storage account and key specified in protected settings. Downloading scripts from external links... 2016/09/11 14:27:10 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Converting /var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/download/0/extension.sh from DOS to Unix formats: Done 2016/09/11 14:27:10 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Removing BOM of /var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/download/0/extension.sh: Done 2016/09/11 14:27:10 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Succeeded to download files, retry count = 0 2016/09/11 14:27:10 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Internal DNS is ready, retry count = 0 2016/09/11 14:27:10 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:sh extension.sh 2016/09/11 14:27:11 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command is finished. 2016/09/11 14:27:11 ---stdout--- 2016/09/11 14:27:11 2016/09/11 14:27:11 ---errout--- 2016/09/11 14:27:11 2016/09/11 14:27:11 2016/09/11 14:27:11 [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Daemon,success,0,Command is finished. 2016/09/11 14:27:11 ---stdout---
脚本执行成功。