快速入門:在 Azure Stack Hub 中使用 PowerShell 建立 Linux 伺服器 VMQuickstart: Create a Linux server VM by using PowerShell in Azure Stack Hub

您可以使用 Azure Stack Hub PowerShell 建立 Ubuntu Server 16.04 LTS 虛擬機器 (VM)。You can create an Ubuntu Server 16.04 LTS virtual machine (VM) by using Azure Stack Hub PowerShell. 在本文中,您將建立和使用虛擬機器。In this article, you create and use a virtual machine. 本文也會說明如何:This article also shows you how to:

  • 使用遠端用戶端連線至 VM。Connect to the VM with a remote client.
  • 安裝 NGINX Web 伺服器,並且檢視預設首頁。Install an NGINX web server and view the default home page.
  • 清除未使用的資源。Clean up unused resources.

PrerequisitesPrerequisites

建立資源群組Create a resource group

資源群組是您可以在其中部署和管理 Azure Stack Hub 資源的邏輯容器。A resource group is a logical container where you can deploy and manage Azure Stack Hub resources. 若要建立資源群組,請執行下列程式碼區塊:To create a resource group, run the following code block:

注意

我們已為下列程式碼範例中的所有變數指派值。We've assigned values for all variables in the following code examples. 不過,您可以指派自己的值。However, you can assign your own values.

# Create variables to store the location and resource group names.
$location = "local"
$ResourceGroupName = "myResourceGroup"

New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $location

建立儲存體資源Create storage resources

建立要用於儲存開機診斷輸出的儲存體帳戶。Create a storage account that will be used for storing the boot diagnostics output.

# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "mystorageaccount"
$SkuName = "Standard_LRS"

# Create a new storage account
$StorageAccount = New-AzStorageAccount `
  -Location $location `
  -ResourceGroupName $ResourceGroupName `
  -Type $SkuName `
  -Name $StorageAccountName

Set-AzCurrentStorageAccount `
  -StorageAccountName $storageAccountName `
  -ResourceGroupName $resourceGroupName

建立網路資源Create networking resources

建立虛擬網路、子網路和公用 IP 位址。Create a virtual network, a subnet, and a public IP address. 這些資源可用來提供 VM 的網路連線能力。These resources are used to provide network connectivity to the VM.

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name mySubnet `
  -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -Name MyVnet `
  -AddressPrefix 192.168.0.0/16 `
  -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -AllocationMethod Static `
  -IdleTimeoutInMinutes 4 `
  -Name "mypublicdns$(Get-Random)"

建立網路安全性群組和網路安全性群組規則Create a network security group and a network security group rule

網路安全性群組可使用輸入和輸出規則來保護 VM。The network security group secures the VM by using inbound and outbound rules. 建立連接埠 3389 的輸入規則以允許傳入的遠端桌面連線,並建立連接埠 80 的輸入規則以允許傳入的網路流量。Create an inbound rule for port 3389 to allow incoming Remote Desktop connections and an inbound rule for port 80 to allow incoming web traffic.

# Create variables to store the network security group and rules names.
$nsgName = "myNetworkSecurityGroup"
$nsgRuleSSHName = "myNetworkSecurityGroupRuleSSH"
$nsgRuleWebName = "myNetworkSecurityGroupRuleWeb"


# Create an inbound network security group rule for port 22
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name $nsgRuleSSHName -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 $nsgRuleWebName -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow

# Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $location `
-Name $nsgName -SecurityRules $nsgRuleSSH,$nsgRuleWeb

建立 VM 的網路卡Create a network card for the VM

網路卡會將 VM 連線至子網路、網路安全性群組和公用 IP 位址。The network card connects the VM to a subnet, network security group, and public IP address.

# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzNetworkInterface `
  -Name myNic `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

建立 VMCreate a VM

建立 VM 組態。Create a VM configuration. 此設定包括部署 VM 時要使用的設定 (例如,使用者認證、大小和 VM 映像)。This configuration includes the settings to use when you deploy the VM (for example, user credentials, size, and the VM image).

# Define a credential object
$UserName='demouser'
$securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($UserName, $securePassword)

# Create the VM configuration object
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_D1"
$VirtualMachine = New-AzVMConfig `
  -VMName $VmName `
  -VMSize $VmSize

$VirtualMachine = Set-AzVMOperatingSystem `
  -VM $VirtualMachine `
  -Linux `
  -ComputerName "MainComputer" `
  -Credential $cred

$VirtualMachine = Set-AzVMSourceImage `
  -VM $VirtualMachine `
  -PublisherName "Canonical" `
  -Offer "UbuntuServer" `
  -Skus "16.04-LTS" `
  -Version "latest"

# Set the operating system disk properties on a VM
$VirtualMachine = Set-AzVMOSDisk `
  -VM $VirtualMachine `
  -CreateOption FromImage | `
  Set-AzVMBootDiagnostics -ResourceGroupName $ResourceGroupName `
  -StorageAccountName $StorageAccountName -Enable |`
  Add-AzVMNetworkInterface -Id $nic.Id

# Configure SSH keys
$sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub"

# Add the SSH key to the VM
Add-AzVMSshPublicKey -VM $VirtualMachine `
 -KeyData $sshPublicKey `
 -Path "/home/azureuser/.ssh/authorized_keys"

# Create the VM
New-AzVM `
  -ResourceGroupName $ResourceGroupName `
 -Location $location `
  -VM $VirtualMachine

VM 快速建立:完整的腳本VM Quick Create: Full script

注意

此步驟基本上是合併上述程式碼,但使用密碼而非 SSH 金鑰進行驗證。This step is essentially the preceding code merged together, but with a password rather than an SSH key for authentication.

## Create a resource group

<#
A resource group is a logical container where you can deploy and manage Azure Stack Hub resources. From your development kit or the Azure Stack Hub integrated system, run the following code block to create a resource group. Though we've assigned values for all the variables in this article, you can use these values or assign new ones.
#>

# Edit your variables, if required

# Create variables to store the location and resource group names
$location = "local"
$ResourceGroupName = "myResourceGroup"

# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "mystorageaccount"
$SkuName = "Standard_LRS"

# Create variables to store the network security group and rules names
$nsgName = "myNetworkSecurityGroup"
$nsgRuleSSHName = "myNetworkSecurityGroupRuleSSH"
$nsgRuleWebName = "myNetworkSecurityGroupRuleWeb"

# Create variable for VM password
$VMPassword = 'Password123!'

# End of variables - no need to edit anything past that point to deploy a single VM

# Create a resource group
New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $location

## Create storage resources

# Create a storage account, and then create a storage container for the Ubuntu Server 16.04 LTS image

# Create a new storage account
$StorageAccount = New-AzStorageAccount `
  -Location $location `
  -ResourceGroupName $ResourceGroupName `
  -Type $SkuName `
  -Name $StorageAccountName

Set-AzCurrentStorageAccount `
  -StorageAccountName $storageAccountName `
  -ResourceGroupName $resourceGroupName

# Create a storage container to store the VM image
$containerName = 'osdisks'
$container = New-AzureStorageContainer `
  -Name $containerName `
  -Permission Blob


## Create networking resources

# Create a virtual network, a subnet, and a public IP address, resources that are used provide network connectivity to the VM

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name mySubnet `
  -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -Name MyVnet `
  -AddressPrefix 192.168.0.0/16 `
  -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -AllocationMethod Static `
  -IdleTimeoutInMinutes 4 `
  -Name "mypublicdns$(Get-Random)"


### Create a network security group and a network security group rule

<#
The network security group secures the VM by using inbound and outbound rules. Create an inbound rule for port 3389 to allow incoming Remote Desktop connections and an inbound rule for port 80 to allow incoming web traffic.
#>

# Create an inbound network security group rule for port 22
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name $nsgRuleSSHName -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 $nsgRuleWebName -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow

# Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $location `
-Name $nsgName -SecurityRules $nsgRuleSSH,$nsgRuleWeb

### Create a network card for the VM

# The network card connects the VM to a subnet, network security group, and public IP address.

# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzNetworkInterface `
  -Name myNic `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

## Create a VM
<#
Create a VM configuration. This configuration includes the settings used when deploying the VM. For example: user credentials, size, and the VM image.
#>

# Define a credential object
$UserName='demouser'
$securePassword = ConvertTo-SecureString $VMPassword -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($UserName, $securePassword)

# Create the VM configuration object
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_D1"
$VirtualMachine = New-AzVMConfig `
  -VMName $VmName `
  -VMSize $VmSize

$VirtualMachine = Set-AzVMOperatingSystem `
  -VM $VirtualMachine `
  -Linux `
  -ComputerName "MainComputer" `
  -Credential $cred

$VirtualMachine = Set-AzVMSourceImage `
  -VM $VirtualMachine `
  -PublisherName "Canonical" `
  -Offer "UbuntuServer" `
  -Skus "16.04-LTS" `
  -Version "latest"

$osDiskName = "OsDisk"
$osDiskUri = '{0}vhds/{1}-{2}.vhd' -f `
  $StorageAccount.PrimaryEndpoints.Blob.ToString(),`
  $vmName.ToLower(), `
  $osDiskName

# Set the operating system disk properties on a VM
$VirtualMachine = Set-AzVMOSDisk `
  -VM $VirtualMachine `
  -Name $osDiskName `
  -VhdUri $OsDiskUri `
  -CreateOption FromImage | `
  Add-AzVMNetworkInterface -Id $nic.Id

# Create the VM
New-AzVM `
  -ResourceGroupName $ResourceGroupName `
 -Location $location `
  -VM $VirtualMachine

連接至 VMConnect to the VM

部署 VM 之後,請設定 VM 的 SSH 連線。After you've deployed the VM, configure an SSH connection for it. 若要取得 VM 的公用 IP 位址,請使用 >get-azpublicipaddress 命令:To get the public IP address of the VM, use the Get-AzPublicIpAddress command:

Get-AzPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress

從已安裝 SSH 的用戶端系統,使用下列命令連線至 VM。From a client system with SSH installed, use the following command to connect to the VM. 如果您使用的是 Windows,則可以使用 PuTTY 來建立連接。If you're working on Windows, you can use PuTTY to create the connection.

ssh <Public IP Address>

出現提示時,請以 azureuser 登入。When you're prompted, sign in as azureuser. 如果您在建立 SSH 金鑰時使用複雜密碼,您必須提供複雜密碼。If you used a passphrase when you created the SSH keys, you'll have to provide the passphrase.

安裝 NGINX 網頁伺服器Install the NGINX web server

若要更新套件資源及安裝最新的 NGINX 套件,請執行下列指令碼:To update package resources and install the latest NGINX package, run the following script:

#!/bin/bash

# update package source
apt-get -y update

# install NGINX
apt-get -y install nginx

檢視 NGINX 歡迎使用頁面View the NGINX welcome page

在已於 VM 上安裝 NGINX Web 伺服器並開啟連接埠 80 的情況下,您可以使用 VM 的公用 IP 位址存取 Web 伺服器。With the NGINX web server installed, and port 80 open on your VM, you can access the web server by using the VM's public IP address. 開啟網頁瀏覽器,然後前往 http://<public IP address>Open a web browser, and go to http://<public IP address>.

NGINX Web 伺服器歡迎頁面

清除資源Clean up resources

您可以使用 >new-azresourcegroup 命令,清除不再需要的資源。You can clean up the resources that you don't need any longer by using the Remove-AzResourceGroup command. 若要刪除資源群組和其所有資源,請執行下列命令︰To delete the resource group and all its resources, run the following command:

Remove-AzResourceGroup -Name myResourceGroup

後續步驟Next steps

在本快速入門中,您已部署基本的 Linux 伺服器 VM。In this quickstart, you deployed a basic Linux server VM. 若要深入了解 Azure Stack Hub VM,請移至 Azure Stack Hub 中 VM 的考量To learn more about Azure Stack Hub VMs, go to Considerations for VMs in Azure Stack Hub.