LoginSignup
0
0

More than 1 year has passed since last update.

Azure Container Registry のタイマートリガータスクを使って定期的にコマンドを実行するサンプル

Posted at

背景と目的

Azure で定期的にコマンドを実行する環境としてパッと思いつくのは、Azure Functions と App Service の WebJobs です。これはこれで便利なのですが、色々と前提条件や制約があって検討の結果 Linux の crontab に落ち着いたりすることがあったりなかったり・・・。そこで今回のサンプルは、Azure Container Registry のタイマートリガータスクを使ってコンテナイメージ化したコマンドを定期的に実行してみます。コマンドをコンテナイメージ化さえ出来れば、プログラミング言語や実行環境の前提条件や制約はかなり広がるので、後はとても簡単です。

ACR を作成

bash
# 環境変数をセットします
region=japaneast
prefix=mnracrtask

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

# ACR を作成します
az acr create \
  --resource-group ${prefix}-rg \
  --name ${prefix} \
  --sku Basic \
  --admin-enabled true

# ACR の名前を取得します
acr=$(az acr show \
  --name ${prefix} \
  --query loginServer \
  --output tsv)

ACR タイマートリガータスクを作成

bash
# タスクを作成します
# コマンド実行はマイクロソフトが用意している hello-world を使用
az acr task create \
  --name timertask \
  --registry $acr \
  --cmd mcr.microsoft.com/hello-world \
  --schedule "0 */5 * * *" \
  --context /dev/null

# タスクの登録状況を確認します
az acr task show \
  --name timertask \
  --registry $acr \
  --output table

NAME       PLATFORM    STATUS    SOURCE REPOSITORY    TRIGGERS
---------  ----------  --------  -------------------  -----------------
timertask  linux       Enabled                        BASE_IMAGE, TIMER

ACR タスクを実行し検証

bash
# 手動でタスクを実行します
az acr task run \
  --name timertask \
  --registry $acr

The login server endpoint suffix '.azurecr.io' is automatically omitted.
Queued a run with ID: ce1
Waiting for an agent...
Unable to find image 'mcr.microsoft.com/hello-world:latest' locally
latest: Pulling from hello-world
1b930d010525: Pulling fs layer
1b930d010525: Verifying Checksum
1b930d010525: Download complete
1b930d010525: Pull complete
Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
Status: Downloaded newer image for mcr.microsoft.com/hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Hello from Docker! を出力するコマンドが実行されたことが確認できます。

つまり、コマンドを実行するコンテナイメージを ACR に用意しておき、ACR タスクでそのコンテナイメージを定期的に実行できるという事は、コマンドは azure cli でも terraform でも可能になりますし、タイマートリガーではなく CI/CD パイプラインの途中で ACR タスクを実行してコマンドを実行する事も可能になリます。

ACR タスク実行履歴を確認

bash
# 実行履歴を確認します(手動実行直後)
az acr task list-runs \
  --name timertask \
  --registry $acr \
  --output table

RUN ID    TASK       PLATFORM    STATUS     TRIGGER    STARTED               DURATION
--------  ---------  ----------  ---------  ---------  --------------------  ----------
ce1       timertask  linux       Succeeded  Manual     2022-07-09T23:57:39Z  00:00:06

# 実行履歴を確認します(タイマートリガー実行直後)
az acr task list-runs \
  --name timertask \
  --registry $acr \
  --output table

RUN ID    TASK       PLATFORM    STATUS     TRIGGER    STARTED               DURATION
--------  ---------  ----------  ---------  ---------  --------------------  ----------
ce2       timertask  linux       Succeeded  Timer      2022-07-10T00:01:07Z  00:00:07
ce1       timertask  linux       Succeeded  Manual     2022-07-09T23:57:39Z  00:00:06

検証環境を削除

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

参考

0
0
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
0
0