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

概要

以下のような構成をデプロイします。
VNet [ Subnet [ 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
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
-
-
|
!
 
-
!
-
-
!
-
|
-
-
|
-
|
!
!
!
-
|
-
|
!
!
 
-
|
!
 
 
 
 
-
|
!
 
 
 
 
-
!
 
-
-
|
|
|
!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
!
-
!
-
!
|
-
|
!
|
|
!
 
#///////////////////////////////////////////////////////////
# 
# 以下の構成をデプロイ
#   VNet [ Subnet [ 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_vmv1.ps1
#   サブスクリプション, 仮想ネットワーク, サブネット, カレントストレージ, VM名, RDPポートを引数経由で指定してデプロイ
#     ./deploy_vmv1.ps1 -SubscriptionName "無料試用版" -VNetName "vnet1" -SubnetName "Subnet-1" -CurrentStorageName "storage1" -VMNames "vm1","vm2" -RDPPorts 80,443
# 
#///////////////////////////////////////////////////////////
 
[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]$InstanceSize = "Small",
    [Parameter(Mandatory=$False)]
        [string[]]$VMNames = @("vm1", "vm2"),
    [Parameter(Mandatory=$False, HelpMessage="Duplication is not permitted.")]
        [int32[]]$RDPPorts = @(55200, 55201),
    [Parameter(Mandatory=$False, HelpMessage="New or existing either.")]
        [string]$AvailabilitySetName = "as" + (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 `
        -InstanceSize $InstanceSize `
        -SubnetNames $SubnetName `
        -VNetName $VNetName `
        -AvailabilitySetName $AvailabilitySetName `
        -NoWinRMEndpoint `
        -WaitForBoot `
        -ErrorAction Stop
    Log("New-AzureQuickVM done($($VMNames[$i]))")
    
    # NAT設定
    # 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 <ポート番号>
    Add-AzureEndpoint -VM $VM -Name "RemoteDesktop" -Protocol "tcp" -PublicPort $RDPPorts[$i] -LocalPort 3389 -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
-
!
 
-
|
!
-
|
!
# ログイン(ASM)
Add-AzureAccount
 
# vnet1, Subnet-1, storage20160621000225(要ユニーク)を事前に用意(サンプルスクリプトのヘッダを参照)
# ・・・
 
# デプロイ
# 足りない設定を対話式で入力後、確認画面を経て実行
./deploy_vmv1.ps1 -SubscriptionName "無料試用版" -VNetName "vnet1" -SubnetName "Subnet-1" -CurrentStorageName "storage20160621000225" -CloudServiceName "sc20160621000225"

以上で、CloudServicesの中に可用性セットで囲まれた2つのVMがデプロイされました。
今回作ったCloudServicesはPublic endpointを持つため、外部から"sc20160621000225.cloudapp.net"でアクセスできます。
RDPClientでsc20160621000225.cloudapp.net:55200にアクセスすると、vm1に。
sc20160621000225.cloudapp.net:55201にアクセスすると、vm2にログインできます。

02.png

検証時の環境