教學課程:設定 Azure IoT MQ 預覽版與 Azure 事件方格 之間的 MQTT 網橋
重要
Azure IoT Operations Preview – 由 Azure Arc 啟用目前處於預覽狀態。 您不應該在生產環境中使用此預覽軟體。
請參閱 Microsoft Azure 預覽版增補使用規定,以了解適用於 Azure 功能 (搶鮮版 (Beta)、預覽版,或尚未正式發行的版本) 的法律條款。
在本教學課程中,您將瞭解如何使用 Azure 事件方格 MQTT 訊息代理程式 PaaS 設定雙向 MQTT 網橋的 IoT MQ。 您可以使用這項功能來處理邊緣和雲端中的 IoT 資料。 例如,您可以使用IoT MQ來處理邊緣的遙測數據,然後將數據橋接至 Azure 事件方格,以在雲端中進一步處理。
必要條件
設定環境變數
使用 Azure CLI 登入:
az login
設定其餘設定的環境變數。 將中的 <>
值取代為您選擇的有效值或名稱。 系統會根據您提供的名稱,在您的 Azure 訂用帳戶中建立新的 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 建立事件方格命名空間 。 位置應該與您用來部署 Azure IoT 作業的位置相同。
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繁衍多個實例,但仍連線。 若要深入瞭解,請參閱 多會話支援。
建立主題空間
在事件方格命名空間中,使用主題範本 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
。
為 Azure IoT MQ 預覽提供事件方格主題空間的存取權
使用 az k8s-extension show
,尋找 Azure IoT MQ Arc 擴充功能的主體標識碼。 命令會將主體標識碼儲存在變數中,以供日後使用。
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
記下 的輸出值 identity.principalId
,這是具有下列格式的 GUID 值:
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
提示
範圍符合 id
您在上一個步驟中建立 az eventgrid namespace topic-space create
的主題空間,而且您可以在命令的輸出中找到它。
事件方格 MQTT 訊息代理程式主機名
使用 Azure CLI 取得事件方格 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 網橋連接器和主題對應組態。 以上一個步驟中的 Event Grid MQTT 主機名取代 remoteBroker 連線 ionendpoint
中的範例佔位符值。 包含埠號碼 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 網橋連接器設定為:
- 使用事件方格 MQTT 訊息代理程式作為遠端訊息代理程式
- 使用本機 IoT MQ 訊息代理程式作為本機訊息代理程式
- 針對遠端和本機訊息代理程式使用 TLS
- 使用系統指派的受控識別向遠端代理程式進行驗證
- 使用 Kubernetes 服務帳戶向本機訊息代理程式進行驗證
- 使用主題對應將主題對應
tutorial/local
至telemetry/iot-mq
遠端訊息代理程式上的主題 - 使用主題對應,將遠端訊息代理程式上的主題對應
telemetry/#
至tutorial/cloud
本機訊息代理程式上的主題
當您發佈至 tutorial/local
本機 IoT MQ 訊息代理程式的主題時,訊息會橋接至 telemetry/iot-mq
遠端事件方格 MQTT 訊息代理程式上的主題。 然後,訊息會橋接回本機 tutorial/cloud
IoT MQ 訊息代理程式上的主題。 同樣地,當您發佈至 telemetry/iot-mq
遠端事件方格 MQTT 訊息代理程式上的主題時,訊息會橋接至 tutorial/cloud
本機 IoT MQ 訊息代理程式上的主題。
使用 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 來檢查兩個網橋實例已就緒且正在執行。
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
您現在可以在本機訊息代理程式上發佈,並訂閱事件方格 MQTT 訊息代理程式,並確認訊息流程如預期般運作。
部署 MQTT 用戶端
若要確認 MQTT 網橋是否正常運作,請將 MQTT 用戶端部署至與 IoT MQ 相同的命名空間。 在名為 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 用戶端 Pod 中啟動殼層。
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 用戶端 Pod 中啟動另一個殼層。
kubectl exec --stdin --tty mqtt-client -n azure-iot-operations -- sh
在殼層內,使用 mosquitto 將五則訊息發佈至 tutorial/local
主題。
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
主題、橋接至事件方格 MQTT 訊息代理程式,然後在主題上 tutorial/cloud
再次橋接回本機 IoT MQ 訊息代理程式。 訊息接著會傳遞至訂閱者。 在此範例中,來回時間約為 80 毫秒。
檢查事件方格計量以確認訊息傳遞
您也可以檢查事件方格計量,以確認訊息已傳遞至事件方格 MQTT 訊息代理程式。 在 Azure 入口網站 中,流覽至您建立的事件方格命名空間。 在 [>計量 MQTT:成功發佈的訊息] 下。 當您將訊息發佈至本機 IoT MQ 訊息代理程式時,您應該會看到已發行和傳遞的訊息數目增加。
提示
您可以使用 CLI 擴充az iot ops check --detail-level 2
功能來檢查主題對應、QoS 和訊息路由的設定。
下一步
在本教學課程中,您已瞭解如何使用 Azure 事件方格 MQTT 訊息代理程式設定雙向 MQTT 網橋的 IoT MQ。 後續步驟會探索下列案例:
- 若要使用 MQTT 用戶端將訊息直接發佈至事件方格 MQTT 訊息代理程式,請參閱 將 MQTT 訊息發佈至事件方格 MQTT 訊息代理程式。 為用戶端提供您建立之主題空間的
telemetry
發行者許可權系結,而且您可以將訊息發佈至 下的任何主題,例如telemetry/temperature
或telemetry/humidity
。 所有這些訊息都會橋接至tutorial/cloud
本機 IoT MQ 訊息代理程式的主題。 - 若要設定事件方格 MQTT 訊息代理程式的路由規則,請參閱 設定事件方格 MQTT 訊息代理程式的路由規則。 您可以使用路由規則,根據主題名稱將訊息路由傳送至不同的主題,或根據訊息內容篩選訊息。