Share via


チュートリアル: Azure IoT MQ Preview と Azure Event Grid の間に MQTT ブリッジを構成する

重要

Azure Arc によって有効にされる Azure IoT Operations Preview は、 現在プレビュー段階です。 運用環境ではこのプレビュー ソフトウェアを使わないでください。

ベータ版、プレビュー版、または一般提供としてまだリリースされていない Azure の機能に適用される法律条項については、「Microsoft Azure プレビューの追加使用条件」を参照してください。

このチュートリアルでは、Azure Event Grid MQTT ブローカー PaaS を使用して双方向 MQTT ブリッジ用に IoT MQ を構成する方法について説明します。 この機能を使用して、エッジとクラウドで IoT データを処理できます。 たとえば、IoT MQ を使用してエッジでテレメトリ データを処理し、そのデータを Azure Event Grid にブリッジしてクラウドでさらに処理することができます。

前提条件

環境変数を設定する

Azure CLI を使ってサインインします。

az login

設定の残りの部分の環境変数を設定します。 <> の値を、選択した有効な値または名前に置き換えます。 新しい Azure Event Grid 名前空間とトピック空間は、指定した名前に基づいて Azure サブスクリプション内に作成されます。

# For this tutorial, the steps assume the IoT Operations cluster and the Event Grid
# are in the same subscription, resource group, and location.

# Name of the resource group of Azure Event Grid and IoT Operations cluster 
export RESOURCE_GROUP=<RESOURCE_GROUP_NAME>

# Azure region of Azure Event Grid and IoT Operations cluster
export LOCATION=<LOCATION>

# Name of the Azure Event Grid namespace
export EVENT_GRID_NAMESPACE=<EVENT_GRID_NAMESPACE>

# Name of the Arc-enabled IoT Operations cluster 
export CLUSTER_NAME=<CLUSTER_NAME>

# Subscription ID of Azure Event Grid and IoT Operations cluster
export SUBSCRIPTION_ID=<SUBSCRIPTION_ID>

MQTT ブローカーを有効にして Event Grid 名前空間を作成する

Azure CLI を使用して Event Grid 名前空間を作成します。 場所は、Azure IoT Operations のデプロイに使用した場所と同じである必要があります。

az eventgrid namespace create \
  --namespace-name $EVENT_GRID_NAMESPACE \
  --resource-group $RESOURCE_GROUP \
  --location $LOCATION \
  --topic-spaces-configuration "{state:Enabled,maximumClientSessionsPerAuthenticationName:3}"

topic-spaces-configuration を設定すると、このコマンドにより次のような名前空間が作成されます。

  • MQTT ブローカー が有効になっている
  • 認証名あたりのクライアント セッションの最大数が 3 である。

最大クライアント セッション オプションを使用すると、IoT MQ は複数のインスタンスを生成しながら、接続を行うことができます。 詳細については、マルチセッションのサポートを参照してください。

トピック空間を作成する

Event Grid 名前空間で、トピック テンプレート telemetry/# を使用して tutorial という名前のトピック空間を作成します。

az eventgrid namespace topic-space create \
  --resource-group $RESOURCE_GROUP \
  --namespace-name $EVENT_GRID_NAMESPACE \
  --name tutorial \
  --topic-templates "telemetry/#"

トピック テンプレートで # ワイルドカードを使用すると、 telemetry トピック空間の下の任意のトピックに発行できます。 たとえば、telemetry/temperature または telemetry/humidity です。

Event Grid トピック空間に Azure IoT MQ Preview へのアクセス権を付与する

az k8s-extension show を使用して、Azure IoT MQ Arc 拡張機能のプリンシパル ID を見つけます。 このコマンドを実行すると、後で使用できるようにプリンシパル ID を変数に格納することができます。

export PRINCIPAL_ID=$(az k8s-extension show \
  --resource-group $RESOURCE_GROUP \
  --cluster-name $CLUSTER_NAME \
  --name mq \
  --cluster-type connectedClusters \
  --query identity.principalId -o tsv)
echo $PRINCIPAL_ID

次の形式の GUID 値である出力値 identity.principalId を書き留めておきます。

d84481ae-9181-xxxx-xxxx-xxxxxxxxxxxx

次に、Azure CLI を使用して、作成したトピック空間に対するパブリッシャー ロールとサブスクライバー ロールを IoT MQ に割り当てます。

パブリッシャー ロールを割り当てます。

az role assignment create \
  --assignee $PRINCIPAL_ID \
  --role "EventGrid TopicSpaces Publisher" \
  --scope /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventGrid/namespaces/$EVENT_GRID_NAMESPACE/topicSpaces/tutorial

サブスクライバー ロールを割り当てます。

az role assignment create \
  --assignee $PRINCIPAL_ID \
  --role "EventGrid TopicSpaces Subscriber" \
  --scope /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventGrid/namespaces/$EVENT_GRID_NAMESPACE/topicSpaces/tutorial

ヒント

スコープは、 az eventgrid namespace topic-space create を使用して前の手順で作成したトピック空間の id と一致し、コマンドの出力で見つけることができます。

Event Grid MQTT ブローカーのホスト名

Azure CLI を使用して Event Grid MQTT ブローカーのホスト名を取得します。

az eventgrid namespace show \
  --resource-group $RESOURCE_GROUP \
  --namespace-name $EVENT_GRID_NAMESPACE \
  --query topicSpacesConfiguration.hostname \
  -o tsv

topicSpacesConfiguration.hostname のようなホスト名値の出力値を書き留めておきます。

example.region-1.ts.eventgrid.azure.net

MQTT ブリッジ コネクタとトピック マップ リソースを作成する

bridge.yaml という名前の新しいファイルで、MQTT ブリッジ コネクタとトピック マップの構成を指定します。 remoteBrokerConnectionendpoint のプレースホルダー値の例を、前の手順の Event Grid MQTT ホスト名に置き換えます。 ポート番号 8883 を含めてください。

apiVersion: mq.iotoperations.azure.com/v1beta1
kind: MqttBridgeConnector
metadata:
  name: tutorial-bridge
  namespace: azure-iot-operations
spec:
  image: 
    repository: mcr.microsoft.com/azureiotoperations/mqttbridge
    tag: 0.4.0-preview
    pullPolicy: IfNotPresent
  protocol: v5
  bridgeInstances: 2
  logLevel: debug
  remoteBrokerConnection:
    endpoint: example.region-1.ts.eventgrid.azure.net:8883
    tls:
      tlsEnabled: true
    authentication:
      systemAssignedManagedIdentity:
        audience: https://eventgrid.azure.net
  localBrokerConnection:
    endpoint: aio-mq-dmqtt-frontend:8883
    tls:
      tlsEnabled: true
      trustedCaCertificateConfigMap: aio-ca-trust-bundle-test-only
    authentication:
      kubernetes: {}
---
apiVersion: mq.iotoperations.azure.com/v1beta1
kind: MqttBridgeTopicMap
metadata:
  name: tutorial-topic-map
  namespace: azure-iot-operations 
spec:
  mqttBridgeConnectorRef: tutorial-bridge
  routes:
    - direction: local-to-remote
      name: publish
      source: tutorial/local
      target: telemetry/iot-mq
      qos: 1
    - direction: remote-to-local
      name: subscribe
      source: telemetry/#
      target: tutorial/cloud
      qos: 1

MQTT ブリッジ コネクタは、次のように構成します。

  • Event Grid MQTT ブローカーをリモート ブローカーとして使用する
  • ローカル ブローカーとしてローカル IoT MQ ブローカーを使用する
  • リモート ブローカーとローカル ブローカーの両方に TLS を使用する
  • リモート ブローカーへの認証にシステム割り当てマネージド ID を使用する
  • ローカル ブローカーへの認証に Kubernetes サービス アカウントを使用する
  • トピック マップを使用して、tutorial/local トピックをリモート ブローカーの telemetry/iot-mq トピックにマップする
  • トピック マップを使用して、リモート ブローカーの telemetry/# トピックをローカル ブローカーの tutorial/cloud トピックにマップする

ローカル IoT MQ ブローカーで tutorial/local トピックに発行すると、メッセージはリモート Event Grid MQTT ブローカーの telemetry/iot-mq トピックにブリッジされます。 その後、メッセージはローカル IoT MQ ブローカーの tutorial/cloud トピックにブリッジ バックされます。 同様に、リモート Event Grid MQTT ブローカーで telemetry/iot-mq トピックに発行すると、メッセージはローカル IoT MQ ブローカーの tutorial/cloud トピックにブリッジされます。

kubectl を使用してデプロイ ファイルを適用します。

kubectl apply -f bridge.yaml
mqttbridgeconnector.mq.iotoperations.azure.com/tutorial-bridge created
mqttbridgetopicmap.mq.iotoperations.azure.com/tutorial-topic-map created

MQTT ブリッジのデプロイを確認する

kubectl を使用して、2 つのブリッジ インスタンスの準備が完了し、実行されていることを確認します。

kubectl get pods -n azure-iot-operations -l app=aio-mq-mqttbridge
NAME                       READY   STATUS    RESTARTS   AGE
aio-mq-tutorial-bridge-0   1/1     Running   0          45s
aio-mq-tutorial-bridge-1   1/1     Running   0          45s

これで、ローカル ブローカーで発行し、Event Grid MQTT ブローカーをサブスクライブし、メッセージ フローが想定どおりかを確認できるようになりました。

MQTT クライアントをデプロイする

MQTT ブリッジが機能していることを確認するには、IoT MQ と同じ名前空間に MQTT クライアントをデプロイします。 client.yaml という名前の新しいファイルで、クライアントのデプロイを指定します。

apiVersion: v1
kind: Pod
metadata:
  name: mqtt-client
  namespace: azure-iot-operations
spec:
  serviceAccountName: mqtt-client
  containers:
  - image: alpine
    name: mqtt-client
    command: ["sh", "-c"]
    args: ["apk add mosquitto-clients mqttui && sleep infinity"]
    volumeMounts:
    - name: mq-sat
      mountPath: /var/run/secrets/tokens
    - name: trust-bundle
      mountPath: /var/run/certs
  volumes:
  - name: mq-sat
    projected:
      sources:
      - serviceAccountToken:
          path: mq-sat
          audience: aio-mq
          expirationSeconds: 86400
  - name: trust-bundle
    configMap:
      name: aio-ca-trust-bundle-test-only

kubectl を使用してデプロイ ファイルを適用します。

kubectl apply -f client.yaml
pod/mqtt-client created

サブスクライバーを開始する

kubectl exec を使用して mosquitto クライアント ポッドでシェルを立ち上げます。

kubectl exec --stdin --tty mqtt-client -n azure-iot-operations -- sh

シェル内で、mqttui を使用して tutorial/# トピック空間上の IoT MQ ブローカーへのサブスクライバーを開始します。

mqttui log "tutorial/#" \
-b mqtts://aio-mq-dmqtt-frontend:8883 \
-u '$sat' \
--password $(cat /var/run/secrets/tokens/mq-sat) \
--insecure

コマンドを実行したままにして、新しいターミナル ウィンドウを開きます。

ブリッジ経由で MQTT メッセージをクラウドに発行する

新しいターミナル ウィンドウ上で、mosquitto クライアント ポッドの別のシェルを立ち上げます。

kubectl exec --stdin --tty mqtt-client -n azure-iot-operations -- sh

シェル内で mosquitto を使用して、tutorial/local トピックに 5 つのメッセージを発行します。

mosquitto_pub -h aio-mq-dmqtt-frontend -p 8883 \
-m "This message goes all the way to the cloud and back!" \
-t "tutorial/local" -u '$sat' -P $(cat /var/run/secrets/tokens/mq-sat) \
--cafile /var/run/certs/ca.crt \
--repeat 5 --repeat-delay 1 -d

サブスクライバー内のメッセージを表示する

サブスクライバー シェルに、発行したメッセージが表示されます。

23:17:50.802 QoS:AtMostOnce  tutorial/local     Payload( 52): This message goes all the way to the cloud and back!
23:17:51.086 QoS:AtMostOnce  tutorial/cloud     Payload( 52): This message goes all the way to the cloud and back!
23:17:51.803 QoS:AtMostOnce  tutorial/local     Payload( 52): This message goes all the way to the cloud and back!
23:17:51.888 QoS:AtMostOnce  tutorial/cloud     Payload( 52): This message goes all the way to the cloud and back!
23:17:52.804 QoS:AtMostOnce  tutorial/local     Payload( 52): This message goes all the way to the cloud and back!
23:17:52.888 QoS:AtMostOnce  tutorial/cloud     Payload( 52): This message goes all the way to the cloud and back!
23:17:53.805 QoS:AtMostOnce  tutorial/local     Payload( 52): This message goes all the way to the cloud and back!
23:17:53.895 QoS:AtMostOnce  tutorial/cloud     Payload( 52): This message goes all the way to the cloud and back!
23:17:54.807 QoS:AtMostOnce  tutorial/local     Payload( 52): This message goes all the way to the cloud and back!
23:17:54.881 QoS:AtMostOnce  tutorial/cloud     Payload( 52): This message goes all the way to the cloud and back!

ここでメッセージは、ローカル IoT MQ ブローカーに tutorial/local トピックに対して発行され、Event Grid MQTT ブローカーにブリッジされた後、 tutorial/cloud トピックでローカル IoT MQ ブローカーに再びブリッジされたことがわかります。 その後、メッセージはサブスクライバーに配信されます。 この例では、ラウンド トリップ時間は約 80 ミリ秒です。

Event Grid メトリックを確認してメッセージ配信を確認する

Event Grid メトリックを確認して、メッセージが Event Grid MQTT ブローカーに配信されていることを確認することもできます。 Azure portal で、作成した Event Grid 名前空間に移動します。 [メトリック]>[MQTT: 正常に発行されたメッセージ] の下にあります。 ローカル IoT MQ ブローカーにメッセージを発行すると、発行および配信されるメッセージの数が増えることがわかるはずです。

Screenshot of the metrics view in Azure portal to show successful MQTT messages.

ヒント

トピック マップ、QoS、メッセージ ルートの構成は CLI 拡張機能az iot ops check --detail-level 2で確認できます。

次のステップ

このチュートリアルでは、Azure Event Grid MQTT ブローカーを使用して双方向 MQTT ブリッジ用に IoT MQ を構成する方法について説明しました。 次の手順としては、以下のシナリオをご確認ください。

  • MQTT クライアントを使用して Event Grid MQTT ブローカーに直接メッセージを発行するには、「Event Grid MQTT ブローカーに MQTT メッセージを発行する」を参照してください。 作成したトピック空間に対する発行元のアクセス許可バインドをクライアント に付与すると、telemetry/temperaturetelemetry/humidityなど、telemetry の下にある任意のトピックにメッセージを発行できます。 これらのメッセージはすべて、ローカル IoT MQ ブローカーの tutorial/cloud トピックにブリッジされます。
  • Event Grid MQTT ブローカーのルーティング規則を設定するには、「Event Grid MQTT ブローカーのルーティング規則の構成」を参照してください。 ルーティング規則を使用すると、トピック名に基づいて異なるトピックにメッセージをルーティングしたり、メッセージの内容に基づいてメッセージをフィルター処理したりできます。