Azure アプリケーションゲートウェイ 正常性プローブ

スポンサーリンク

Azure アプリケーションゲートウェイ(Application Gateway)で正常性プローブを設定する方法をまとめます。正常性プローブを定義すると、正常な仮想マシンのみに負荷分散させる事ができます。仮想マシンスケールセットやロードバランサにも正常性プローブの機能は備わっていますが、アプリケーションゲートウェイの正常性プローブの方が設定可能なパラメタは多めです。

前提

公式ドキュメント

参考になる公式ドキュメントを以下に示します。

事前設定

以下、リソースグループを作成します。

az group create --name MyResourceGroup --location japaneast

以下、仮想ネットワークを作成します。

az network vnet create \
  --resource-group MyResourceGroup \
  --name MyVnet \
  --address-prefix 172.16.0.0/16

az network vnet subnet create \
  --resource-group MyResourceGroup \
  --name Subnet01 \
  --vnet-name MyVnet \
  --address-prefixes 172.16.1.0/24

az network vnet subnet create \
  --resource-group MyResourceGroup \
  --name Subnet02 \
  --vnet-name MyVnet \
  --address-prefixes 172.16.2.0/24

アプリケーションゲートウェイを作成します。

az network public-ip create \
  --resource-group MyResourceGroup \
  --name MyPublicAddress \
  --sku Standard

az network application-gateway create \
  --name MyAppGateway \
  --resource-group MyResourceGroup \
  --capacity 2 \
  --sku Standard_v2 \
  --public-ip-address MyPublicAddress \
  --vnet-name MyVnet \
  --subnet Subnet01

アプリケーションゲートウェイ配下に仮想マシンスケールセットを配置します。

az vmss create \
  --resource-group MyResourceGroup \
  --name MyScaleSet01 \
  --image UbuntuLTS \
  --vm-sku Standard_B1s \
  --admin-username azureuser \
  --ssh-key-values ~/.ssh/authorized_keys \
  --vnet-name MyVnet \
  --subnet subnet02 \
  --app-gateway MyAppGateway \
  --backend-pool-name appGatewayBackendPool \
  --custom-data /dev/stdin << 'EOF'
#!/bin/bash
apt-get -y install nginx
echo "this is ${HOSTNAME}" > /var/www/html/index.html
EOF

正常性プローブ

設定

以下のようなコマンドで正常性プローブを定義します。Azureの仕様上、引数hostまたは引数host-name-from-http-settingsによるホストヘッダーの指定は必須です。

az network application-gateway probe create \
  --gateway-name MyAppGateway \
  --resource-group MyResourceGroup \
  --name probe-index-html \
  --protocol http \
  --host gokatei.net \
  --path '/index.html' \
  --match-body 'this is'

プローブの結果が正常と判定されるか否かは、以下のshowコマンドで確認できます。出力される量が多いので、適宜、queryで絞り込んで閲覧ください。

$ az network application-gateway show-backend-health \
  --name MyAppGateway \
  --resource-group MyResourceGroup \
  --query "backendAddressPools[0].backendHttpSettingsCollection[0].servers[].{address:address, health:health, log:healthProbeLog}"
[
  {
    "address": "172.16.2.4",
    "health": "Healthy",
    "log": "Success. Received 200 status code"
  },
  {
    "address": "172.16.2.5",
    "health": "Healthy",
    "log": "Success. Received 200 status code"
  }
]

動作確認

仮想マシンのうち1台で擬似障害を発生させます。まずは仮想マシンスケールセット内のインスタンスIDを調査します。

$ az vmss list-instances \
  --resource-group MyResourceGroup \
  --name MyScaleSet01 \
  --output table
InstanceId    LatestModelApplied    Location    ModelDefinitionApplied    Name            ProvisioningState    ResourceGroup    VmId
------------  --------------------  ----------  ------------------------  --------------  -------------------  ---------------  ------------------------------------
0             True                  japaneast   VirtualMachineScaleSet    MyScaleSet01_0  Succeeded            MyResourceGroup  f2eef5e7-dd2a-46c7-bfaf-47eb4ef59d0c
1             True                  japaneast   VirtualMachineScaleSet    MyScaleSet01_1  Succeeded            MyResourceGroup  88f2435a-b4f2-4561-8b46-85457d9b4e17

仮想マシンのうち1台でnginxを停止させます。

az vmss run-command invoke \
  --resource-group MyResourceGroup \
  --name MyScaleSet01 \
  --instance-id 0 \
  --command-id RunShellScript \
  --scripts 'systemctl stop nginx'

擬似障害発生直後は「Cannot connect to backend server…」とのログが表示されますが、healthは「Healthy」のままです。

% az network application-gateway show-backend-health \
  --name MyAppGateway \
  --resource-group MyResourceGroup \
  --query "backendAddressPools[0].backendHttpSettingsCollection[0].servers[].{address:address, health:health, log:healthProbeLog}"
[
  {
    "address": "172.16.2.4",
    "health": "Healthy",
    "log": "Cannot connect to backend server. Check whether any NSG/UDR/Firewall is blocking access to the server. Check if application is running on correct port. To learn more visit - https://aka.ms/servernotreachable."
  },
  {
    "address": "172.16.2.5",
    "health": "Healthy",
    "log": "Success. Received 200 status code"
  }
]

デフォルト設定では監視間隔は30秒で、8回の監視失敗で仮想マシンがダウンしたと判断します。8回の監視失敗後に状態を確認すると、healthが「Unhealthy」に変わります。

az network application-gateway show-backend-health \
  --name MyAppGateway \
  --resource-group MyResourceGroup \
  --query "backendAddressPools[0].backendHttpSettingsCollection[0].servers[].{address:address, health:health, log:healthProbeLog}"
[
  {
    "address": "172.16.2.4",
    "health": "Unhealthy",
    "log": "Cannot connect to backend server. Check whether any NSG/UDR/Firewall is blocking access to the server. Check if application is running on correct port. To learn more visit - https://aka.ms/servernotreachable."
  },
  {
    "address": "172.16.2.5",
    "health": "Healthy",
    "log": "Success. Received 200 status code"
  }
]

curlコマンドによる疎通確認をすると、正常な1台の仮想マシンのみに負荷分散される事が分かります。

admin@mac19 ~ % az network public-ip show \
  --resource-group MyResourceGroup \
  --name MyPublicAddress \
  --query "ipAddress"
"52.253.98.146"
admin@mac19 ~ % 
admin@mac19 ~ % 
admin@mac19 ~ % curl 52.253.98.146
this is myscae302000001
admin@mac19 ~ % 
admin@mac19 ~ % 
admin@mac19 ~ % curl 52.253.98.146
this is myscae302000001
admin@mac19 ~ % 
admin@mac19 ~ % 
admin@mac19 ~ % curl 52.253.98.146
this is myscae302000001
admin@mac19 ~ % 
タイトルとURLをコピーしました