问题
After spending several hours of trying every relevant Azure Power Shell command to assign a virtual public IP to a Azure VM, I am back to square one. It is amazing that such a basic function cannot be done in Azure.
First, I reserved a static IP. Then create a vm. Then tried set-azurestaticvnetip. It complaint that subnetnames could not be null. So I created a virtual network in Azure portal and created a subnet. Then used set-azuresubnet -subnetnames subnet-1. Now it complaint that virtual network name could not be null. Problem is that none of the commands in the sequence takes -vnetname as a parameter.
I then found that this parameter could be passed in new-azurevm. So I deleted (what a nice workaround. i am glad I did not spend time configuring software in the vm) the VM and tried to create using this command. (image parameter is not specified in this command. I was entering that on prompt).
new-azurevmconfig -name myvm -instancesize Basic_A2|
add-azureprovisioningconfig -adminusername "myvmadmin" -windows -password "myvmP123"|
set-azuresubnet -subnetnames subnet-1 |
set-azurestaticvnetip -ipaddress 23.101.39.28 |
new-azurevm -servicename myvm -vnetname mynet –Location "East US" -waitforboot
Throws error-
new-azurevm : BadRequest : The static address 23.101.39.28 doesn't belong to the address space defined by the role's subnets.
What is wrong here? Looks to me, these instructions are for assigning a private IP to VM. It is so easy to assign a static IP to a VM created through Web Role (just put public IP name in the config file, thats it). How do I assign a Public IP to a VM. Microsoft documentation merely states that public IP can be used for a VM but did they think everybody would know how to do that?
回答1:
I think you need to create a regional vnet. This worked for me:
1) Get your current Network Config
Get-AzureVNetConfig -ExportToFile "c:\temp\MyAzNets.netcfg"
2) Open the MyAzNets.netcfg and edit/(add?) a VirtualNetworkSite. I think the key here is Location and not Affinity Group. Your reserved IP/VM will need to be in the same.
You should have something like this:
<VirtualNetworkSites>
<VirtualNetworkSite name="yourvnet" Location="West US">
<AddressSpace>
<AddressPrefix>192.168.50.0/24</AddressPrefix>
</AddressSpace>
<Subnets>
<Subnet name="yoursubnet">
<AddressPrefix>192.168.50.0/24</AddressPrefix>
</Subnet>
</Subnets>
</VirtualNetworkSite>
</VirtualNetworkSites>
3) Send it back into Azure:
Set-AzureVNetConfig -ConfigurationPath "C:\temp\MyAzNets.netcfg"
4) Add/Get your IP in the same location as your vnet.
Get-AzureReservedIP / New-AzureReservedIP
5) Create your VM.
When creating or moving a VM make sure the cloud service doesn't exist. To move a VM just hit the capture button in the management portal and give it a friendly name then delete both the VM AND cloud service.
New-AzureVMConfig -Name "my-vm01" -InstanceSize Basic_A2 -ImageName "someimage" -Label "my-vm" | Set-AzureSubnet "**yoursubnet**" | Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 61030 | Add-AzureEndpoint -LocalPort 80 -Name 'HTTP' -Protocol tcp -PublicPort 80 | Add-AzureEndpoint -LocalPort 443 -Name 'HTTPS' -Protocol tcp -PublicPort 443| New-AzureVM -ServiceName "my-vm" -ReservedIPName "**reservedipname**" -Location "West US" -VNetName "**yourvnet**"
6) The IP should be assigned. If you run Get-AzureReservedIP it should now show something like this:
ReservedIPName : reservedipname
Address : 127.0.0.1
Id : xxx
Label :
Location : West US
State : Created
InUse : True
ServiceName : my-vm
DeploymentName : my-vm
回答2:
You should be able to dynamically assign a reserved IP to a VM (or Cloud Service Web Role) after the VM is created and without any tear down. Works for me. Just do this;
New-AzureReservedIP –ReservedIPName MyReservedIP –Location "East US"
Set-AzureReservedIPAssociation -ReservedIPName MyReservedIP -ServiceName MyVMName
First command reserves the IP. Second command assigns it to the VM. The assignment is immediate and the server will reboot
回答3:
After many trials with different options in the last two days, finally this worked:
New-AzureVMConfig -Name "mysite" -InstanceSize Basic_A2 -Label "mysite" | Set-AzureSubnet "subnet-1" | add-azureprovisioningconfig -adminusername "myuser" -windows -password "mypwd"| Add-AzureEndpoint -LocalPort 80 -Name 'HTTP' -Protocol tcp -PublicPort 80 | Add-AzureEndpoint -LocalPort 443 -Name 'HTTPS' -Protocol tcp -PublicPort 443| New-AzureVM -ServiceName "mysite" -ReservedIPName "mysiteip" -Location "East US" -VNetName "mysite"
Don't know why but it did. I had tried this before without any luck.
来源:https://stackoverflow.com/questions/24833662/how-to-use-public-static-ip-with-azure-vm