Azure PowerShell模塊用于從PowerShell命令行或腳本創(chuàng)建和管理Azure資源。本快速入門展示了如何使用Azure PowerShell模塊在Azure中部署Linux虛擬機(VM)。本快速入門使用Canonical提供的Ubuntu 18.04 LTS市場映像。若要查看運行中的VM,也可以通過SSH登錄到該VM并安裝NGINX Web服務器。
如果沒有Azure訂閱,可在開始前創(chuàng)建一個試用帳戶。
啟動Azure本地Shell
若要在本地安裝和使用PowerShell,請運行Get-Module-ListAvailable Az.*以查找版本。如果在本地運行PowerShell,則還需運行Connect-AzAccount-Environment AzureChinaCloud來創(chuàng)建與Azure的連接。
創(chuàng)建SSH密鑰對
使用ssh-keygen創(chuàng)建SSH密鑰對。如果已有一個SSH密鑰對,則可以跳過此步驟。
Azure PowerShell
ssh-keygen-m PEM-t rsa-b 4096
系統(tǒng)會提示為密鑰對提供文件名,也可以點擊Enter以使用/home/<username>/.ssh/id_rsa默認位置。如果需要,還能夠為密鑰創(chuàng)建密碼。
有關如何創(chuàng)建SSH密鑰對的更多詳細信息,請參閱如何將SSH密鑰與Windows配合使用。
創(chuàng)建資源組
使用New-AzResourceGroup創(chuàng)建Azure資源組。資源組是在其中部署和管理Azure資源的邏輯容器:
Azure PowerShell
New-AzResourceGroup-Name"myResourceGroup"-Location"ChinaEast2"
創(chuàng)建虛擬網絡資源
創(chuàng)建虛擬網絡、子網和公共IP地址。這些資源用來與VM建立網絡連接,以及將其連接到Internet:
Azure PowerShell
#Create a subnet configuration
$subnetConfig=New-AzVirtualNetworkSubnetConfig`
-Name"mySubnet"`
-AddressPrefix 192.168.1.0/24
#Create a virtual network
$vnet=New-AzVirtualNetwork`
-ResourceGroupName"myResourceGroup"`
-Location"ChinaEast2"`
-Name"myVNET"`
-AddressPrefix 192.168.0.0/16`
-Subnet$subnetConfig
#Create a public IP address and specify a DNS name
$pip=New-AzPublicIpAddress`
-ResourceGroupName"myResourceGroup"`
-Location"ChinaEast2"`
-AllocationMethod Static`
-IdleTimeoutInMinutes 4`
-Name"mypublicdns$(Get-Random)"
創(chuàng)建Azure網絡安全組和流量規(guī)則。網絡安全組使用入站和出站規(guī)則來保護VM。在下面的示例中,將為TCP端口22創(chuàng)建允許SSH連接的入站規(guī)則。為允許傳入的Web流量,還將為TCP端口80創(chuàng)建一個入站規(guī)則。
Azure PowerShell
#Create an inbound network security group rule for port 22
$nsgRuleSSH=New-AzNetworkSecurityRuleConfig`
-Name"myNetworkSecurityGroupRuleSSH"`
-Protocol"Tcp"`
-Direction"Inbound"`
-Priority 1000`
-SourceAddressPrefix*`
-SourcePortRange*`
-DestinationAddressPrefix*`
-DestinationPortRange 22`
-Access"Allow"
#Create an inbound network security group rule for port 80
$nsgRuleWeb=New-AzNetworkSecurityRuleConfig`
-Name"myNetworkSecurityGroupRuleWWW"`
-Protocol"Tcp"`
-Direction"Inbound"`
-Priority 1001`
-SourceAddressPrefix*`
-SourcePortRange*`
-DestinationAddressPrefix*`
-DestinationPortRange 80`
-Access"Allow"
#Create a network security group
$nsg=New-AzNetworkSecurityGroup`
-ResourceGroupName"myResourceGroup"`
-Location"ChinaEast2"`
-Name"myNetworkSecurityGroup"`
-SecurityRules$nsgRuleSSH,$nsgRuleWeb
使用New-AzNetworkInterface創(chuàng)建虛擬網絡接口卡(NIC)。虛擬NIC將VM連接到子網、網絡安全組和公共IP地址。
Azure PowerShell
#Create a virtual network card and associate with public IP address and NSG
$nic=New-AzNetworkInterface`
-Name"myNic"`
-ResourceGroupName"myResourceGroup"`
-Location"ChinaEast2"`
-SubnetId$vnet.Subnets[0].Id`
-PublicIpAddressId$pip.Id`
-NetworkSecurityGroupId$nsg.Id
創(chuàng)建虛擬機
若要在PowerShell中創(chuàng)建VM,請創(chuàng)建一個配置,其中包含要使用的映像、大小和身份驗證選項等設置。然后,系統(tǒng)會使用此配置來生成VM。
定義SSH憑據(jù)、OS信息和VM大小。在此示例中,SSH密鑰存儲在~/.ssh/id_rsa.pub中。
Azure PowerShell
#Define a credential object
$securePassword=ConvertTo-SecureString''-AsPlainText-Force
$cred=New-Object System.Management.Automation.PSCredential("azureuser",$securePassword)
#Create a virtual machine configuration
$vmConfig=New-AzVMConfig`
-VMName"myVM"`
-VMSize"Standard_D1_v2"|`
Set-AzVMOperatingSystem`
-Linux`
-ComputerName"myVM"`
-Credential$cred`
-DisablePasswordAuthentication|`
Set-AzVMSourceImage`
-PublisherName"Canonical"`
-Offer"UbuntuServer"`
-Skus"18.04-LTS"`
-Version"latest"|`
Add-AzVMNetworkInterface`
-Id$nic.Id
#Configure the SSH key
$sshPublicKey=cat~/.ssh/id_rsa.pub
Add-AzVMSshPublicKey`
-VM$vmconfig`
-KeyData$sshPublicKey`
-Path"/home/azureuser/.ssh/authorized_keys"
現(xiàn)在,組合前面的配置定義來使用New-AzVM創(chuàng)建虛擬機:
Azure PowerShell
New-AzVM`
-ResourceGroupName"myResourceGroup"`
-Location chinaeast2-VM$vmConfig
部署VM需要數(shù)分鐘。部署完成后,請轉到下一部分。
連接到VM
使用公共IP地址創(chuàng)建與VM的SSH連接。若要查看VM的公共IP地址,請使用Get-AzPublicIpAddress cmdlet:
Azure PowerShell
Get-AzPublicIpAddress-ResourceGroupName"myResourceGroup"|Select"IpAddress"
使用用于創(chuàng)建SSH密鑰對的相同shell,將以下命令粘貼到shell中以創(chuàng)建SSH會話。將10.111.12.123替換為VM的IP地址。
Bash
ssh azureuser 10.111.12.123
出現(xiàn)提示時,請輸入登錄用戶名azureuser。如果將通行短語與SSH密鑰配合使用,則需要在出現(xiàn)提示時將其輸入。
安裝NGINX
若要查看運行中的VM,請安裝NGINX Web服務器。在SSH會話中更新包源,然后安裝最新的NGINX包。
Bash
sudo apt-get-y update
sudo apt-get-y install nginx
完成后,鍵入exit以離開SSH會話。
查看運行中的Web服務器
使用所選的Web瀏覽器查看默認的NGINX歡迎頁。輸入VM的公共IP地址作為Web地址??梢栽赩M概覽頁上或此前使用過的SSH連接字符串中找到公共IP地址。
清理資源
不再需要時,可以使用Remove-AzResourceGroup cmdlet刪除資源組、VM和所有相關資源:
Azure PowerShell
復制
Remove-AzResourceGroup-Name"myResourceGroup"