クイック スタート: Azure IoT Operations SDK での開発を開始する

Azure IoT Operations SDK の開発を開始します。 次の手順に従って、サンプルをビルドして実行するための開発環境をセットアップし、独自の高可用性エッジ アプリケーションを作成してテストします。

GitHub リポジトリ | .NET SDK | Go SDK | Rust SDK

[前提条件]

開始する前に、次の前提条件を準備します。

  • Azure サブスクリプション。 Azure サブスクリプションをお持ちでない場合は、開始する前に 無料でアカウントを 1 つ作成してください。

セットアップ中

Azure IoT Operations SDK を使用して開発するには、Azure IoT Operationsデプロイされた Kubernetes クラスターが必要です。 さらに構成すると、開発環境から直接 MQTT ブローカーにアクセスできます。

Von Bedeutung

次の開発環境のセットアップ オプションでは、軽量の Kubernetes クラスターに K3d で実行されている K3s を使用します。 テスト設定を使用してAzure IoT Operationsを展開します。 運用環境デプロイの場合は、[セキュリティで保護された設定]を選択します。
セキュリティで保護された設定を使用するためには、Azure Arc 対応 Kubernetes クラスターの準備の手順に従って、Ubuntu 上に K3s クラスターを作成してください。そして、Azure IoT Operations を運用クラスターにセキュリティで保護された設定でデプロイしてください。 次に、開発用のAzure IoT Operationsの構成に進みます。

GitHub Codespaces は、最も合理化されたエクスペリエンスを提供し、数分で開発環境を稼働させることができます。

  1. Azure IoT Operations SDKs リポジトリから GitHub Codespaces に codespace を作成します。

  2. コードスペースが作成されると、開発者ツールを含むコンテナーと、K3d で実行されているローカル K3s クラスターがプレインストールされます。

Azure IoT Operationsをデプロイする

前の手順で作成した開発クラスターをアーク有効化し、テスト設定を使用してAzure IoT Operationsをデプロイします。

新しい Bash ターミナルを開き、次の手順を実行します。

  1. リポジトリのルート ディレクトリに移動します。

    cd <REPOSITORY ROOT>
    
  2. install-aio-arc.sh スクリプトを実行してクラスターをアーク有効化し、Azure IoT Operationsをデプロイします。 プレースホルダーを実際の値に置き換えます。

    パラメーター 価値
    LOCATION あなたの近くのAzureリージョン。 現在サポートされているすべてのリージョンの一覧については、サポートされているリージョンに関するページを参照してください。
    RESOURCE_GROUP クラスターが作成される新しいAzure リソース グループの名前。
    CLUSTER_NAME Kubernetes クラスターの名前。
    STORAGE_ACCOUNT_NAME ストレージ アカウントの名前。 ストレージ アカウント名の長さは 3 - 24 文字で、数字と小文字のみを使用できます。
    SCHEMA_REGISTRY_NAME スキーマ レジストリの名前。 スキーマ レジストリ名には、数字、英小文字、ハイフンのみを使用できます。
    SCHEMA_REGISTRY_NAMESPACE スキーマ レジストリ名前空間の名前。 名前空間によってテナント内のスキーマ レジストリが一意に識別されます。 スキーマ レジストリ名前空間名には、数字、英小文字、ハイフンのみを使用できます。
    DEVICE_REGISTRY_NAMESPACE デバイス レジストリ名前空間の名称です。 入居者内で一意で、3文字から24文字の範囲でなければなりません。 数字、英字、ハイフン、アンダースコアのみ使用できます。
    ./tools/deployment/install-aio-arc.sh -l <LOCATION> -g <RESOURCE_GROUP> -c <CLUSTER_NAME> -s <STORAGE_ACCOUNT_NAME> -r <SCHEMA_REGISTRY_NAME> -n <SCHEMA_REGISTRY_NAMESPACE> -d <DEVICE_REGISTRY_NAMESPACE>
    

    このスクリプトでは、次のコマンドを実行します。

    1. Azure CLIにログインする
    2. リソース グループを作成する
    3. 必要なAzure プロバイダーを登録する
    4. Kubernetes クラスターを Azure Arc に接続する
    5. Azure Arc機能を有効にする
    6. Azure Storage アカウントを作成する
    7. Azure IoT Operations のスキーマレジストリを作成する
    8. Azure デバイス レジストリ名前空間を作成する
    9. Azure IoT Operationsを初期化する
    10. Azure IoT Operations インスタンスを作成する
  3. デプロイが完了したら、az iot ops check を使用して、正常性、構成、および使いやすさAzure IoT Operationsサービスのデプロイを評価します。 check コマンドは、デプロイと構成の問題を見つけるのに役立ちます。

    az iot ops check
    

開発用にAzure IoT Operationsを構成する

Azure IoT Operationsをデプロイしたら、開発用に構成します。 MQTT ブローカーと認証方法を設定します。 開発環境に必要な環境変数を設定してください。

  1. リポジトリのルート ディレクトリに移動します。

    cd <REPOSITORY ROOT>
    
  2. configure-aio.sh スクリプトを実行して、開発用にAzure IoT Operationsを構成します。

    ./tools/deployment/configure-aio.sh
    

    このスクリプトでは、次のコマンドを実行します。

    1. 不足している場合は、証明書サービスを設定します
    2. x509 認証用のルート CA と中間 CA を作成します
    3. ブローカーが x509 クライアントを認証するための信頼バンドル ConfigMap を作成します
    4. SAT および x509 認証用の BrokerListener リソースと BrokerAuthentication リソースを構成します

インストールのテスト

セットアップをテストするには、 mosquitto_pub を使用して MQTT ブローカーに接続し、x509 証明書、SAT、および信頼バンドルを検証します。

  1. .session ディレクトリをエクスポートします。

    export SESSION=$(git rev-parse --show-toplevel)/.session
    
  2. TLS なし、認証なしをテストする:

    mosquitto_pub -L mqtt://localhost:1883/hello -m world --debug
    
  3. x509 認証を使用した TLS をテストする:

    mosquitto_pub -L mqtts://localhost:8883/hello -m world --cafile $SESSION/broker-ca.crt --cert $SESSION/client.crt --key $SESSION/client.key --debug
    
  4. SAT 認証を使用した TLS をテストする:

    mosquitto_pub -L mqtts://localhost:8884/hello -m world --cafile $SESSION/broker-ca.crt -D CONNECT authentication-method K8S-SAT -D CONNECT authentication-data $(cat $SESSION/token.txt) --debug
    

サンプルを実行する

このサンプルでは、Telemetry と remote プロシージャ コール (RPC) を使用したクライアントとサーバー間の簡単な通信を示します。 サーバーはカウンターの値を追跡し、クライアントからの RPC 要求を受け入れて、そのカウンターを読み取るか、またはインクリメントします。

このサンプルでは、WoT Thing モデル ファイルと共に v2 プロトコル コンパイラを使用します。TestThing フォルダーを参照してください。

  1. .NET 9.0 SDKをインストールします。

  2. Azure IoT Operations SDK GitHub リポジトリ環境変数から構成を読み取ります。 リポジトリ ルートには、MQTT ブローカーに接続するためにサンプルで使用される変数をエクスポートする .env ファイルが用意されています。 .env ファイルを編集して使用する環境の値を設定するか、ファイルに指定されている既定値を使用します。

  3. CounterServer サンプル ディレクトリに移動します。

    cd <REPOSITORY ROOT>/dotnet/samples/Protocol/RPC/CounterServer/
    
  4. サンプルをビルドします。

    dotnet build
    
  5. サンプルを実行します。

    source `git rev-parse --show-toplevel`/.env; export AIO_MQTT_CLIENT_ID=counter-server; dotnet run
    
  6. 新しいシェルを開き、CounterClient サンプル ディレクトリに移動します。

    cd <REPOSITORY ROOT>/dotnet/samples/Protocol/RPC/CounterClient/
    
  7. サンプルをビルドします。

    dotnet build
    
  8. サンプルを実行します。

    source `git rev-parse --show-toplevel`/.env; export AIO_MQTT_CLIENT_ID=counter-client; export COUNTER_SERVER_ID=counter-server; dotnet run
    
  9. クライアントとサーバーが通信し、クライアントがカウンターの値を読み上げて増量するリクエストを送り、サーバーがテレメトリを送信します。 詳細は 「クライアントとサーバー出力の検証」を参照してください。

  10. CounterClientサンプルは、完了すると自動的に終了します。 ターミナルで CounterServer を押して、Ctrl+C サンプルを停止することもできます。

クライアントとサーバーの出力を確認する

このセクションでは、クライアントとサーバーの出力の例を示します。 クライアントはカウンター値を読み込み、増量するよう呼び出しを行い、サーバーが応答します。 また、サーバーはテレメトリも出力するため、クライアントはカウンター値を追跡できます。 これらの例ではメッセージは論理的にグループ化されていますが、出力時のメッセージの順序はクライアントとサーバーのタイミングによって変わります。 これらの例はMQTTメッセージの出力を示していますが、クライアントとサーバーのmqttDiagファイルで環境変数をfalseにappsettings.json設定することで、この出力を抑制できます。

リードカウンター

以下の例は読み取りカタの出力メッセージを示しています。 クライアントとサーバー間のリクエストとレスポンスは相関IDでマッチングされ、この例では相関IDは aaaa0000-bb11-2222-33cc-444444ddddddです。

クライアントのアウトプット:

# Subscribe once on startup.
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4663363Z] [11]: TX (51 bytes) >>> Subscribe: [PacketIdentifier=2] [TopicFilters=clients/+/rpc/command-samples/+/readCounter@AtLeastOnce]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4689212Z] [6]: RX (6 bytes) <<< SubAck: [PacketIdentifier=2] [ReasonCode=GrantedQoS1]
CounterClient Information: 0 : Subscribed to topic filter 'clients/+/rpc/command-samples/+/readCounter' for command invoker 'readCounter'

# Invoke readCounter once on startup and once at end -- only one shown.
info: CounterClient.RpcCommandRunner[0] Calling ReadCounter with aaaa0000-bb11-2222-33cc-444444dddddd
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4777793Z] [11]: TX (335 bytes) >>> Publish: [Topic=rpc/command-samples/counter-server/readCounter] [PayloadLength=0] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=3]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4818287Z] [10]: RX (4 bytes) <<< PubAck: [PacketIdentifier=3] [ReasonCode=Success]
CounterClient Information: 0 : Invoked command 'readCounter' with correlation ID aaaa0000-bb11-2222-33cc-444444dddddd to topic 'rpc/command-samples/counter-server/readCounter'
CounterClient Information: 0 : >> [2026-07-29T17:35:53.5861515Z] [10]: RX (260 bytes) <<< Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/readCounter] [PayloadLength=21] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=1]
info: CounterClient.RpcCommandRunner[0] called read 0 with id aaaa0000-bb11-2222-33cc-444444dddddd
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6572000Z] [10]: TX (4 bytes) >>> PubAck: [PacketIdentifier=1] [ReasonCode=Success]

サーバー出力:

# Subscribe to readCounter topic on startup.
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3456281Z] [7]: TX (54 bytes) >>> Subscribe: [PacketIdentifier=1] [TopicFilters=rpc/command-samples/counter-server/readCounter@AtLeastOnce]
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3553742Z] [11]: RX (6 bytes) <<< SubAck: [PacketIdentifier=1] [ReasonCode=GrantedQoS1]
CounterServer Information: 0 : Command executor for 'readCounter' started.

# Multiple (2) invocations occur - only one shown.
CounterServer Information: 0 : >> [2026-07-29T17:35:53.4842146Z] [11]: RX (334 bytes) <<< Publish: [Topic=rpc/command-samples/counter-server/readCounter] [PayloadLength=0] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=1]
<6>17:35:53CounterServer.CounterService[0] --> Executing Counter.ReadCounter with id aaaa0000-bb11-2222-33cc-444444dddddd for counter-client
<6>17:35:53CounterServer.CounterService[0] --> Executed Counter.ReadCounter with id aaaa0000-bb11-2222-33cc-444444dddddd for counter-client
CounterServer Information: 0 : >> [2026-07-29T17:35:53.5815820Z] [7]: TX (261 bytes) >>> Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/readCounter] [PayloadLength=21] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=4]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.5854665Z] [11]: RX (4 bytes) <<< PubAck: [PacketIdentifier=4] [ReasonCode=Success]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.5881430Z] [12]: TX (4 bytes) >>> PubAck: [PacketIdentifier=1] [ReasonCode=Success]

カウンターを増やす

以下の例はインクリメントカウンタの出力メッセージを示しています。 リクエストとレスポンスはクライアントとサーバー間で相関IDによってマッチングされます。この例では相関IDは bbbb1111-cc22-3333-44dd-555555eeeeeeです。

クライアントのアウトプット:

# Invoke increment multiple times -- only one shown.
info: CounterClient.RpcCommandRunner[0] calling counter.incr  with id bbbb1111-cc22-3333-44dd-555555eeeeee
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6375578Z] [11]: TX (49 bytes) >>> Subscribe: [PacketIdentifier=4] [TopicFilters=clients/+/rpc/command-samples/+/increment@AtLeastOnce]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6399406Z] [10]: RX (6 bytes) <<< SubAck: [PacketIdentifier=4] [ReasonCode=GrantedQoS1]
CounterClient Information: 0 : Subscribed to topic filter 'clients/+/rpc/command-samples/+/increment' for command invoker 'increment'
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6461370Z] [11]: TX (372 bytes) >>> Publish: [Topic=rpc/command-samples/counter-server/increment] [PayloadLength=20] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=21]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6552803Z] [10]: RX (4 bytes) <<< PubAck: [PacketIdentifier=21] [ReasonCode=Success]
CounterClient Information: 0 : Invoked command 'increment' with correlation ID bbbb1111-cc22-3333-44dd-555555eeeeee to topic 'rpc/command-samples/counter-server/increment'
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6960747Z] [13]: RX (259 bytes) <<< Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/increment] [PayloadLength=22] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=12]
info: CounterClient.RpcCommandRunner[0] called counter.incr 10 with id bbbb1111-cc22-3333-44dd-555555eeeeee
CounterClient Information: 0 : >> [2026-07-29T17:35:53.7138884Z] [10]: TX (4 bytes) >>> PubAck: [PacketIdentifier=12] [ReasonCode=Success]

サーバー出力:

# Subscribe to increment once on startup.
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3548320Z] [7]: TX (52 bytes) >>> Subscribe: [PacketIdentifier=2] [TopicFilters=rpc/command-samples/counter-server/increment@AtLeastOnce]
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3613847Z] [11]: RX (6 bytes) <<< SubAck: [PacketIdentifier=2] [ReasonCode=GrantedQoS1]
CounterServer Information: 0 : Command executor for 'increment' started.

# Multiple invocations are received -- only one shown.
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6502886Z] [13]: RX (371 bytes) <<< Publish: [Topic=rpc/command-samples/counter-server/increment] [PayloadLength=20] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=2]
<6>17:35:53CounterServer.CounterService[0] --> Executing Counter.Increment with id bbbb1111-cc22-3333-44dd-555555eeeeee for counter-client
<6>17:35:53CounterServer.CounterService[0] --> Executed Counter.Increment with id bbbb1111-cc22-3333-44dd-555555eeeeee for counter-client
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6924040Z] [7]: TX (260 bytes) >>> Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/increment] [PayloadLength=22] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=15]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6983722Z] [18]: RX (4 bytes) <<< PubAck: [PacketIdentifier=15] [ReasonCode=Success]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6992783Z] [12]: TX (4 bytes) >>> PubAck: [PacketIdentifier=2] [ReasonCode=Success]

カウンターバリューテレメトリー

以下の例はCounterValueテレメトリーの出力メッセージを示しています。 サーバー出力が最初に表示されるのは、テレメトリを送信するためです。

サーバー出力:

# Telemetry data is published multiple times. 
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6805980Z] [7]: TX (203 bytes) >>> Publish: [Topic=telemetry/telemetry-samples/counterValue] [PayloadLength=18] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=5]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6843445Z] [11]: RX (4 bytes) <<< PubAck: [PacketIdentifier=5] [ReasonCode=Success]
CounterServer Information: 0 : Telemetry sent successfully to the topic 'telemetry/telemetry-samples/counterValue'

クライアントのアウトプット:

# Subscribe performed once on startup.
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4283758Z] [11]: TX (48 bytes) >>> Subscribe: [PacketIdentifier=1] [TopicFilters=telemetry/telemetry-samples/counterValue@AtLeastOnce]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4355484Z] [10]: RX (6 bytes) <<< SubAck: [PacketIdentifier=1] [ReasonCode=GrantedQoS1]
CounterClient Information: 0 : Telemetry receiver subscribed for topic telemetry/telemetry-samples/counterValue.

# Multiple telemetry values received - only one shown.
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6820692Z] [6]: RX (202 bytes) <<< Publish: [Topic=telemetry/telemetry-samples/counterValue] [PayloadLength=18] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=2]
CounterClient Information: 0 : Telemetry received from telemetry/telemetry-samples/counterValue
info: CounterClient.CounterClient[0] Telemetry received from counter-server: CounterValue=4
CounterClient Information: 0 : >> [2026-07-29T17:35:53.7131604Z] [10]: TX (4 bytes) >>> PubAck: [PacketIdentifier=2] [ReasonCode=Success]

構成の概要

MQTT ブローカーの構成

ソフトウェアをインストールすると、クラスターには次の MQTT ブローカー定義が含まれます。

コンポーネントの種類 名前 説明
Broker デフォルト MQTT ブローカー
BrokerListener デフォルト MQTT ブローカーへの クラスター アクセス を提供します
BrokerListener default-external MQTT ブローカー へのオフクラスター アクセス を提供します
BrokerAuthentication デフォルト SAT 認証定義
BrokerAuthentication default-x509 x509 認証定義

MQTT ブローカー アクセス

次の表で説明する接続情報を使用して、オンクラスターとオフクラスターの両方の MQTT ブローカーにアクセスできます。 アプリケーションの構成時に使用する環境変数については、「Connection 設定」を参照してください。

注

MQTT ブローカーのオフクラスターへのアクセス時のホスト名は、セットアップによっては localhost と異なる場合があります。

ホスト名 認証 TLS オン クラスター ポート オフ クラスター ポート
aio-broker SAT ✅ 18883 -
localhost 無し ❌ 1883 1883
localhost x509 ✅ 8883 8883
localhost SAT ✅ 8884 8884

開発成果物

デプロイ スクリプトの一部として、MQTT ブローカーへの接続と認証を容易にするために、ローカル環境によって次のファイルが作成されます。 これらのファイルは、リポジトリ のルートにある .session ディレクトリにあります。

ファイル 説明
broker-ca.crt ポート 8883 および 8884 で MQTT ブローカーを検証するために必要な MQTT ブローカー信頼バンドル
token.txt 8884 の MQTT ブローカーと認証するためのサービス認証トークン (SAT)
client.crt ポートで MQTT ブローカーを使用して認証するための x509 クライアント証明書 8883
client.key ポートで MQTT ブローカーを使用して認証するための x509 クライアント秘密キー 8883

トラブルシューティング

Azure IoT Operations SDK GitHub リポジトリの一般的な問題については、トラブルシューティング ガイドを参照してください。

次のステップ

このクイック スタートでは、Azure IoT Operations SDK を設定し、サンプル アプリケーションを実行しました。 SDK を使用した開発の詳細については、次のリソースを参照してください。