Редагувати

Поділитися через


Use Dapr to develop distributed application workloads that talk with MQTT broker

Important

Azure IoT Operations Preview – enabled by Azure Arc is currently in preview. You shouldn't use this preview software in production environments.

You'll need to deploy a new Azure IoT Operations installation when a generally available release becomes available. You won't be able to upgrade a preview installation.

For legal terms that apply to Azure features that are in beta, in preview, or otherwise not yet released into general availability, see the Supplemental Terms of Use for Microsoft Azure Previews.

To use the MQTT broker Dapr pluggable components, deploy both the pub/sub and state store components in your application deployment along with your Dapr application. This guide shows you how to deploy an application using the Dapr SDK and MQTT broker pluggable components.

Prerequisites

Creating a Dapr application

Building the application

The first step is to write an application that uses a Dapr SDK to publish/subscribe or do state management.

Package the application

After you finish writing the Dapr application, build the container:

  1. Package the application into a container with the following command:

    docker build . -t my-dapr-app
    
  2. Push it to your Container Registry of your choice, such as:

Deploy a Dapr application

The following Deployment definition contains volumes for SAT authentication and TLS certificate chain, and utilizes Dapr sidecar injection to automatically add the pluggable components to the Pod.

The following definition components might require customization to your specific application:

Component Description
template:metadata:annotations:dapr.io/inject-pluggable-components Allows the IoT Operations pluggable components to be automatically injected into the pod
template:metadata:annotations:dapr.io/app-port Tells Dapr which port your application is listening on. If your application us not using this feature (such as a pubsub subscription), then remove this line
volumes:mqtt-client-token The System Authentication Token used for authenticating the Dapr pluggable components with the MQTT broker
volumes:aio-ca-trust-bundle The chain of trust to validate the MQTT broker TLS cert. This defaults to the test certificate deployed with Azure IoT Operations
containers:name A name given to your application container
containers:image The application container you want to deploy

Caution

If your Dapr application is not listening for traffic from the Dapr sidecar, then remove the dapr.io/app-port and dapr.io/app-protocol annotations otherwise the Dapr sidecar will fail to initialize.

  1. Save the following yaml to a file named dapr-app.yaml:

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: dapr-client
      namespace: azure-iot-operations
      annotations:
        aio-broker-auth/group: dapr-workload
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-dapr-app
      namespace: azure-iot-operations
    spec:
      selector:
        matchLabels:
          app: my-dapr-app
      template:
        metadata:
          labels:
            app: my-dapr-app
          annotations:
            dapr.io/enabled: "true"
            dapr.io/inject-pluggable-components: "true"
            dapr.io/app-id: "my-dapr-app"
            dapr.io/app-port: "6001"
            dapr.io/app-protocol: "grpc"
        spec:
          serviceAccountName: dapr-client
    
          volumes:
          # SAT used to authenticate between Dapr and the MQTT broker
          - name: mqtt-client-token
            projected:
              sources:
                - serviceAccountToken:
                    path: mqtt-client-token
                    audience: aio-internal
                    expirationSeconds: 86400
    
          # Certificate chain for Dapr to validate the MQTT broker
          - name: aio-ca-trust-bundle
            configMap:
              name: azure-iot-operations-aio-ca-trust-bundle
    
          containers:
          # Container for the Dapr application 
          - name: mq-dapr-app
            image: <YOUR_DAPR_APPLICATION>
    
  2. Deploy the component by running the following command:

    kubectl apply -f dapr-app.yaml
    kubectl get pods -w
    

    The pod should report three containers running after a short interval, as shown in the following example output:

    NAME                          READY   STATUS              RESTARTS   AGE
    ...
    my-dapr-app                   3/3     Running             0          30s
    

Troubleshooting

If the application doesn't start or you see the containers in CrashLoopBackoff state, the log for the daprd container often contains useful information.

Run the following command to view the logs for the daprd component:

kubectl logs -l app=my-dapr-app -c daprd

Next steps

Now that you know how to develop a Dapr application, you can run through the tutorial to Build an event-driven app with Dapr.