How to set up a multi-regional ASP.NET Core with Azure Traffic Manager

Hi, all! This post is a step-by-step tutorial on how to create an Azure Web App and deploy it over multiple regions. Our main task is to load the nearest copy of the Web App for a user. To accomplish it I create two Azure Web Apps and put them in different geo-distributed Azure Service Plans. Then I build CI (Continuous Integration) for each of my Web Apps using deployment options and public GitHub repository. I configure a custom domain and a traffic manager to get a single entry point for my application. Please check a picture of an Azure topology and final web application.

list of used azure services

Step 1. Create Azure Web Apps

First of all, I need to create a separate GeoApp resource group. It helps me to manage access to all nested services from a single place

create resource group

I can set up any region. Azure allows me to create nested services with any region without any dependence on a region of the resource group.

I create a new Application Service plan in the West Europe region, set GeoApp resource group and use S1 tier or higher. Now I can create my Europe Web App and put it as a service plan. I can create multiple web apps inside a single service plan but unfortunately, I am able to set a region only for a service plan, i.e. a container. It means that I need to create a second service plan, located in the USA and put a new app there.

create service plans and web apps

Only S and higher tiers allow me to use a traffic manager

Step 2. Create ASP.NET Core application

I assume you are familiar with ASP.NET Core so far. But it's not a problem if you have a lack of experience with "Core", as my app is rather simple. You can check these tutorials if you are still afraid of Core. Ok, I have a basic project, let's bind it with public GitHub repository. Well, the next step is to show a data center region of the loaded page. To make this happen, I need to create my custom application settings inside each Web App. Let's do it!

enter image description here

Step 3. Set application settings

Let's get back to our Azure portal and specify application settings DATA_CENTER with West Europe and South Central US values to correspond to Web Apps.

set azure application settings

The value from the settings can be gained from the code below in an index action of my home controller.

ViewBag.DataCenter = Environment.GetEnvironmentVariable("DATA_CENTER") ?? "Unknown";

Step 4. Configure CI

If you’re lazy like me, use continuous integration to handle two Azure web apps and not to create two publish profiles in your visual studio. Thanks to Microsoft Azure it is possible to link GitHub repository to an Azure web app. The main advantage of using this approach is that there is a single push, but multiple deliveries of all web apps. Please see the directions below.

enter image description here

Step 5. Setup Traffic Manager

The Traffic manager profile allows us to redirect traffic between web apps by using one of routing methods. It helps to reduce downtime and improve responsiveness. We have three routing methods: weighted traffic-routing, priority traffic-routing, and performance traffic-routing. You can read about each method in more detail here. I choose performance because it improves the responsiveness of many applications by routing traffic to the location that is 'closest' to you.

enter image description here

Step 6. Manage custom domain

On this step, I want to create a domain and link it to the traffic manager. I use freenom.com to create free domains. It should take up to 15 minutes to register and create a new domain. I create www.geoapp.tk for it.

Remember, most of the domains are free only for the first year

I like an idea to manage everything from a single place and Azure gives me such opportunity. I create a DNS zone service and transfer the domain to azure by changing nameservers. You can find azure nameserver values by clicking DNS Zone -> overview -> NS record raw.

enter image description here

Now I can manage domain records from the Azure portal. To link domain with traffic manager I simply add CNAME record which points to the geowebapp.trafficmanager.net.

enter image description here

One simple, but important SEO improvement which we can add to our application is non-www to www redirect. To apply it, I install Microsoft.AspNetCore.Rewrite NuGet package and create a rule:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
using System.Text;

namespace GeoWebApp.Utilities.Rules
{
    public class WwwRule : IRule
    {
        public void ApplyRule(RewriteContext context)
        {
            var req = context.HttpContext.Request;
            var currentHost = req.Host;
            if (!currentHost.Host.StartsWith("www."))
            {
                var newHost = new HostString("www." + currentHost.Host, currentHost.Port ?? 80);
                var newUrl = new StringBuilder()
                    .Append("http://")
                    .Append(newHost)
                    .Append(req.PathBase)
                    .Append(req.Path)
                    .Append(req.QueryString);
                context.HttpContext.Response.Redirect(newUrl.ToString());
                context.Result = RuleResult.EndResponse;
            }
        }
    }
}

Then I add an instance of the rule to the Configure method in the Startup class (Startup.cs file).

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // ... 
    options.Rules.Add(new WwwRule());
    // ...
}

Step 7. Check results

It’s time to check our results and make sure that everything works fine. As I'm in Ukraine, I see a huge header "WebApp is hosted in the West Europe data center". I have a couple of virtual machines located in the USA and Canada. I open my browser and see "WebApp is hosted in the South Central USA data center". Of course, if you forget credentials for a VM and don't have any friend abroad you can run a test using DNS Propagation Checker and see the different IPs from different locations.

DNS propogation sample

Step 8. Performance tests

Do you think it's all? I believe we should measure profit of our solution.

enter image description here

Great! I have achieved the goal but totally forgot about price. I make an annual estimate with an Azure calculator, also add an SQL database. I believe each application has its own database nowadays. See the picture below to get the answer.

estimated price

Step 9. Need to remember

Check these links to get more information about source code and Geo-distributed web application:

  1. GitHub repository
  2. Publicly available globe distributed website

Thank you for reading till the end!