Windows Virtual Desktop (classic) #3 Auto Scaling編

この記事のWVDは”Windows Virtual Desktop Fall 2019 Release”が対象です。最新の”Spring 2020 Relase”ではありません。Spring 2020 はこちら

Azure上で仮想デスクトップを提供する”Windows Virtual Desktop”について紹介します。  前回の2つの記事で紹介出来ていなかった”Auto Scaling”について確認した内容をまとめました。 ”Auto Scaling”を構成することで仮想デスクトップを利用しない夜間などにAzure VMを停止することでコストを最適化することが可能になります。

1. 公式ドキュメント

”Auto Scaling”について公式ドキュメントに構成方法や利用イメージが案内されています。

URLはこちら

ドキュメントの冒頭に”Auto Scaling”を利用する背景と実現方式について記載があります。 重要なことは”session hosts”で利用可能ということになります。いわゆるRDSHとなります。 最初、Windows 10のデスクトップは未対応でServer OSのRDSHのみではあまり意味が無い(コスト最適化が難しい)と思ったのですが、WVDではWindows 10のマルチセッションに対応してます。結果Windows 10のデスクトップでも”Auto Scaling”を実現出来るのだと思います(テスト出来ていないので推測)

その他記載内容としてはWVDで必要となるコストはAzure上のVMリソースのコストとなるため、利用しない時間帯にVMの起動数を調整することでコストを最適化できると。そのために”Auto Scaling”しましょうということです。 

なお、”Auto Scaling”の実現方式ですがスクリプトベースになります。 CitrixやVMwareではGUIからも可能なので将来の機能拡充に期待ですね。

Automatically scale session hosts

For many Windows Virtual Desktop Preview deployments in Azure, the virtual machine costs represent significant portion of the total Windows Virtual Desktop deployment cost. To reduce costs, it’s best to shut down and deallocate session host virtual machines (VMs) during off-peak usage hours, then restart them during peak usage hours.


This article uses a simple scaling script to automatically scale session host virtual machines in your Windows Virtual Desktop environment. To learn more about how the scaling script works, see the How the scaling script works section.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

2. 前提条件

前提として5つの条件があります。1,2は当たり前ですがWVDのテナントとhost poolsを事前に作成しておくこと、3はタスクスケジューラを利用すること(OS標準でもその他ジョブ管理システムでもよいかと)、4,5はスクリプトで利用するARMとWVDのPowerShellモジュールを事前にインストールしておきましょう、ということになります。

  1. 適切な権限を付与したWVDのテナントの作成
  2. Session host poolsの作成
  3. タスクスケジューラの利用
  4. Azure Resource ManagerのPowerShell モジュール
  5. WVDのPowerShellモジュール

・A Windows Virtual Desktop tenant and account or a service principal with permissions to query that tenant (such as RDS Contributor).
・Session host pool VMs configured and registered with the Windows Virtual Desktop service.
・An additional scaler VM that runs the scheduled task via Task Schedule and that has network access to session hosts.
・The Microsoft Azure Resource Manager PowerShell module installed on the VM running the scheduled task.
・The Windows Virtual Desktop PowerShell module installed on the VM running the scheduled task.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

3. スクリプトで制御できる項目

WVDのPowerShellモジュールでは別途”config.xml”ファイルに記載した構成情報を読み取り、その内容で”Auto Scaling”します。

No.10-11でピーク時間の開始と終了時刻を指定可能です。 曜日に関する項目は見当たらないので、スクリプトを2つ準備し平日実行用と休日実行用とでスケジュールを分けて実行することになりそうです

No.13の”SessionThresholdPerCPU”を利用することで性能閾値による制御も可能とのこと。

No.14の”MinimumNumberOfRDSH”ではオフピーク時の最低起動数を指定可能です。

No.15-17は利用者へのサインアウトの強制やその待機時間の指定、利用者側に表示するメッセージのタイトルと本文を指定できます。この機能はScale in(スケールイン)の時に利用する感じでしょうかね。

No.FieldDescription
1AADTenantIdAzure AD Tenant ID that associates the subscription where the session host VMs run
セッションホストVMが実行されているサブスクリプションを関連付けるAzure ADテナントID
2AADApplicationIdService principal application ID
サービスプリンシパルアプリケーションID
3AADServicePrincipalSecretThis can be entered during the testing phase but is to be kept empty once you create credentials with Functions-PSStoredCredentials.ps1
これはテスト段階で入力できますが、Functions-PSStoredCredentials.ps1を使用して資格情報を作成すると空のままになります
4currentAzureSubscriptionIdThe ID of the Azure subscription where the session host VMs run
セッションホストVMが実行されているAzureサブスクリプションのID
5tenantNameWindows Virtual Desktop tenant name
WVDのテナント名
6hostPoolNameWindows Virtual Desktop host pool name
WVDのhost pool名
7RDBrokerURL to the WVD service, default value https://rdbroker.wvd.microsoft.com
WVDのURL
8UsernameThe service principal application ID (it’s possible to have the same service principal as in AADApplicationId) or standard user without multi-factor authentication
サービスプリンシパルアプリケーションID(AADApplicationIdと同じサービスプリンシパルを持つことも可能)または多要素認証なしの標準ユーザー
9isServicePrincipalAccepted values are true or false. Indicates whether the second set of credentials being used is a service principal or a standard account.
許容値はtrueまたはfalseです。使用されている2番目の資格情報がサービスプリンシパルか標準アカウントかを示します。
10BeginPeakTimeWhen peak usage time begins
ピーク時間帯の開始時刻
11EndPeakTimeWhen peak usage time ends
ピーク時間帯の終了時刻
12TimeDifferenceInHoursTime difference between local time and UTC, in hours
現地時間とUTCの時差
13SessionThresholdPerCPUMaximum number of sessions per CPU threshold used to determine when a new RDSH server needs to be started during peak hours.
ピーク時に新しいRDSHサーバーをいつ起動する必要があるかを判断するために使用される、CPUあたりの最大セッション数しきい値。
14MinimumNumberOfRDSHMinimum number of host pool VMs to keep running during off-peak usage time
オフピーク時に稼働し続けるホストプールVMの最小数
15LimitSecondsToForceLogOffUserNumber of seconds to wait before forcing users to sign out. If set to 0, users aren’t forced to sign out.
ユーザーにサインアウトを強制する前に待機する秒数。0に設定した場合、ユーザーはサインアウトを強制されません。
16LogOffMessageTitleTitle of the message sent to a user before they’re forced to sign out
サインアウトを強制される前にユーザーに送信されたメッセージのタイトル
17LogOffMessageBodyBody of the warning message sent to users before they’re signed out. For example, “This machine will shut down in X minutes. Please save your work and sign out.”
サインアウトする前にユーザーに送信される警告メッセージの本文。たとえば、「このコンピュータはX分でシャットダウンします。作業内容を保存してサインアウトしてください。」

4. 動作イメージ

公式ドキュメントにはWVDのPowerShellモジュールを利用した”Auto Scaling”について具体的な動作イメージが紹介されています。 欲を言えば、動作仕様の詳細について網羅的なドキュメントが欲しいところです。 正式リリース時には公開されることを期待します。

ピーク時間帯の動作としては以下の通りです。

  1. スクリプトは各コレクションについて現在のセッション数と現在実行中のRDSHのキャパシティをチェック
  2. config.xmlのSessionThresholdPerCPUパラメーターに基づいて、実行中のRDSHサーバーに既存のセッションをサポートするのに十分な容量があるかどうかを計算
  3. 不足している場合はRDSHサーバーを追加

During peak usage time, the script checks the current number of sessions and the current running RDSH capacity for each collection. It calculates if the running RDSH servers have enough capacity to support existing sessions based on the SessionThresholdPerCPU parameter defined in the config.xml file. If not, the script starts additional RDSH servers in the collection.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

オフピーク時の動作は以下の通りです。

  1. config.xmlファイルのMinimumNumberOfRDSHパラメータに基づいて、どのRDSHサーバーをシャットダウンするか決定
    • MinimumNumberOfRDSHでは最小起動数を指定するものと考えていたのですが、”どのRDSHサーバー”とするのか、判別アルゴリズムの確認が必要そうです
  2. 新しいセッションがホストに接続するのを防ぐために、RDSHサーバーをドレインモードに設定
  3. LimitSecondsToForceLogOffUserの値に応じて、ユーザ通知や強制サインアウトを実行
  4. すべてのユーザがサインアウトした後にRDSHサーバーをシャットダウン

オフピーク時には、config.xmlファイルのMinimumNumberOfRDSHパラメータに基づいて、どのRDSHサーバをシャットダウンするかが決定されます。このスクリプトは、新しいセッションがホストに接続するのを防ぐために、RDSHサーバーをドレインモードに設定します。あなたが設定した場合LimitSecondsToForceLogOffUserのゼロ以外の正の値にconfig.xmlファイル内のパラメータを、スクリプトは現在、作業を保存設定時間を待ってからサインアウトするようにユーザーを強制的にユーザーに署名を通知します。すべてのユーザーセッションがRDSHサーバーでサインオフされると、スクリプトはサーバーをシャットダウンします。

During the off-peak usage time, the script determines which RDSH servers should shut down based on the MinimumNumberOfRDSH parameter in the config.xml file. The script will set the RDSH servers to drain mode to prevent new sessions connecting to the hosts. If you set the LimitSecondsToForceLogOffUser parameter in the config.xml file to a non-zero positive value, the script will notify any currently signed in users to save work, wait the configured amount of time, and then force the users to sign out. Once all user sessions have been signed off on an RDSH server, the script will shut down the server.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

LimitSecondsToForceLogOffUserを”0”にしている場合は強制サインアウトは行われません。 ユーザがすべてサインアウトするまでRDSHサーバは起動し続けます。 

実運用を考えると利用者の利便性重視であれば”0”がよいでしょう。ただし、Scale inが発生しないためコスト最適化を実現できません。 1ユーザ1VMのデスクトップタイプであればこの運用も良いのでしょうが、RDSHでScale out/inでコスト最適化を優先したい場合には”0”設定は難しいと考えます。

If you set the LimitSecondsToForceLogOffUser parameter in the config.xml file to zero, the script will allow the session configuration setting in the collection properties to handle signing off user sessions. If there are any sessions on an RDSH server, it will leave the RDSH server running. If there aren’t any sessions, the script will shut down the RDSH server.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

スクリプトの実行感覚は環境依存はあるけれども15分間隔をお勧めします、とのこと。

The script is designed to run periodically on the scaler VM server using Task Scheduler. Select the appropriate time interval based on the size of your Remote Desktop Services environment, and remember that starting and shutting down virtual machines can take some time. We recommend running the scaling script every 15 minutes.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

5. ログ

スクリプトでは2つのログファイルを出力するとのこと。 コスト評価のために役立つVMs数をCSVで残せるとのこと。 セッション数は無さそうなのでこれは別途違う方法で取得することになりそうです。

  1. ”WVDTenantScale.log”
    • イベントおよびエラーのログ出力
  2. ”WVDTenantUsage.log”
    • 稼働中のVM数とコア数
    • Azure VMの実行数とコストの評価に利用可能
    • カンマ区切り:time,collection,cores,VMs
    • csv形式でExcelなどで分析可能

Log files
The scaling script creates two log files, WVDTenantScale.log and WVDTenantUsage.log. The WVDTenantScale.log file will log the events and errors (if any) during each execution of the scaling script.
The WVDTenantUsage.log file will record the active number of cores and active number of virtual machines each time you execute the scaling script. You can use this information to estimate the actual usage of Microsoft Azure VMs and the cost. The file is formatted as comma-separated values, with each item containing the following information:
 time, collection, cores, VMs
The file name can also be modified to have a .csv extension, loaded into Microsoft Excel, and analyzed.

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/set-up-scaling-script

6. まとめ

一番のポイントはSession hostにしか対応しないことでしょうか。Windows 10マルチセッションかServer OSによるRDSHでの利用が前提となるようです。 WVDでしか利用を許諾しないWindows 10 マルチセッションの利用を促進させるために意図的にRDSHに限定しているのかなと邪推。まあ、Public previewの現状なので正式公開時には1ユーザ1VMのデスクトップタイプにも対応するのだと勝手に考えています。

スクリプトしか対応しないことについても今後のGUI対応を期待します。今回のAuto Scaling以外の設定でもPowerShellでしか設定出来ない項目は他にも色々あることも含めてです。 インフラをコードで制御することが一般化しつつある現状、PowerShell対応は歓迎します。 しかし、エンドユーザとしては運用管理の負荷を軽減したいためにDaaSに魅力を感じるはずで、PC管理者にInfrastructure as Codeを前提とすることは新たな運用管理の負荷をかけることになると思います。 これもPublic previewなので正式版を待ちたいと思いました。