3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

マイイメージからVirtual Machine Scale Setsを作成する

Posted at

イメージ化したVHDからVirtual Machine Scale Sets(VMSS)を作成するスクリプトになります。

準備

まずは下記のURLを参考に仮想マシンのイメージを作成します。
https://azure.microsoft.com/ja-jp/documentation/articles/virtual-machines-windows-capture-image/

スクリプト例

$subscriptionId = '[サブスクリプションID]'
$rgname = '[リソースグループ名]'
$storageAccount = '[ストレージアカウント名]'

$vnetname = '[仮想ネットワーク名]'
$subnetName = '[サブネット名]'

$publicIpName = '[パブリックIP名]'
$domainName = '[ドメイン名]'

# ログイン
Login-AzureRmAccount

# サブスクリプション 確定
Select-AzureRmSubscription -SubscriptionId $subscriptionId

# ストレージアカウントの取得
$stoaccount = Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $storageAccount;
if(-not $stoaccount) {  
    throw "ストレージアカウント '$storageAccount' が存在しません。"
}
$location = $stoaccount.Location

$VerbosePreference = 'Continue'
$ErrorActionPreference = 'Stop'

# 仮想ネットワーク作成
Write-Verbose 'Creating Virtual Network'
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix "10.0.0.0/24";

$vnet = New-AzureRmVirtualNetwork -Force -Name $vnetname -ResourceGroupName $rgname -Location $location -AddressPrefix "10.0.0.0/16" -DnsServer "10.1.1.1" -Subnet $subnet;
$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $rgname;
$subnetId = $vnet.Subnets[0].Id;

# パブリックIPの作成
Write-Verbose 'Creating Public IP'
$pubip = New-AzureRmPublicIpAddress -Force -Name $publicIpName -ResourceGroupName $rgname -Location $location -AllocationMethod Dynamic -DomainNameLabel $domainName;
$pubip = Get-AzureRmPublicIpAddress -Name $publicIpName -ResourceGroupName $rgname;

# Load Balancer の作成と構成
$frontendName = 'fe' + $rgname
$backendAddressPoolName = 'bepool' + $rgname
$probeName = 'vmssprobe' + $rgname
$inboundNatPoolName = 'innatpool' + $rgname
$lbruleName = 'lbrule' + $rgname
$lbName = 'vmsslb' + $rgname

# Public IPをLoad Balancerにバインド
$frontend = New-AzureRmLoadBalancerFrontendIpConfig -Name $frontendName -PublicIpAddress $pubip

$backendAddressPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name $backendAddressPoolName
$probe = New-AzureRmLoadBalancerProbeConfig -Name $probeName -Protocol tcp -Port 80 -IntervalInSeconds 15 -ProbeCount 2

# Load Balancer を経由して VMSS 内の VM に (負荷分散なしで) 直接ルーティングされた接続に関する NAT 規則を作成
$frontendpoolrangestart = 3360
$frontendpoolrangeend = 3370
$backendvmport = 3389
$inboundNatPool = New-AzureRmLoadBalancerInboundNatPoolConfig -Name $inboundNatPoolName -FrontendIPConfigurationId `
$frontend.Id -Protocol Tcp -FrontendPortRangeStart $frontendpoolrangestart -FrontendPortRangeEnd $frontendpoolrangeend -BackendPort $backendvmport;

# 負荷分散規則を作成
# http(80)で負荷分散する場合の例
$protocol = 'Tcp'
$feLBPort = 80
$beLBPort = 80

$lbrule = New-AzureRmLoadBalancerRuleConfig -Name $lbruleName `
-FrontendIPConfiguration $frontend -BackendAddressPool $backendAddressPool `
-Probe $probe -Protocol $protocol -FrontendPort $feLBPort -BackendPort $beLBPort `
-IdleTimeoutInMinutes 15 -LoadDistribution SourceIP -Verbose;

Write-Verbose 'Creating Load Balancer' 
$actualLb = New-AzureRmLoadBalancer -Name $lbName -ResourceGroupName $rgname -Location $location `
-FrontendIpConfiguration $frontend -BackendAddressPool $backendAddressPool `
-Probe $probe -LoadBalancingRule $lbrule -InboundNatPool $inboundNatPool -Verbose;

# VMSS の作成と構成
$vmssName = '[VMSS名]'

$adminUsername = '[管理者ユーザー名]';
$adminPassword = '[管理者パスワード]';
$vmNamePrefix = '[仮想マシン名Prefix]'
$vmssImage = '[VMSSに使用するイメージのURI https://xxx.blob.core.windows.net/system/Microsoft.Compute/Images/imagename-osDisk.vhd]'

# Load Balancer とサブネットへの NIC のバインド
$ipCfg = New-AzureRmVmssIPConfig -Name 'nic' -LoadBalancerInboundNatPoolsId $actualLb.InboundNatPools[0].Id `
-LoadBalancerBackendAddressPoolsId $actualLb.BackendAddressPools[0].Id -SubnetId $subnetId;

$ipCfg.LoadBalancerBackendAddressPools.Add($actualLb.BackendAddressPools[0].Id)
$ipCfg.LoadBalancerInboundNatPools.Add($actualLb.InboundNatPools[0].Id)

# VMSS 構成の作成
$numberofnodes = 2
$vmss = New-AzureRmVmssConfig -Location $location -SkuCapacity $numberofnodes -SkuName 'Standard_A1' -UpgradePolicyMode 'Manual' `
    | Add-AzureRmVmssNetworkInterfaceConfiguration -Name $subnetName -Primary $true -IPConfiguration $ipCfg `
    | Set-AzureRmVmssOSProfile -ComputerNamePrefix $vmNamePrefix -AdminUsername $adminUsername -AdminPassword $adminPassword `
    | Set-AzureRmVmssStorageProfile -Name 'test' -OsDiskCreateOption 'FromImage' -OsDiskCaching 'None' -Image $vmssImage -OsDiskOsType 'Windows'

Write-Verbose 'Creating Virtual Machine Scale Sets' 
New-AzureRmVmss -ResourceGroupName $rgname -Name $vmssName -VirtualMachineScaleSet $vmss -Verbose

構築のポイント

Set-AzureRmVmssStorageProfileに対し、以下のプロパティを設定します。

  • OsDiskCreateOptionプロパティを"FromImage"に設定
  • Imageプロパティに基となるVHDが格納されているストレージURIを設定
  • OsDiskOsTypeプロパティに"Windows" or "Linux"を設定
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?