クイック スタート:Azure PowerShell を使用して VM の負荷を分散するパブリック ロード バランサーを作成するQuickstart: Create a public load balancer to load balance VMs using Azure PowerShell
Azure PowerShell を使用してパブリック ロード バランサーと 3 つの仮想マシンを作成することにより、Azure Load Balancer の使用を開始します。Get started with Azure Load Balancer by using Azure PowerShell to create a public load balancer and three virtual machines.
前提条件Prerequisites
- アクティブなサブスクリプションが含まれる Azure アカウント。An Azure account with an active subscription. 無料でアカウントを作成できます。Create an account for free.
- ローカルにインストールされた Azure PowerShell または Azure Cloud ShellAzure PowerShell installed locally or Azure Cloud Shell
PowerShell をインストールしてローカルで使用する場合、この記事では Azure PowerShell モジュール バージョン 5.4.1 以降が必要になります。If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 5.4.1 or later. インストールされているバージョンを確認するには、Get-Module -ListAvailable Az
を実行します。Run Get-Module -ListAvailable Az
to find the installed version. アップグレードする必要がある場合は、Azure PowerShell モジュールのインストールに関するページを参照してください。If you need to upgrade, see Install Azure PowerShell module. PowerShell をローカルで実行している場合、Connect-AzAccount
を実行して Azure との接続を作成することも必要です。If you're running PowerShell locally, you also need to run Connect-AzAccount
to create a connection with Azure.
リソース グループを作成するCreate a resource group
Azure リソース グループとは、Azure リソースのデプロイと管理に使用する論理コンテナーです。An Azure resource group is a logical container into which Azure resources are deployed and managed.
New-AzResourceGroup を使用して、次のようにリソース グループを作成します。Create a resource group with New-AzResourceGroup:
New-AzResourceGroup -Name 'CreatePubLBQS-rg' -Location 'eastus'
注意
運用環境のワークロードには、Standard SKU ロード バランサーをお勧めします。Standard SKU load balancer is recommended for production workloads. SKU の詳細については、「 Azure Load Balancer の SKU 」を参照してください。For more information about skus, see Azure Load Balancer SKUs.
パブリック IP アドレスを作成する - StandardCreate a public IP address - Standard
パブリック IP アドレスを作成するには、New-AzPublicIpAddress を使用します。Use New-AzPublicIpAddress to create a public IP address.
$publicip = @{
Name = 'myPublicIP'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
Sku = 'Standard'
AllocationMethod = 'static'
Zone = 1,2,3
}
New-AzPublicIpAddress @publicip
ゾーンのパブリック IP アドレスをゾーン 1 に作成するには、次のコマンドを使用します。To create a zonal public IP address in zone 1, use the following command:
$publicip = @{
Name = 'myPublicIP'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
Sku = 'Standard'
AllocationMethod = 'static'
Zone = 1
}
New-AzPublicIpAddress @publicip
Standard ロード バランサーを作成するCreate standard load balancer
このセクションでは、ロード バランサーの以下のコンポーネントを作成および構成する方法について説明します。This section details how you can create and configure the following components of the load balancer:
フロントエンド IP プールに対し、New-AzLoadBalancerFrontendIpConfig を使用して、次のようにフロントエンド IP を作成します。Create a front-end IP with New-AzLoadBalancerFrontendIpConfig for the frontend IP pool. ロード バランサーでは、この IP で受信トラフィックを受け取ります。This IP receives the incoming traffic on the load balancer
ロード バランサーのフロントエンドから送信されるトラフィック用に、New-AzLoadBalancerBackendAddressPoolConfig でバックエンド アドレス プールを作成します。Create a back-end address pool with New-AzLoadBalancerBackendAddressPoolConfig for traffic sent from the frontend of the load balancer. このプールが、バックエンド仮想マシンのデプロイ先となります。This pool is where your backend virtual machines are deployed.
バックエンド VM インスタンスの正常性を調べる正常性プローブを Add-AzLoadBalancerProbeConfig で作成します。Create a health probe with Add-AzLoadBalancerProbeConfig that determines the health of the backend VM instances.
複数の VM に対してどのようにトラフィックを分散させるかを定義するロード バランサー規則を Add-AzLoadBalancerRuleConfig で作成します。Create a load balancer rule with Add-AzLoadBalancerRuleConfig that defines how traffic is distributed to the VMs.
New-AzLoadBalancer を使用して、次のようにパブリック ロード バランサーを作成します。Create a public load balancer with New-AzLoadBalancer.
## Place public IP created in previous steps into variable. ##
$publicIp = Get-AzPublicIpAddress -Name 'myPublicIP' -ResourceGroupName 'CreatePubLBQS-rg'
## Create load balancer frontend configuration and place in variable. ##
$feip = New-AzLoadBalancerFrontendIpConfig -Name 'myFrontEnd' -PublicIpAddress $publicIp
## Create backend address pool configuration and place in variable. ##
$bepool = New-AzLoadBalancerBackendAddressPoolConfig -Name 'myBackEndPool'
## Create the health probe and place in variable. ##
$probe = @{
Name = 'myHealthProbe'
Protocol = 'http'
Port = '80'
IntervalInSeconds = '360'
ProbeCount = '5'
RequestPath = '/'
}
$healthprobe = New-AzLoadBalancerProbeConfig @probe
## Create the load balancer rule and place in variable. ##
$lbrule = @{
Name = 'myHTTPRule'
Protocol = 'tcp'
FrontendPort = '80'
BackendPort = '80'
IdleTimeoutInMinutes = '15'
FrontendIpConfiguration = $feip
BackendAddressPool = $bePool
}
$rule = New-AzLoadBalancerRuleConfig @lbrule -EnableTcpReset -DisableOutboundSNAT
## Create the load balancer resource. ##
$loadbalancer = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Name = 'myLoadBalancer'
Location = 'eastus'
Sku = 'Standard'
FrontendIpConfiguration = $feip
BackendAddressPool = $bePool
LoadBalancingRule = $rule
Probe = $healthprobe
}
New-AzLoadBalancer @loadbalancer
仮想ネットワークを構成する - StandardConfigure virtual network - Standard
VM をデプロイしてロード バランサーをテストする前に、サポート用の仮想ネットワーク リソースを作成します。Before you deploy VMs and test your load balancer, create the supporting virtual network resources.
バックエンド仮想マシン用の仮想ネットワーク カードを作成します。Create a virtual network for the backend virtual machines.
ネットワーク セキュリティ グループを作成して、仮想ネットワークへの受信接続を定義します。Create a network security group to define inbound connections to your virtual network.
仮想ネットワーク、ネットワーク セキュリティ グループ、bastion ホストを作成するCreate virtual network, network security group, and bastion host
New-AzVirtualNetwork を使用して仮想ネットワークを作成します。Create a virtual network with New-AzVirtualNetwork.
New-AzNetworkSecurityRuleConfig を使用して、ネットワーク セキュリティ グループの規則を作成します。Create a network security group rule with New-AzNetworkSecurityRuleConfig.
New-AzBastion を使用して Azure Bastion ホストを作成します。Create an Azure Bastion host with New-AzBastion.
New-AzNetworkSecurityGroup を使用して、ネットワーク セキュリティ グループを作成します。Create a network security group with New-AzNetworkSecurityGroup.
## Create backend subnet config ##
$subnet = @{
Name = 'myBackendSubnet'
AddressPrefix = '10.1.0.0/24'
}
$subnetConfig = New-AzVirtualNetworkSubnetConfig @subnet
## Create Azure Bastion subnet. ##
$bastsubnet = @{
Name = 'AzureBastionSubnet'
AddressPrefix = '10.1.1.0/24'
}
$bastsubnetConfig = New-AzVirtualNetworkSubnetConfig @bastsubnet
## Create the virtual network ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
AddressPrefix = '10.1.0.0/16'
Subnet = $subnetConfig,$bastsubnetConfig
}
$vnet = New-AzVirtualNetwork @net
## Create public IP address for bastion host. ##
$ip = @{
Name = 'myBastionIP'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
Sku = 'Standard'
AllocationMethod = 'Static'
}
$publicip = New-AzPublicIpAddress @ip
## Create bastion host ##
$bastion = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Name = 'myBastion'
PublicIpAddress = $publicip
VirtualNetwork = $vnet
}
New-AzBastion @bastion -AsJob
## Create rule for network security group and place in variable. ##
$nsgrule = @{
Name = 'myNSGRuleHTTP'
Description = 'Allow HTTP'
Protocol = '*'
SourcePortRange = '*'
DestinationPortRange = '80'
SourceAddressPrefix = 'Internet'
DestinationAddressPrefix = '*'
Access = 'Allow'
Priority = '2000'
Direction = 'Inbound'
}
$rule1 = New-AzNetworkSecurityRuleConfig @nsgrule
## Create network security group ##
$nsg = @{
Name = 'myNSG'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
SecurityRules = $rule1
}
New-AzNetworkSecurityGroup @nsg
仮想マシンを作成する - StandardCreate virtual machines - Standard
このセクションでは、ロード バランサーのバックエンド プール用に 3 つの仮想マシンを作成します。In this section, you'll create the three virtual machines for the backend pool of the load balancer.
New-AzNetworkInterface を使用して、次のように 3 つのネットワーク インターフェイスを作成します。Create three network interfaces with New-AzNetworkInterface.
次のように、Get-Credential を使用して VM の管理者のユーザー名とパスワードを設定します。Set an administrator username and password for the VMs with Get-Credential.
以下を使用して、仮想マシンを作成します。Create the virtual machines with:
# Set the administrator and password for the VMs. ##
$cred = Get-Credential
## Place the virtual network into a variable. ##
$vnet = Get-AzVirtualNetwork -Name 'myVNet' -ResourceGroupName 'CreatePubLBQS-rg'
## Place the load balancer into a variable. ##
$lb = @{
Name = 'myLoadBalancer'
ResourceGroupName = 'CreatePubLBQS-rg'
}
$bepool = Get-AzLoadBalancer @lb | Get-AzLoadBalancerBackendAddressPoolConfig
## Place the network security group into a variable. ##
$nsg = Get-AzNetworkSecurityGroup -Name 'myNSG' -ResourceGroupName 'CreatePubLBQS-rg'
## For loop with variable to create virtual machines for load balancer backend pool. ##
for ($i=1; $i -le 3; $i++)
{
## Command to create network interface for VMs ##
$nic = @{
Name = "myNicVM$i"
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
Subnet = $vnet.Subnets[0]
NetworkSecurityGroup = $nsg
LoadBalancerBackendAddressPool = $bepool
}
$nicVM = New-AzNetworkInterface @nic
## Create a virtual machine configuration for VMs ##
$vmsz = @{
VMName = "myVM$i"
VMSize = 'Standard_DS1_v2'
}
$vmos = @{
ComputerName = "myVM$i"
Credential = $cred
}
$vmimage = @{
PublisherName = 'MicrosoftWindowsServer'
Offer = 'WindowsServer'
Skus = '2019-Datacenter'
Version = 'latest'
}
$vmConfig = New-AzVMConfig @vmsz `
| Set-AzVMOperatingSystem @vmos -Windows `
| Set-AzVMSourceImage @vmimage `
| Add-AzVMNetworkInterface -Id $nicVM.Id
## Create the virtual machine for VMs ##
$vm = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
VM = $vmConfig
Zone = "$i"
}
New-AzVM @vm -AsJob
}
仮想マシンと bastion ホストのデプロイは、PowerShell ジョブとして送信されます。The deployments of the virtual machines and bastion host are submitted as PowerShell jobs. ジョブの状態を表示するには、Get-Job を使用します。To view the status of the jobs, use Get-Job:
Get-Job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Long Running O… AzureLongRunni… Completed True localhost New-AzBastion
2 Long Running O… AzureLongRunni… Completed True localhost New-AzVM
3 Long Running O… AzureLongRunni… Completed True localhost New-AzVM
4 Long Running O… AzureLongRunni… Completed True localhost New-AzVM
注意
Azure provides an ephemeral IP for Azure Virtual Machines which aren't assigned a public IP address, or are in the backend pool of an internal Basic Azure Load Balancer. The ephemeral IP mechanism provides an outbound IP address that isn't configurable.
The ephemeral IP is disabled when a public IP address is assigned to the virtual machine or the virtual machine is placed in the backend pool of a Standard Load Balancer with or without outbound rules. If a Azure Virtual Network NAT gateway resource is assigned to the subnet of the virtual machine, the ephemeral IP is disabled.
For more information on outbound connections in Azure, see Using Source Network Address Translation (SNAT) for outbound connections.
アウトバウンド規則構成の作成Create outbound rule configuration
ロード バランサーのアウトバウンド規則では、バックエンド プール内の VM 用にアウトバウンド送信元ネットワーク アドレス変換 (SNAT) を構成します。Load balancer outbound rules configure outbound source network address translation (SNAT) for VMs in the backend pool.
アウトバウンド接続の詳細については、「Azure のアウトバウンド接続」を参照してください。For more information on outbound connections, see Outbound connections in Azure.
送信パブリック IP アドレスの作成Create outbound public IP address
New-AzPublicIpAddress を使用して、myPublicIPOutbound という名前の Standard ゾーン冗長パブリック IP アドレスを作成します。Use New-AzPublicIpAddress to create a standard zone redundant public IP address named myPublicIPOutbound.
$publicipout = @{
Name = 'myPublicIPOutbound'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
Sku = 'Standard'
AllocationMethod = 'static'
Zone = 1,2,3
}
New-AzPublicIpAddress @publicipout
ゾーンのパブリック IP アドレスをゾーン 1 に作成するには、次のコマンドを使用します。To create a zonal public IP address in zone 1, use the following command:
$publicipout = @{
Name = 'myPublicIPOutbound'
ResourceGroupName = 'CreatePubLBQS-rg'
Location = 'eastus'
Sku = 'Standard'
AllocationMethod = 'static'
Zone = 1
}
New-AzPublicIpAddress @publicipout
アウトバウンドの構成を作成するCreate outbound configuration
Add-AzLoadBalancerFrontendIpConfig を使用して、次のように新しいフロントエンド IP 構成を作成します。Create a new frontend IP configuration with Add-AzLoadBalancerFrontendIpConfig.
Add-AzLoadBalancerBackendAddressPoolConfig を使用して、新しい送信バックエンド アドレス プールを作成します。Create a new outbound backend address pool with Add-AzLoadBalancerBackendAddressPoolConfig.
Set-AzLoadBalancer を使用して、次のようにプールおよびフロントエンド IP アドレスをロード バランサーに適用します。Apply the pool and frontend IP address to the load balancer with Set-AzLoadBalancer.
Add-AzLoadBalancerOutboundRuleConfig を使用して、次のようにアウトバウンド バックエンド プールの新しいアウトバウンド規則を作成します。Create a new outbound rule for the outbound backend pool with Add-AzLoadBalancerOutboundRuleConfig.
## Place public IP created in previous steps into variable. ##
$pubip = @{
Name = 'myPublicIPOutbound'
ResourceGroupName = 'CreatePubLBQS-rg'
}
$publicIp = Get-AzPublicIpAddress @pubip
## Get the load balancer configuration ##
$lbc = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Name = 'myLoadBalancer'
}
$lb = Get-AzLoadBalancer @lbc
## Create the frontend configuration ##
$fe = @{
Name = 'myFrontEndOutbound'
PublicIPAddress = $publicIP
}
$lb | Add-AzLoadBalancerFrontendIPConfig @fe | Set-AzLoadBalancer
## Create the outbound backend address pool ##
$be = @{
Name = 'myBackEndPoolOutbound'
}
$lb | Add-AzLoadBalancerBackendAddressPoolConfig @be | Set-AzLoadBalancer
## Apply the outbound rule configuration to the load balancer. ##
$rule = @{
Name = 'myOutboundRule'
AllocatedOutboundPort = '10000'
Protocol = 'All'
IdleTimeoutInMinutes = '15'
FrontendIPConfiguration = $lb.FrontendIpConfigurations[1]
BackendAddressPool = $lb.BackendAddressPools[1]
}
$lb | Add-AzLoadBalancerOutBoundRuleConfig @rule | Set-AzLoadBalancer
仮想マシンをアウトバウンド プールに追加するAdd virtual machines to outbound pool
Add-AzNetworkInterfaceIpConfig を使用して、次のようにロード バランサーのアウトバウンド プールに仮想マシンのネットワーク インターフェイスを追加します。Add the virtual machine network interfaces to the outbound pool of the load balancer with Add-AzNetworkInterfaceIpConfig:
## Get the load balancer configuration ##
$lbc = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Name = 'myLoadBalancer'
}
$lb = Get-AzLoadBalancer @lbc
# For loop with variable to add virtual machines to backend outbound pool. ##
for ($i=1; $i -le 3; $i++)
{
$nic = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Name = "myNicVM$i"
}
$nicvm = Get-AzNetworkInterface @nic
## Apply the backend to the network interface ##
$be = @{
Name = 'ipconfig1'
LoadBalancerBackendAddressPoolId = $lb.BackendAddressPools[0].id,$lb.BackendAddressPools[1].id
}
$nicvm | Set-AzNetworkInterfaceIpConfig @be | Set-AzNetworkInterface
}
IIS のインストールInstall IIS
Set-AzVMExtension を使用して、カスタム スクリプト拡張機能をインストールします。Use Set-AzVMExtension to install the Custom Script Extension.
この拡張機能によって PowerShell Add-WindowsFeature Web-Server
が実行され、IIS Web サーバーがインストールされます。次に、VM のホスト名を表示するように Default.htm ページが更新されます。The extension runs PowerShell Add-WindowsFeature Web-Server
to install the IIS webserver and then updates the Default.htm page to show the hostname of the VM:
重要
前の手順で行った仮想マシンのデプロイが完了したことを確認してから次に進んでください。Ensure the virtual machine deployments have completed from the previous steps before proceeding. 仮想マシンのデプロイ ジョブの状態は、Get-Job
を使用して確認します。Use Get-Job
to check the status of the virtual machine deployment jobs.
## For loop with variable to install custom script extension on virtual machines. ##
for ($i=1; $i -le 3; $i++)
{
$ext = @{
Publisher = 'Microsoft.Compute'
ExtensionType = 'CustomScriptExtension'
ExtensionName = 'IIS'
ResourceGroupName = 'CreatePubLBQS-rg'
VMName = "myVM$i"
Location = 'eastus'
TypeHandlerVersion = '1.8'
SettingString = '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
}
Set-AzVMExtension @ext -AsJob
}
拡張機能は、PowerShell ジョブとしてデプロイされます。The extensions are deployed as PowerShell jobs. インストール ジョブの状態を表示するには、Get-Job を使用します。To view the status of the installation jobs, use Get-Job:
Get-Job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
8 Long Running O… AzureLongRunni… Running True localhost Set-AzVMExtension
9 Long Running O… AzureLongRunni… Running True localhost Set-AzVMExtension
10 Long Running O… AzureLongRunni… Running True localhost Set-AzVMExtension
ロード バランサーをテストするTest the load balancer
Get-AzPublicIpAddress を使用して、ロード バランサーのパブリック IP アドレスを取得します。Use Get-AzPublicIpAddress to get the public IP address of the load balancer:
$ip = @{
ResourceGroupName = 'CreatePubLBQS-rg'
Name = 'myPublicIP'
}
Get-AzPublicIPAddress @ip | select IpAddress
そのパブリック IP アドレスをコピーし、ブラウザーのアドレス バーに貼り付けます。Copy the public IP address, and then paste it into the address bar of your browser. IIS Web サーバーの既定のページがブラウザーに表示されます。The default page of IIS Web server is displayed on the browser.
ロード バランサーが 3 つの VM すべてにトラフィックを分散していることを確認するには、各 VM の IIS Web サーバーの既定のページをカスタマイズした後、クライアント マシンで Web ブラウザーを強制的に最新の情報に更新します。To see the load balancer distribute traffic across all three VMs, you can customize the default page of each VM's IIS Web server and then force-refresh your web browser from the client machine.
リソースをクリーンアップするClean up resources
必要がなくなれば、Remove-AzResourceGroup コマンドを使用して、リソース グループやロード バランサー、その他のリソースを削除できます。When no longer needed, you can use the Remove-AzResourceGroup command to remove the resource group, load balancer, and the remaining resources.
Remove-AzResourceGroup -Name 'CreatePubLBQS-rg'
次のステップNext steps
このクイック スタートでは次のようにします。In this quickstart:
- Standard または Basic パブリック ロード バランサーを作成しましたYou created a standard or basic public load balancer
- 仮想マシンを接続しました。Attached virtual machines.
- ロード バランサーのトラフィック規則と正常性プローブを構成しました。Configured the load balancer traffic rule and health probe.
- ロード バランサーをテストしました。Tested the load balancer.
Azure Load Balancer についてさらに学習するには、次の記事に進んでください。To learn more about Azure Load Balancer, continue to: