0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Arcのエージェント自動更新 (プレビュー)を確認する

Posted at

本記事について

オンプレのサーバをAzureに接続して管理するためのAzure Arc-enabled serversで、接続のためのエージェントを自動更新する機能がパブリックプレビューになりました。
ライフサイクルを整理しつつ試した内容をまとめておきます。

Public Preview: Auto agent upgrade for Azure Arc-enabled servers

Azure Connected Machine Agentのライフサイクル

Azure Arc-enabled serversはサーバにAzure Connected Machine Agentというエージェントソフトをインストールします。
このエージェントは現在過去1年以内にリリースされたバージョンのみがサポートされることになっています。
また、2025年8月現在毎月1回新しいバージョンがリリースされています。
MS Learn | リリースノート

Azure Connected Machine Agentの更新

手動更新

これまではダウンロードしてきたインストーラーを利用して手動でのアップデートが必要でした。
そのため、自動化するにはPowerShellやシェルスクリプトで自動化を作りこむ必要がありました。
なお、Windows / Linuxいずれの場合もアップデート後に再起動は不要です。

  • Windowsの場合
    手動でインストーラーをダウンロードしてきてアップデートをおこなうか、Windows Updateの設定を変更してWindows Update経由でアップデートするように設定ができます。
  • Linuxの場合
    通常のモジュールと同じようにaptやyumでアップデートをおこないます。

自動更新

自動更新の設定

自動更新の設定は現時点ではプレビューなこともありAPI経由でのみ設定ができます。
Azure PowerShellのInvoke-AzRestMethodで以下のパラメータを指定して実行します。
パラメータの中では<リソースグループ名>とを書き換えます。

$params = @{
  ResourceGroupName = "<リソースグループ名>"
  ResourceProviderName = "Microsoft.HybridCompute"
  ResourceType = "Machines"
  ApiVersion = "2024-05-20-preview"
  Name = "<Azure Arc-enabled servers名>"
  Method = "PATCH"
  Payload = '{"properties":{"agentUpgrade":{ "enableAutomaticUpgrade":true}}}'
}
Invoke-AzRestMethod @params

成功すると200応答が返ってきます。

自動更新の仕組みを紐解く

自動更新はWindowsであればタスクスケジューラ、Linuxではcronで1日ごとに更新をチェックしてくれるようです。

Windowsの場合

タスクスケジューラを見るとazcmagentという名前でタスクが登録されています。
これは自動更新の設定をおこなう前から登録されており、エージェントインストール時に設定されていました。
おそらく自動更新に対応するバージョン1.48以降で入ったものと思われます。

何台かのWindows Server 2025で試しましたが、いずれもAM 1:30頃で日次で実行されるように設定されていました。
おそらく複数台が同じ環境にあるときに同時刻にダウンロードが始まらないようにずらしているのではと考えられますが、実行時間を変更したい場合はこちらを更新してしまえばよさそうですね。

タスクの実行内容を確認するとazcmagent_check_updates.ps1というスクリプトが呼ばれています。
task_scheduler_01.png

スクリプトの中身で更新を実行している部分を一部確認してみます。
azcmangetコマンドで見慣れないconfig getというオプション付きのコマンドが実行されており、値をチェックしています。
自動更新の設定有無や適用できる更新バージョンなどのチェックをおこない、該当するバージョンがあればインストーラーをダウンロードして更新してくれるようです。

azcmanget_check_updates.ps1 抜粋
try {

    try {
        $show = azcmagent show --json | ConvertFrom-Json
    } catch {
        Invoke-Failure -ErrorCode "AZCM0161" -Message "azcmagent show command failed: $_"
    }

    Set-SerialNumber
    Set-InstalledSoftware

    try {
        $enabled = azcmagent config get automaticupgrade.enabled
        if ($enabled -ne "true") {
            Send-Log "automaticupgrade.enabled not enabled" "AZCM0162" $show
            exit 0
        }
    } catch {
        Invoke-Failure -ErrorCode "AZCM0163" -Message "unable to get automaticupgrade.enabled flag: $_"
    }

    try {
        $desiredVersion = azcmagent config get automaticupgrade.desiredversion
    } catch {
        Invoke-Failure -ErrorCode "AZCM0164" -Message "unable to get automaticupgrade.desiredversion: $_"
    }

    $agentVersion = $show.agentVersion
    if ([System.Version]$agentVersion -gt [System.Version]$desiredVersion) {
        Send-Log "automaticupgrade.desiredversion $desiredVersion not greater than existing agent version $agentVersion" "AZCM0165" $show
        exit 0
    }

    try {
        $lastattempt = azcmagent config get automaticupgrade.lastattempt.status
    } catch {
        Invoke-Failure -ErrorCode "AZCM0166" -Message "unable to get automaticupgrade.lastattempt.status field: $_"
    }

    if ($lastattempt -eq "success") {
        Send-Log "Last attempt was successful, skipping upgrade" "AZCM0167" $show
        exit 0
    }
    if ($lastattempt -eq "failed") {
        $timestamp = Get-Date -UFormat %s -Millisecond 0
        try {
            $lastAttemptTimestamp = azcmagent config get automaticupgrade.lastattempt.timestamp
            $lastAttemptTimestamp =  [int]::Parse($lastAttemptTimestamp)
            
            $timeSinceLastAttemptSec = $timestamp - $lastAttemptTimestamp 
            if($timeSinceLastAttemptSec -lt 60 * 60  * 24 * 3){ # 60 seconds/min * 60 min/hr * 24 hr/day * 3 days 
                Send-Log "Last attempt failed, skipping upgrade" "AZCM0168" $show
                exit 0
            }
        } catch {
            Invoke-Failure -ErrorCode "AZCM0166" -Message "unable to check last attempt timestamp $_"
        }
    }

    try {
        $downloadlink = azcmagent config get automaticupgrade.downloadlink

        $proxy = (azcmagent config get proxy.url -j | ConvertFrom-Json).Data
        if ($proxy){
             $response = Invoke-WebRequest -Uri $downloadlink -Method Head -Proxy $proxy
        }
        else{
             $response = Invoke-WebRequest -Uri $downloadlink -Method Head 
        }
    } catch {
        Invoke-Failure -ErrorCode "AZCM0169" -Message "unable to get or reach automaticupgrade.downloadlink: $_"
    }

ちなみに、何台かで試したところ自動更新を設定してすぐの自動更新スクリプトの実行では更新がなされませんでした。
クラウド側から自動更新の設定が降りてくるまで数日かかることもあるようでした。

また、原因はわかりませんが一度目の自動更新が失敗した後、前回の更新失敗の結果が残っているためか次回以降更新が失敗し続けるケースもありました。
下記のコンフィグファイルに失敗の情報が残っているようでしたので、削除してAzure Hybrid Instance Metadata Serviceを再起動したところ次回更新で成功するようになったケースもありました。
このあたりはプレビュー中なのでまだ安定していないところがあるのかもしれません。

  • ファイルパス: C:\ProgramData\AzureConnectedMachineAgent\Config\localconfig.json
    failed_01.png

なお、エージェントのログは以下にあり、更新時の結果も出力されています。

  • ログフォルダパス: C:\ProgramData\AzureConnectedMachineAgent\Log

Linuxの場合

Ubuntuで確認しましたがcronでWindowsのときと同じように毎日AM 1:30頃に自動更新のスクリプトが設定されていました。
スクリプトの中身もおこなっている内容はほぼ同じようでした。

  • cron設定ファイルパス: /etc/cron.d/azcmagent_autoupgrade
10 1 * * * root /opt/azcmagent/bin/azcmagent_check_updates.sh

まとめ

エージェントの管理はこれまでもWindowsではWindows Updateに統合できていましたが、単体ツールとして自動更新が出てきたことでクラウド側ですべて設定が閉じるのは嬉しいです。
実際の運用ではエージェントに加えて利用するAzureサービスに応じてインストールされる拡張機能 (監視であればAzure Monitor Agent等)のライフサイクル管理も考慮する必要があります。
多くの拡張機能は自動更新に対応していますので、これらの機能を活用して管理を楽にしていきたいところです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?