创建 Azure VM 无法 select "europe"

create Azure VM cannot select "europe"

我想使用 public 静态 IP 创建 Azure VM。

以下是我遇到的问题: 我有一个 "VS professional MSDN subscription",它给了我 45e/月的 Azure 积分。 我使用启用了 MSDN 订阅的帐户登录到 Azure 门户 (https://manage.windowsazure.com) 以创建新的虚拟机,在 "REGION/AFFINITY GROUP" 选项中,仅 "central us"、"south central us"、"east asia", "south east asia"、"japan west"可用,而我想在"north europe"

中创建虚拟机

如果我登录到 Azure 预览门户 (https://portal.azure.com),我可以在 "north europe" 中创建 VM,但是 New-AzureReservedIP powershell 命令无法使用 `-Location "North Europe" , 只有来自旧门户区域列表的可用位置允许(例如 "south central us")

在 "classic" 模式下 New-AzureReservedIP 为您的云服务创建一个新的 IP 地址。创建预留IP后,只能关联云服务

什么时候有用?

如果云服务中的所有虚拟机都处于 StoppedDeallocated 状态(或已删除),public IP 云服务地址丢失。您可以通过使用托管虚拟机的保留 IP 地址来缓解这种情况。

现在,如果您使用 https://portal.azure.com 和 "Resource Manager" 创建 VM,您可以在创建时为 VM 配置 public IP 或在创建后更改。

创建时

目前似乎不支持静态IP地址。我试图用 New-AzureRmPublicIpAddress 创建一个静态 PublicIP 地址,但它只支持动态类型。当我为开关 -AllocationMethod Static 设置 "static" 时,它 return 我这个错误

Currently this 
configuration is not supported. Network interfaces can use only dynamic public IPs.

在 ARM CLI 中,您可以使用以下命令在资源组 "momentarybehaviorrg"(必须已经存在)中创建静态 public IP,名称为 "myipname" in location "North Europe":

azure network public-ip create -g momentarybehaviorrg -n myipname -l "North Europe" -a Static

有关详细信息,请使用以下命令查看文档:

azure network public-ip create -h

Microsoft 阻止我的帐户在欧洲地区创建 VM。 他们的技术支持在内部团队讨论后启用了它。 现在我可以随心所欲地创建虚拟机了。

因此,这是后端的问题。

您可以通过 powershell 编写所有 vm 创建过程的脚本,这包括创建资源组、子网、vnet、public ip、网络安全组、存储等等。

您可以将所有 vm 资源附加到一个资源组,这样您可以通过执行一个命令行轻松删除 vm 和实际资源。

安装并导入 AzureRM

为了使用和执行下面的命令,我们需要安装和导入 AzureRM powershell 模块(如果我们还没有的话),如果您已经有了可以跳过此部分。

PS:您需要提升权限才能从 PowerShell Gallery 安装模块

`Install-Module -Name AzureRM -AllowClobber`

默认情况下,PowerShell 库未配置为 PowerShellGet 的受信任存储库。第一次使用 PSGallery 时,会显示以下消息:

Untrusted repository

    You are installing the modules from an untrusted repository. If you trust this repository, change
    its InstallationPolicy value by running the Set-PSRepository cmdlet.

    Are you sure you want to install the modules from 'PSGallery'?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend [?] Help (default is "N"):
    So, don't worry about this message.

安装后,您可以通过运行以下命令导入 AzureRM:

Import-Module AzureRM   

最后,要完成本节,我们需要连接到 Azure 帐户,只需执行此命令,系统会提示您:

# Connect to Azure with an interactive dialog for sign-in
Connect-AzureRmAccount

创建资源组

资源组是部署和管理 Azure 资源的逻辑容器。 从您的 SDK 运行 以下代码块创建资源组:

# Create variables to store the location and resource group names.
    $location = "francecentral"
    $ResourceGroupName = "resource-group-1"

    New-AzureRmResourceGroup -Name $ResourceGroupName -Location $location

创建存储资源

创建一个存储帐户来存储启动诊断的输出。

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

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

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

创建网络资源

创建一个 VNet(虚拟网络)、子网和一个 public IP 地址。创建这些 Azure 资源有助于我们为 VM 提供网络连接。

# Create a the subnet configuration
    $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
      -Name subNet-1 `
      -AddressPrefix 192.168.1.0/24

创建虚拟网络

$vnet = New-AzureRmVirtualNetwork `
      -ResourceGroupName $ResourceGroupName `
      -Location $location `
      -Name vNet-1 `
      -AddressPrefix 192.168.0.0/16 `
      -Subnet $subnetConfig

创建 public IP 地址并指定 DNS 名称

$publicip = New-AzureRmPublicIpAddress `
      -ResourceGroupName $ResourceGroupName `
      -Location $location `
      -AllocationMethod Static `
      -IdleTimeoutInMinutes 4 `
      -Name "mypublicdns$(Get-Random)"

创建 NSG(网络安全组)和 NSG 规则

NSG 使用入站和出站规则保护我们的 VM。

现在,我们需要为端口 3389 创建一个入站规则以允许传入的 RDP(远程桌面)连接,并为端口 80 创建一个入站规则以让我们的 VM 接收传入的网络流量。

为 3389 端口创建入站 NSG 规则
# Create an inbound NSG rule for the 3389 port
    # This rule will allow us to connect to the VM via an RDP connection
    $nsgrdprule = New-AzureRmNetworkSecurityRuleConfig `
      -Name nsg-rdp-rule `
      -Protocol Tcp `
      -Direction Inbound `
      -Priority 1000 `
      -SourceAddressPrefix * `
      -SourcePortRange * `
      -DestinationAddressPrefix * `
      -DestinationPortRange 3389 `
      -Access Allow
为80端口创建入站网络安全组规则
# This rule will allow the VM to receive incoming web connections via the port 80


$nsgwebrule = New-AzureRmNetworkSecurityRuleConfig `
      -Name nsg-inbound-www-rule `
      -Protocol Tcp `
      -Direction Inbound `
      -Priority 1001 `
      -SourceAddressPrefix * `
      -SourcePortRange * `
      -DestinationAddressPrefix * `
      -DestinationPortRange 80 `
      -Access Allow
创建 NSG(网络安全组)
# This will wrap up previously created rules (nsg-web-rule and nsg-rdp-rule) within an NSG
    $nsg = New-AzureRmNetworkSecurityGroup `
      -ResourceGroupName $ResourceGroupName `
      -Location $location `
      -Name nsg-1 `
      -SecurityRules $nsgrdprule,$nsgwebrule 

    # This command will create a VNC (virtual network card) and associate it with public IP address and NSG
    $nic = New-AzureRmNetworkInterface `
      -Name nic-1 `
      -ResourceGroupName $ResourceGroupName `
      -Location $location `
      -SubnetId $vnet.Subnets[0].Id `
      -PublicIpAddressId $publicip.Id `
      -NetworkSecurityGroupId $nsg.Id

    # Define a credential object to store the username and password for the VM
    $UserName='ali.mselmi'
    $Password='P@ssword123'| ConvertTo-SecureString -Force -AsPlainText
    $Credential=New-Object PSCredential($UserName,$Password)

创建 VM 配置对象

$VmName = "VirtualMachinelatest"
    $VmSize = "Standard_A1"
    $VirtualMachine = New-AzureRmVMConfig `
      -VMName $VmName `
      -VMSize $VmSize

    $VirtualMachine = Set-AzureRmVMOperatingSystem `
      -VM $VirtualMachine `
      -Windows `
      -ComputerName "MainComputer" `
      -Credential $Credential -ProvisionVMAgent

    $VirtualMachine = Set-AzureRmVMSourceImage `
      -VM $VirtualMachine `
      -PublisherName "MicrosoftWindowsServer" `
      -Offer "WindowsServer" `
      -Skus "2016-Datacenter" `
      -Version "latest"

    # Sets the operating system disk properties on a VM.
    $VirtualMachine = Set-AzureRmVMOSDisk `
      -VM $VirtualMachine `
      -CreateOption FromImage | `
      Set-AzureRmVMBootDiagnostics -ResourceGroupName $ResourceGroupName `
      -StorageAccountName $StorageAccountName -Enable |`
      Add-AzureRmVMNetworkInterface -Id $nic.Id

创建虚拟机

最后我们可以创建 VM 部署配置。

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

我们可以通过 Azure 门户检查 VM 创建

连接到虚拟机

要对我们在上一步中创建的 VM 进行远程访问,我们需要它的 public IP 地址也是先前设置的。

为此,我们只需要 运行 以下命令并获取 public IP 地址:

Get-AzureRmPublicIpAddress `
      -ResourceGroupName $ResourceGroupName | Select IpAddress

现在,我们可以在与 VM 建立远程桌面会话后进行远程桌面访问,只需将 IP 地址替换为您的 VM 的 publicIP 地址即可。 出现提示时,您可以使用创建 VM 时使用的凭据登录。

`mstsc /v publicIpAddress`

使用 RDP、SSH 或 Bastion 连接到 Azure VM

您可以通过 RDP、SSH 或 Bastion 连接到 VM,只需通过 Azure 门户单击创建的 VM,然后单击连接。

删除虚拟机

我们可以使用以下命令删除包含VM及其相关资源的资源组:

Remove-AzureRmResourceGroup `
      -Name $ResourceGroupName

最后的话...

为整个 VM 创建过程编写脚本的优势在于,我们通常不需要创建单个 VM,而是创建多个,为该过程创建脚本使我们能够灵活地在 large-scale.

原创博客post

Create a Windows Server virtual machine with PowerShell