RDP Automatic scaling in Azure

I am moving a customer from a traditional hosting provider to Microsoft Azure. They have a traditional setup with ADDS, file servers and a RDP Session host. In their current setup they complain about performance, so i wanted to test out if we could run some kind of scaling in Azure based on time or usage.

A quick search on Google sent me towards a Technet script promising the features i was after. Let’s test it out.

You will need:

  • An active Azure Subscription
  • An ADDS setup
  • A RDP Broker
  • Minimum 2 RDP session servers

When you are done, you should end up with something like this:

1-300x165

I have configured a Fileserver to host the User Profile Disks, a server to host the Broker and web access RDP roles and two servers to host the session host roles.

Design

The script is executed as a scheduled task on the RDP Broker server.

2-300x179

It will turn on/off the Session Hosts, not delete them, so there will still be a very small cost for the VM’s when they are not being used. As we all know we do not pay for compute when a server is turned off (deallocated).

Implementation

I start with configuring the Connection Broker, as it will serve as the controller of operations.

Script setup

First we need to download and extract the Automatic Scaling script to c:DynamicRDSH or whatever folder you want.

Open the config.xml

3-300x174

Unfortunately, there is an error in the config.xml file, i have provided a fixed version here:

<?xml version="1.0" encoding="UTF-8"?>  
<RDSScale>  
  <Azure>
    <Variable Name="AzurePublishSettingFile" Value="INSERT PATH TO PUBLISH FILE"/>
    <Variable Name="CurrentAzureSubscriptionName" Value="INSERT SUBSCRIPTION"/>
    <Variable Name="CloudServiceName" Value="INSERT CLOUD SERVICE"/>
  </Azure>
  <RDSScaleSettings>
    <Variable Name="BeginPeakTime" Value="08:00:00"/>
    <Variable Name="EndPeakTime" Value="17:00:00"/>
    <Variable Name="TimeDifferenceInHours" Value="+1" />
    <Variable Name="SessionThresholdPerCPU" Value="3"/>
    <Variable Name="MinimumNumberOfRDSH" Value="1"/>
    <Variable Name="LimitSecondsToForceLogOffUser" Value="120"/>
    <Variable Name="LogOffMessageTitle" Value="System Under Maintenance"/>
    <Variable Name="LogOffMessageBody" Value="Please save your work and logoff!"/>
  </RDSScaleSettings>
</RDSScale>

Change the location of your AzurePublishSettingFile, Azure Subscription and the cloud service to the one your servers are running in.

Connecting to Azure

To install Azure Powershell simply use this Link. Unfortunately, the platform installer installs a bunch of unnecessary programs

4-300x190

Which, if you are like me, you uninstall.

5-300x188

Now we need to establish the connection to our Azure Subscription, thankfully, this is really simple. First you run

Get-AzurePublishSettingsFile

This will open up a browser and ask for your credentials, input these and when asked, save the file in your c:DynamicRDSH folder

6-300x180

Next import the PublishSettingsFile with

Import-AzurePublishSettingsFile -PublishSettingsFile

7-300x227

Now we will be able to run commands agains Azure from this server. When we are in Powershell we need to run these commands in order for the script to work.

Since the script is not signed we need to allow for all scripts to run.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

We also need to unblock the script.

Unblock-File .RDSScaler.ps1

Setup schedule

Next we need to setup the scheduled task that will probe the session host to see if they need scaling.

Create a new task and name it whatever you like and make sure you have selected Run Whether user is logged on or not and Run with highest privileges

8-300x226

Select Triggers and create one that repeats every 15 minutes for indefinitely. If you have a small or very large enviroment you could choose to lower this setting, anything lower than 15 minutes will not allow for Azure to complete its operations.

10-274x300

Select Actions and enter powershell.exe c:dynamicrdshrdsscaler.ps1

10-274x300

When done you should see something like this

11-300x159

Testing

I was a little curious so i ran the script to see what would happen

12-300x253

It detected that it was off-hours and noticed i had 2 sessions host running and no active sessions, so it turned off a session host.

RDSScale.log

10/13/2014 11:54:01 AM - [Info] Starting RDS Scale Optimization: Current Date Time is: 10/13/2014 11:54:01  
10/13/2014 11:54:07 AM - [Info] It is off-peak hours. Starting to scale down RD session hosts...  
10/13/2014 11:54:07 AM - [Info] Processing collection Fjernskrivebord  
10/13/2014 11:54:51 AM - [Info] Counting the current sessions on the host...  
10/13/2014 11:56:51 AM - [Info] Force users to log off...  
10/13/2014 11:58:10 AM - [Info] Waiting for Azure VM to stop <session host> ...  
10/13/2014 11:58:28 AM - [Info] End RDS Scale Optimization.  

RDSUsage.log

10/13/2014 12:06:31 PM, Fjernskrivebord, 0, 0  

And we can see a VM turned off in the Azure Portal

13

Conclusion

The script works as promissed and once the issues were fixed it was not that hard to get working. One thing i would like to see, is scaling down inside office hours, as it is now it will only scale down once its off-hours.

I hope you could use this guide, feel free to ask questions below.

One thought on “RDP Automatic scaling in Azure

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