Binding running services to an IoT core

    To make the following commands easier, we are going to set the various variables here and use them later.

    Variables you must Change

    Variables you may Change

    1. export IOTCORE_REGISTRY="iot-demo"
    2. export IOTCORE_DEVICE="iot-demo-client"
    3. export IOTCORE_REGION="us-central1"
    4. export IOTCORE_TOPIC_DATA="iot-demo-pubsub-topic"
    5. export IOTCORE_TOPIC_DEVICE="iot-demo-device-pubsub-topic"

    Kubernetes

    1. Have a running Kubernetes cluster with kubectl pointing at it.

    GCP

    1. Create a Google Cloud Project.

    2. Have installed and pointing at that project.

    3. Enable the Cloud Pub/Sub API on that project.

      1. gcloud services enable pubsub.googleapis.com
    4. Create the two GCP PubSub topics.

      1. gcloud pubsub topics create $IOTCORE_TOPIC_DATA
    5. Setup Knative Eventing.

    GCP PubSub Source

    1. Create a GCP Service Account.

      1. Determine the Service Account to use, or create a new one.

      2. Give that Service Account the ‘Pub/Sub Editor’ role on your GCP project.

      3. Create two secrets with the downloaded key (one for the Source, one for the Receive Adapter):

        1. kubectl --namespace knative-sources create secret generic gcppubsub-source-key --from-file=key.json=PATH_TO_KEY_FILE.json
        2. kubectl --namespace default create secret generic google-cloud-key --from-file=key.json=PATH_TO_KEY_FILE.json
    2. Deploy the GcpPubSubSource controller as part of eventing-source’s controller.

    Broker

    1. Install the default Broker.

      1. kubectl create -f - <<EOF
      2. apiVersion: eventing.knative.dev/v1
      3. kind: Broker
      4. metadata:
      5. EOF

    GCP PubSub Source

    1. Deploy gcp-pubsub-source.yaml.

      1. sed -e "s/PROJECT_ID/$IOTCORE_PROJECT/" \
      2. -e "s/TOPIC_NAME/$IOTCORE_TOPIC_DATA/" \
      3. docs/eventing/samples/iot-core/gcp-pubsub-source.yaml |
      4. kubectl apply --filename -

    Trigger

    Even though the Source isn’t completely ready yet, we can setup the Trigger for all events coming out of it.

    1. Deploy trigger.yaml.

      1. kubectl apply --filename docs/eventing/samples/iot-core/trigger.yaml
      • This uses a very simple Knative Service to see that events are flowing. Feel free to replace it.

    IoT Core

    We now have everything setup on the Knative side. We will now setup the IoT Core.

    1. Create a device registry:

      1. gcloud iot registries create $IOTCORE_REGISTRY \
      2. --project=$IOTCORE_PROJECT \
      3. --region=$IOTCORE_REGION \
      4. --event-notification-config=topic=$IOTCORE_TOPIC_DATA \
    2. Create the certificates.

      1. openssl req -x509 -nodes -newkey rsa:2048 \
      2. -keyout device.key.pem \
      3. -out device.crt.pem \
      4. -days 365 \
      5. curl https://pki.google.com/roots.pem > ./root-ca.pem

    We now have everything installed and ready to go. We will generate events and see them in the subscriber.

    1. Run the following program to generate events:

      1. go run github.com/knative/docs/docs/eventing/samples/iot-core/generator \
      2. -project $IOTCORE_PROJECT \
      3. -region $IOTCORE_REGION \
      4. -registry $IOTCORE_REGISTRY \
      5. -device $IOTCORE_DEVICE \
      6. -ca "$PWD/root-ca.pem" \
      7. -key "$PWD/device.key.pem" \
      8. -src "iot-core demo" \
      9. -events 10
    2. Inspect the logs of the subscriber:

      1. kubectl logs --selector serving.knative.dev/service=event-display -c user-container

      You should see something along the similar to:

      1. {"ID":"481014114648052","Data":"eyJzb3VyY2VfaWQiOiJpb3QtY29yZSBkZW1vIiwiZXZlbnRfaWQiOiJlaWQtMzI3MjJiMzItZWU5Mi00YzZlLWEzOTgtNDlmYjRkYWYyNGE1IiwiZXZlbnRfdHMiOjE1NTM3MTczOTYsIm1ldHJpYyI6MC4xMzY1MjI5OH0=","Attributes":{"deviceId":"iot-demo-client","deviceNumId":"2754785852315736","deviceRegistryId":"iot-demo","deviceRegistryLocation":"us-central1","projectId":"s9-demo","subFolder":""},"PublishTime":"2019-03-27T20:09:56.685Z"}

    To cleanup the knative resources:

    1. Remove the GcpPubSubSource:

      1. sed -e "s/PROJECT_ID/$IOTCORE_PROJECT/" \
      2. -e "s/TOPIC_NAME/$IOTCORE_TOPIC_DATA/" \
      3. docs/eventing/samples/iot-core/gcp-pubsub-source.yaml |
      4. kubectl delete --filename -
    2. Remove the Trigger:

      1. kubectl delete --filename docs/eventing/samples/iot-core/trigger.yaml
    3. Remove the GcpPubSubSource controller:

    Feedback

    Was this page helpful?

    Sorry to hear that. Please tell us how we can improve.