Last-modified: 2016-06-24 (金) 01:13:28
Azure/Internal load balancer - VM構成をデプロイ(ASM)

概要

以下のような構成をデプロイします。
VNet [ Subnet [ InternalLoadBalancer - AvailabilitySet/CloudServices( VM1, ...VMn ) ] ]

01.png

サンプルスクリプト

Everything is expanded.Everything is shortened.
  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
-
-
|
!
 
-
!
-
-
!
-
|
-
-
|
-
|
!
!
!
-
|
-
|
!
!
 
-
|
!
 
 
 
 
-
|
!
 
 
 
 
-
!
 
-
-
|
|
|
!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
-
|
|
|
|
|
!
|
-
!
-
!
|
-
|
!
|
|
|
|
|
|
|
|
|
|
|
-
|
!
|
|
!
 
#///////////////////////////////////////////////////////////
# 
# 以下の構成をデプロイ
#   VNet [ Subnet [ InternalLoadBalancer - AvailabilitySet/CloudServices( VM1, ...VMn ) ] ]
#   全てASM(v1)リソース
# 
# 前提条件
#   デプロイ先の有効なサブスクリプション, カレントストレージ, 仮想ネットワーク, サブネットが必要
#   ASMへのログイン済みであること(Add-AzureAccount)
#
# 補足
#   引数は、Get-Helpで確認可能
#   引数指定では、対話モードで設定できない項目の変更も可能
#   全体を通してエラー処理を考慮していないので注意
#   
#   カレントストレージは、ポータルないし、以下のコマンドで構築
#     New-AzureStorageAccount -StorageAccountName <アカウント名> -Location <ロケーション名(e.g, Japan East)> -Type <タイプ名(e.g, Standard_LRS)>
#   仮想ネットワークは、ポータルないし、XML経由で構築(クソ仕様)
#     Get-AzureVNetConfig -ExportToFile <エクスポートパス>
#     Set-AzureVNetConfig -ConfigurationPath <インポートパス>
#
# 例
#   パラメータを対話形式で設定してデプロイ
#     ./deploy_ilb_vmv1.ps1
#   サブスクリプション, 仮想ネットワーク, サブネット, カレントストレージ, VM名を引数経由で指定してデプロイ
#     ./deploy_ilb_vmv1.ps1 -SubscriptionName "無料試用版" -VNetName "vnet1" -SubnetName "Subnet-1" -CurrentStorageName "storage1" -VMNames "vm1","vm2"
# 
#///////////////////////////////////////////////////////////
 
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
        [string]$SubscriptionName,
    [Parameter(Mandatory=$True, HelpMessage="It must be pre-built.")]
        [string]$VNetName,
    [Parameter(Mandatory=$True, HelpMessage="It must be pre-built.")]
        [string]$SubnetName,
    [Parameter(Mandatory=$True, HelpMessage="It must be pre-built.")]
        [string]$CurrentStorageName,
    [Parameter(Mandatory=$True, HelpMessage="New or existing either.")]
        [string]$CloudServiceName,
    [Parameter(Mandatory=$True, HelpMessage="Cloud services location(e.g, Japan East, West Japan, East Asia, West US, North Europe, ...)")]
        [string]$Location,
    [Parameter(Mandatory=$True)]
        [string]$AdminUserName,
    [Parameter(Mandatory=$True)]
        [string]$AdminPassword,
    [Parameter(Mandatory=$False)]
        [boolean]$AlwaysYes = $False,
    [Parameter(Mandatory=$False)]
        [string]$VMImage = @(Get-AzureVMImage | Where-Object -Property Label -Match "Windows Server 2012 R2 Datacenter.* 2016").ImageName[-1],
    [Parameter(Mandatory=$False)]
        [string[]]$VMNames = @("vm1", "vm2"),
    [Parameter(Mandatory=$False, HelpMessage="It must be not exist.")]
        [string]$AvailabilitySetName = "as" + (Get-Date -Format "yyyyMMddHHmmssfff").ToString(),
    [Parameter(Mandatory=$False)]
        [string]$EndPointName = "http",
    [Parameter(Mandatory=$False)]
        [int32]$EndPointPublicPort = 80,
    [Parameter(Mandatory=$False)]
        [int32]$EndPointLocalPort = 80,
    [Parameter(Mandatory=$False)]
        [string]$LoadBalancerName = "lb" + (Get-Date -Format "yyyyMMddHHmmssfff").ToString(),
    [Parameter(Mandatory=$False)]
        [string]$InternalLoadBalancerName = "ilb" + (Get-Date -Format "yyyyMMddHHmmssfff").ToString()
)
 
#///////////////////////////////////////////////////////////
 
# ログ出力
function Log($text) {
    return (Get-Date -Format "yyyy/MM/dd HH:mm:ss").ToString() + " $($text)"
}
 
#///////////////////////////////////////////////////////////
 
# 確認
"< Deploy on Azure >"
"Deploy with the following settings."
($MyInvocation.MyCommand.Parameters ).Keys | %{
    $val = (Get-Variable -Name $_ -EA SilentlyContinue).Value
    if ($val.length -gt 0) {
        if ($_ -match "password") {
            "  $($_) = ***"
        } else {
            "  $($_) = $($val)"
        }
    }
}
if (!$AlwaysYes) {
    $in = Read-Host "Is it OK?(Y/N) [default Y] "
    if (($in -ine "Y") -and ($in -ine "")) {
        Exit
    }
}
 
# サブスクリプションを指定
# Select-AzureSubscription -SubscriptionName <サブスクリプション名> -Current
Select-AzureSubscription `
    -SubscriptionName $SubscriptionName `
    -Current `
    -ErrorAction Stop
 
# カレントストレージを設定
# Set-AzureSubscription <サブスクリプション名> -CurrentStorageAccount <ストレージアカウント名>
Set-AzureSubscription `
    -SubscriptionName $SubscriptionName `
    -CurrentStorageAccount $CurrentStorageName `
    -ErrorAction Stop
 
# 仮想マシン(v1)を構成
Log("VM(v1) creation start...")
$VMs = @()
for ($i = 0; $i -lt $VMNames.Length; ++$i) {
    # VM作成
    # New-AzureQuickVM -ImageName <イメージ名> -ServiceName <CloudService名> -Name <VM名> -Windows `
    # 	-AdminUsername <管理者ユーザ名> -Password <パスワード> -Location <ロケーション名> `
    # 	-SubnetNames <サブネット名> -VNetName <VNet名> -AvailabilitySetName <可用性セット名>
    New-AzureQuickVM `
        -ImageName $VMImage `
        -ServiceName $CloudServiceName `
        -Name $VMNames[$i] `
        -Windows `
        -AdminUsername $AdminUserName `
        -Password $AdminPassword `
        -Location $Location `
        -SubnetNames $SubnetName `
        -VNetName $VNetName `
        -AvailabilitySetName $AvailabilitySetName `
        -NoWinRMEndpoint `
        -WaitForBoot `
        -ErrorAction Stop
    Log("New-AzureQuickVM done($($VMNames[$i]))")
 
    # 内部ロードバランサーがなければ作成
    if (!((Get-AzureInternalLoadBalancer -ServiceName $CloudServiceName -ErrorAction Stop).InternalLoadBalancerName -match $InternalLoadBalancerName)) {
        Add-AzureInternalLoadBalancer `
            -InternalLoadBalancerName $InternalLoadBalancerName `
            -ServiceName $CloudServiceName `
            -SubnetName $SubnetName `
            -ErrorAction Stop
    }
 
    # Get-AzureVM -ServiceName <CloudServcie名> -Name <VM名>
    $VM = Get-AzureVM -ServiceName $CloudServiceName -Name $VMNames[$i] -ErrorAction Stop
    # Remove-AzureEndpoint -VM <VM object> -Name <エンドポイント名>
    Remove-AzureEndpoint -VM $VM -Name "RemoteDesktop" -ErrorAction Stop
 
    # Add-AzureEndpoint -VM <VM object> -Name <エンドポイント名> -Protocol <プロトコル> -PublicPort <ポート番号> -LocalPort <ポート番号> `
    # 	-LBSetName <LB名> -ProbePort <ポート番号> -ProbeProtocol <HTTP/TCP> -InternalLoadBalancerName <ILB名>
    Add-AzureEndpoint `
        -VM $VM `
        -Name $EndPointName `
        -Protocol "tcp" `
        -PublicPort $EndPointPublicPort `
        -LocalPort $EndPointLocalPort `
        -LBSetName $LoadBalancerName `
        -ProbePort $EndPointLocalPort `
        -ProbeProtocol "tcp" `
        -InternalLoadBalancerName $InternalLoadBalancerName `
        -ErrorAction Stop
    
    # 更新
    # -VMオプションあるくせに動かないクソ
    $VM | Update-AzureVM -ErrorAction Stop
 
    $VMs += $VM
}
Log("...VM(v1) criation end")

サンプル実行

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
-
!
 
-
|
!
-
!
 
-
|
!
 
-
|
|
|
|
!
-
|
|
# ログイン(ASM)
Add-AzureAccount
 
# vnet1, Subnet-1, Subnet-2, storage20160621000225(要ユニーク)を事前に用意(サンプルスクリプトのヘッダを参照)
# ・・・
 
# IIS設定&疎通確認用VMのデプロイ(参考 Azure/Load balancer - VM構成をデプロイ(ASM) を参照)
./deploy_vmv1.ps1 -SubscriptionName "無料試用版" -VNetName "vnet1" -SubnetName "Subnet-1" -CurrentStorageName "storage20160621000225" -CloudServiceName "sc20160621000225" -VMNames "vm0" -RDPPort 52200
 
# 本筋のデプロイ(別Cloud Services)
# 足りない設定を対話式で入力後、確認画面を経て実行
./deploy_ilb_vmv1.ps1 -SubscriptionName "無料試用版" -VNetName "vnet1" -SubnetName "Subnet-2" -CurrentStorageName "storage20160621000225" -CloudServiceName "sc20160621005430"
 
# 外部からRDPでsc20160621000225.cloudapp.net:55200にログインし、そこからvm1, vm2へRDPでログイン
# vm1, vm2のIPは、Get-AzureVM -ServiceName "sc20160621005430"で確認
# ServerManager - [Add roles and features] - ・・・ - [Server Roles] - [WebServer(IIS)] - ・・・ - [Install]で、vm1, vm2にIISをインストール
# Internal load balancerのIPは、Get-AzureVM -ServiceName "sc20160621005430" | Get-AzureEndpointで確認
# vm0でIEを立ち上げ、http://<Internal load balancerのIP>で、IISの初期ページが出れば成功
 
# 負荷分散できているかは、vm1,vm2のIISを止めたり、つけたりすればわかる(試したときはProbeIntervalInSeconds=15秒だったので、切り替えまでに15秒程度待つ必要がある)
# Get-AzureVM -ServiceName "sc20160621005430" | Get-AzureEndpointに表示されるProbe*パラメータが、backendを決める指標になる
# Probe*パラメータは、Add-AzureEndpointのタイミングで指定可能

以上で、Internal load balancer以下にVM(ASM)を閉じ込められました。
サンプルとしてIISを手動で設定しましたが、実際は利用するサービスを自動で構成しないと、使い物にならないでしょう。

02.png

補足

検証時の環境

参考