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---
指令碼執行成功。