Figuring out Azure VM scale set machine names

How do the individual VM host names in a scale set get their names?

Can you choose the names, or the naming convention?

How do you correlate a machine name with a VM instance ID?

When you create a scale set, every virtual machine instance in the scale set has several ways to reference it.

In the portal Instances blade, the VMs are shown by their Azure Resource names, consisting of the scale set name, underscore, instance ID:

image

VM Hostname

If you log in to a scale set VM, you’ll see a hostname like myprefix0000VU.

The hostname is composed of 2 parts:

The computerNamePrefix is a scale set property you can set when creating a VM. Many scale set Azure templates default it to the same value as the scale set name, but it doesn’t have to be the same.

The number is a base-36, also known as hexatrigesimal, representation of the VM instance ID, filled with leading zeroes to give it a fixed length of 6 characters. For example, VM ID 10 would be represented as 00000A, VM ID 35 would be 00000Z, and VM ID 1146 would be 0000VU.

Why hexatrigesimal?

Base-36 is the most compact way to represent a number using single-case alphanumeric characters (i.e. only uppercase letters and digits).

Some platforms (like Windows) have a limited maximum hostname length of 15 characters.

Using the compact hexatrigesimal numbering system to represent the VM ID provides a compromise between allowing the maximum length for a computer name prefix, and allowing for the maximum number of unique IDs before wrapping around. On Windows, this leaves a maximum machineNamePrefix length of 9 characters. The prefix can be longer on Linux.

The maximum machine name of myprefixZZZZZZ represents a VM ID of 2176782335. You would have to do a lot of scaling in and out to reach that value, but if you did, after that, the name would start wrapping around to reuse values from deleted VMs.

Note: The only part of the naming mechanism you can change is the machineNamePrefix. You can’t pick how many leading zeroes, or change other aspects of the hexatrigesimal numbering scheme.

Correlating VMSS hostname with instance ID

There is a direct correlation between a VMSS VM hostname and its instance ID. You just convert the hexatrigesimal number to decimal. Here’s a Python 3 function to convert a hostname to a decimal instance ID:

def hostname_to_vmid(hostname):
    # get last 6 characters and remove leading zeroes
    hexatrig = hostname[-6:].lstrip('0')
    multiplier = 1
    vmid = 0
    # reverse string and process each char
    for x in hexatrig[::-1]:
        if x.isdigit():
            vmid += int(x) * multiplier
        else:
            # convert letter to corresponding integer
            vmid += (ord(x) - 55) * multiplier
        multiplier *= 36
    return vmid

Here’s a function to convert an instance ID and machine name prefix to a hostname:

def vmid_to_hostname(vmid, prefix):
    hexatrig = ''
    # convert decimal vmid to hexatrigesimal base36
    while vmid > 0:
        vmid_mod = vmid % 36
        # convert int to corresponding letter
        if vmid_mod > 9:
            char = chr(vmid_mod + 55)
        else:
            char = str(vmid_mod)
        hexatrig = char + hexatrig
        vmid = int(vmid/36) 
    return prefix + hexatrig.zfill(6)

You can find an example command line script to convert these names here: https://github.com/gbowerman/vmsstools/tree/master/vmssvmname

Summary

Here’s a handy summary of the various ways you can reference a VM in a scale set:

Property

Where found

Used for

Example

instanceId

VMSS VM->instanceId

Decimal value incremented each time a new VM is created.

1146

Name

VMSS VM->name

Azure resource name, shown in the portal Instances blade.

vmssname_1146

Hostname

VMSS VM->properties->osProfile->computerName

Hostname used by the VM operating system.

myprefix0000VU

vmId

VMSS VM->properties->vmId

Unique GUID used by metrics pipeline (MDM).

3ce29928-8577-4b78-a47c-d42c675b2992

Advertisements
This entry was posted in Cloud, Computers and Internet, Python, VM Scale Sets and tagged , , , . Bookmark the permalink.

3 Responses to Figuring out Azure VM scale set machine names

  1. Pingback: Moving from Azure VMs to Azure VM Scale Sets – Runtime Instance Configuration | siliconvalve

  2. lian says:

    If you want to get the VM ID using bash script you can use the following:
    # Get the VM host name
    export PYTHONIOENCODING=utf8
    hostname=”$(curl -H Metadata:true http://169.254.169.254/metadata/instance?api-version=2017-03-01 | \
    python -c “import sys, json; print json.load(sys.stdin)[‘compute’][‘name’]”)”
    # From the VM host name, get the VM ID
    vmIdBase36=”$(echo ${hostname##*_})”

    # Convert the VM ID from base 36 to base 10
    vmIdBase10=”$(echo $((36#$vmIdBase36)))”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s