# Enhance your service availability and data locality by using Azure Traffic Manager

# Set up Traffic Manager

# Create a new Traffic Manager profile

az network traffic-manager profile create \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --name TM-MusicStream-Priority \
    --routing-method Priority \
    --unique-dns-name TM-MusicStream-Priority-$RANDOM
1
2
3
4
5

--routing-method Priority :Creates the Traffic Manager profile by using the priority routing method.

--unique-dns-name :Creates the globally unique domain name .trafficmanager.net. We use the $RANDOM Bash function to return a random whole number to ensure that the name is unique.

# Deploy the web applications

This command deploys an ARM template that provisions two servers - one in East Asia, and one in West US 2.

az deployment group create \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --template-uri  https://raw.githubusercontent.com/MicrosoftDocs/mslearn-distribute-load-with-traffic-manager/master/azuredeploy.json \
    --parameters password="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)"
1
2
3
4

# Assign the public IPs of the newly created VMs as endpoints on the Traffic Manager profile

WestId=$(az network public-ip show \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --name westus2-vm-nic-pip \
    --query id \
    --out tsv)

az network traffic-manager endpoint create \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --profile-name TM-MusicStream-Priority \
    --name "Primary-WestUS" \
    --type azureEndpoints \
    --priority 1 \
    --target-resource-id $WestId

EastId=$(az network public-ip show \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --name eastasia-vm-nic-pip \
    --query id \
    --out tsv)

az network traffic-manager endpoint create \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --profile-name TM-MusicStream-Priority \
    --name "Failover-EastAsia" \
    --type azureEndpoints \
    --priority 2 \
    --target-resource-id $EastId
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# View the created Traffic Manager endpoints

az network traffic-manager endpoint list \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --profile-name TM-MusicStream-Priority \
    --output table
1
2
3
4

# Test the application

The address for the Traffic Manager profile should match the IP address for the westus2-vm-nic-pip public IP assigned to the westus2-vm virtual machine.

# Retrieve the address for the West US 2 web app
nslookup $(az network public-ip show \
            --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
            --name eastasia-vm-nic-pip \
            --query dnsSettings.fqdn \
            --output tsv)
# Retrieve the address for the East Asia web app
nslookup $(az network public-ip show \
            --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
            --name westus2-vm-nic-pip \
            --query dnsSettings.fqdn \
            --output tsv)
# Retrieve the address for the Traffic Manager profile
nslookup $(az network traffic-manager profile show \
            --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
            --name TM-MusicStream-Priority \
            --query dnsConfig.fqdn \
            --out tsv)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Get the FQDN for the Traffic Manager profile

When visiting this URI, your browser should show a page indicating you're on the West US 2 region.

echo http://$(az network traffic-manager profile show \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
    --name TM-MusicStream-Priority \
    --query dnsConfig.fqdn \
    --out tsv)
1
2
3
4
5

# Disable the primary endpoint

After doing this, browsing the URI should indicate you're on the East Asia region.

az network traffic-manager endpoint update \
    --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa  \
    --name "Primary-WestUS" \
    --profile-name TM-MusicStream-Priority \
    --type azureEndpoints \
    --endpoint-status Disabled
1
2
3
4
5
6

# View the DNS records for the Traffic Manager profile

You should see that the Traffic Manager profile returns the East Asia IP.

# Retrieve the address for the West US 2 web app
nslookup $(az network public-ip show \
            --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
            --name eastasia-vm-nic-pip \
            --query dnsSettings.fqdn \
            --output tsv)
# Retrieve the address for the East Asia web app
nslookup $(az network public-ip show \
            --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
            --name westus2-vm-nic-pip \
            --query dnsSettings.fqdn \
            --output tsv)
# Retrieve the address for the Traffic Manager profile
nslookup $(az network traffic-manager profile show \
            --resource-group learn-e53fd6aa-0b11-458a-b367-17bb015fd7aa \
            --name TM-MusicStream-Priority \
            --query dnsConfig.fqdn \
            --out tsv)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Last Updated: 3/7/2022, 9:55:04 PM