I run a domain in Azure, meaning I have domain controllers and a lot of VMs joined to my domain. I run applications on those VMs such as System Center 2012 R2 components, SQL Server, customer web portals, Active Directory Federation Services, and so on. In any production environment I like to utilize static IPs and when running my services in Azure, there’s no exception.
One of the issues I run into when assigning a static IP to VMs running in Azure is the VMs periodically drop the DNS entry I’ve manually added. That’s because by default, Azure VMs utilize DHCP provided by Azure and if the DNS entry is blank in the virtual network settings, once in a while the NIC on the VMs will be set back to its default settings, i.e blank. To get around this issue the DNS server needs to be set under the virtual network configuration via the Azure portal (or through PowerShell) as shown in the image below.
However, what about setting DNS after the VMs have been deployed when DNS settings haven’t been defined in the virtual network settings beforehand?
The drawback to setting DNS in virtual network settings after VMs have been deployed, are VMs reboots are required to pick up the new DNS settings from Azure provided DHCP.
As a work around, we can remotely PowerShell to the VMs and set DNS temporarily so we can hold out for a reboot for a later time. I’ll discuss how to remotely access a VM via PowerShell later in this post.
First, it’s important to set the static IP for the VM and the following PowerShell commands show how to set a static IP for a VM in Azure:
Add-AzureAccount
get-AzureSubscription
select-azuresubscription "Azure subscription name"
$staticVM = Get-AzureVM -ServiceName cbdomain -Name myAzureVMname
set-AzureStaticVNetIP -VM $staticVM -IPAddress 172.16.0.20 | Update-AzureVM
Get-AzureStaticVNetIP -VM $staticVM
Remote PowerShell
Before remotely connecting to and Azure VM using PowerShell, we must locate the cloud service DNS name and the public port PowerShell is configured on. To find the cloud service DNS name the VM is running under navigate to the Azure Portal and select a VM then select dashboard and look on the right side of the page under DNS NAME.
To find the public PowerShell port, navigate to the VM in the Azure Portal and select ENDPOINTS. Under ENDPOINT note the public PowerShell port. Next open up Azure PowerShell and run the following command:
Enter-PSSession -ComputerName mydnsname.cloudapp.net -Port YourPSPort# -Credential username -UseSSL
Next we’ll want to set the DNS server (for example if we want to join and existing machine to the domain or access other machines by name). The following PowerShell commands show how to set DNS for the primary NIC my Azure VM (assuming the DNS setting were not set in Azure):
#sets DNS on NIC adapter
$wmi = Get-WmiObject win32_networkadapterconfiguration
$wmi.SetDNSServerSearchOrder("172.16.0.4")
Using the same PowerShell interface, run an ipconfig to make sure the DNS Server was set:
ipconfig /all | findstr /i "DNS Servers"
Conclusion
As you dive deeper into Azure, you’ll find that managing services and VMs are not much different than managing services and VMs (or even physical machines) on premises. Yes there are some differences and details to learn, however as we move into a cloud first and mobile first world, we’ll continually be challenged to discover and utilize innovative technologies to solve evolve our businesses. As a technology professional, you have a unique opportunity to bring these technologies to the forefront of your organization and bring those who are traditionally resistant to change on an exciting journey.