😀

パブリックプレビューの Azure Container Registry 「専用エージェントプールで ACR タスクを実

に公開

背景と目的

以前、Azure Container Registry のタイマートリガータスクを使って定期的にコマンドを実行するサンプルを書いたのですが、ACR タスクは VNET 内で実行する必要があるケースが多いと思います。そこで今回は ACR の専用エージェントを VNET に作成して、その専用エージェント上で ACR タスク(ビルド)を実行してみました。

前提条件

bash
$ az version
{
  "azure-cli": "2.39.0",
  "azure-cli-core": "2.39.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

検証用 VNET を作成

bash
# 環境変数を設定します
region=eastasia
prefix=mnracrap

# リソースグループを作成します
az group create \
  --name ${prefix}-rg \
  --location $region

# VNET を作成します
az network vnet create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vnet \
  --address-prefixes 10.1.0.0/24 \
  --subnet-name default-subnet \
  --subnet-prefix 10.1.0.0/24

エージェントプール用の NSG ルールを作成

bash
# NSG を作成します
az network nsg create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-nsg

# NSG ルールを作成します(エージェントプールに必要なアウトバウンド通信を許可)
az network nsg rule create \
  --resource-group ${prefix}-rg \
  --name Allow-AzureKeyVault \
  --nsg-name ${prefix}-nsg \
  --priority 100 \
  --destination-address-prefixes AzureKeyVault \
  --destination-port-ranges 443 \
  --direction Outbound \
  --access Allow \
  --protocol Tcp
az network nsg rule create \
  --resource-group ${prefix}-rg \
  --name Allow-SSH \
  --nsg-name ${prefix}-nsg \
  --priority 200 \
  --destination-address-prefixes Storage \
  --destination-port-ranges 443 \
  --direction Outbound \
  --access Allow \
  --protocol Tcp
az network nsg rule create \
  --resource-group ${prefix}-rg \
  --name Allow-EventHub \
  --nsg-name ${prefix}-nsg \
  --priority 300 \
  --destination-address-prefixes EventHub \
  --destination-port-ranges 443 \
  --direction Outbound \
  --access Allow \
  --protocol Tcp
az network nsg rule create \
  --resource-group ${prefix}-rg \
  --name Allow-AzureActiveDirectory \
  --nsg-name ${prefix}-nsg \
  --priority 400 \
  --destination-address-prefixes AzureActiveDirectory \
  --destination-port-ranges 443 \
  --direction Outbound \
  --access Allow \
  --protocol Tcp
az network nsg rule create \
  --resource-group ${prefix}-rg \
  --name Allow-AzureMonitor \
  --nsg-name ${prefix}-nsg \
  --priority 500 \
  --destination-address-prefixes AzureMonitor \
  --destination-port-ranges 443 \
  --direction Outbound \
  --access Allow \
  --protocol Tcp

# NSG ルールを作成します(エージェントプールに必要なアウトバウンド通信を除く HTTPS 通信を遮断)
az network nsg rule create \
  --resource-group ${prefix}-rg \
  --name Deny-HTTPS \
  --nsg-name ${prefix}-nsg \
  --priority 4096 \
  --destination-address-prefixes Internet \
  --destination-port-ranges 443 \
  --direction Outbound \
  --access Deny

# NSG をサブネットに紐付けます
az network vnet subnet update \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet \
  --name default-subnet \
  --network-security-group ${prefix}-nsg

検証用 ACR を作成

bash
# ACR を作成します
az acr create \
  --resource-group ${prefix}-rg \
  --name ${prefix} \
  --sku Premium

専用エージェントプールを作成

bash
# サブネットにエージェントプールを作成します(自分の環境では約 10 分程度かかった)
az acr agentpool create \
  --registry ${prefix} \
  --name ${prefix}-agentpool \
  --tier S1 \
  --subnet-id $(az network vnet subnet show \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet \
  --name default-subnet \
  --query id \
  --output tsv)

失敗する ACR タスク(ビルド)を試す

bash
# エージェントプールでビルドを試してみます
az acr build \
  --registry ${prefix} \
  --agent-pool ${prefix}-agentpool \
  --image myimage:mytag \
  --file Dockerfile \
  https://github.com/Azure-Samples/acr-build-helloworld-node.git#main

# 5 分ほどで以下のようなメッセージが表示されて失敗しました
Argument '--agent-pool' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Sending context to registry: mnracrap...
Queued a build with ID: ck1
Waiting for an agent...
2022/08/06 00:00:14 Downloading source code...
Run ID: ck1 failed after 5m25s. Error: failed to download context. Please check if the URL is incorrect. If it has credentials, please check if they are expired
Run failed

成功する ACR タスク(ビルド)を試す

bash
# HTTPS 通信を遮断している NSG ルールを削除します
az network nsg rule delete \
  --resource-group ${prefix}-rg \
  --name Deny-HTTPS \
  --nsg-name ${prefix}-nsg

# もう一度エージェントプールでビルドを試してみます
az acr build \
  --registry ${prefix} \
  --agent-pool ${prefix}-agentpool \
  --image myimage:mytag \
  --file Dockerfile \
  https://github.com/Azure-Samples/acr-build-helloworld-node.git#main

# 今度は以下のようなメッセージが表示され成功しました
Argument '--agent-pool' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Sending context to registry: mnracrap...
Queued a build with ID: ck2
Waiting for an agent...
2022/08/06 00:08:04 Downloading source code...
2022/08/06 00:08:13 Finished downloading source code
2022/08/06 00:08:14 Using acb_vol_737d2310-af72-4ca4-a99b-524e70d524a9 as the home volume
2022/08/06 00:08:14 Setting up Docker configuration...
2022/08/06 00:08:16 Successfully set up Docker configuration
(中略)
Run ID: ck2 was successful after 1m17s

検証環境を削除

bash
# リソースグループを削除します
az group delete \
  --name ${prefix}-rg \
  --yes

参考

https://docs.microsoft.com/ja-jp/azure/container-registry/tasks-agent-pools

Discussion