Skip to content

Instantly share code, notes, and snippets.

@bart-jansen
Last active January 31, 2022 13:32
Show Gist options
  • Save bart-jansen/caf957f3678cf62cc0c942590b88da1c to your computer and use it in GitHub Desktop.
Save bart-jansen/caf957f3678cf62cc0c942590b88da1c to your computer and use it in GitHub Desktop.

Deploy Azure IoT Edge on Kubernetes

⚠️ This approach is no longer supported. So please use this approach with this caution in mind.

This guide shows how to deploy an Azure IoT Edge module on your Kubernetes cluster and is based on the existing Microsoft HelloWorld docs for Azure IoT Edge deployments.

Requirements

  • Working Kubernetes (K8s or K3s) cluster accessible through kubectl
  • Kubernetes Nodes should be running on version < 1.22.x (i.e. 1.21.9 or earlier)
  • Helm

Deploy IoT Hub, IoT Edge device and module

  1. Follow this guide to create a resource group, create an Azure IoT Hub and deploy the IoT Edge device.

    After this step you should be able to ssh into your VM and sudo iotedge system status should show the iotedge service running.

  2. Deploy the simulated temperature edge module using this guide.

    Save the connection string from az iot hub device-identity connection-string show --device-id myEdgeDevice --hub-name {hub_name} as we'll need this later in this tutorial

    When you now ssh into your vm, you should be able to run this to show generated temperature logs for your edge device:

    sudo iotedge logs SimulatedTemperatureSensor -f

Setup IoT Edge on Kubernetes

  1. Before you start, make sure your kubernetes nodes are running on version 1.21.9 or any version before that. Since the Helm packages that we're using aren't compatible with 1.22x and above. To check the version, run kubectl get nodes. This should return something like this:

    NAME      STATUS   ROLES                  AGE   VERSION
    rpi-400   Ready    control-plane,master   14h   v1.21.9+k3s1
    
  2. Create a Kubernetes namespace which we can install the edge geployment into:

    kubectl create ns helloworld
  3. Install IoT Edge Custom Resource Definition (CRD)

    helm install --repo https://edgek8s.blob.core.windows.net/staging edge-crd edge-kubernetes-crd  
  4. Deploy the edge workload into the previously created K8s namespace.

    # Store the device connection string in a variable (enclose in single quotes)
    export connStr='replace-with-device-connection-string-from-step-1'
    
    # Install edge deployment into the created namespace
    helm install --repo https://edgek8s.blob.core.windows.net/staging edge1 edge-kubernetes \
    --namespace helloworld \
    --set "provisioning.deviceConnectionString=$connStr"
  5. Give this a couple of minutes (really, it takes a while). And have a look at the pods that are deployed with:

    kubectl get pods -n helloworld

    If successful, this should return 4 pods like this:

    NAME                                          READY   STATUS    RESTARTS   AGE
    edgeagent-775b6cc944-8gsjx                    2/2     Running   0          4h26m
    simulatedtemperaturesensor-5d67846ddc-f2825   2/2     Running   0          4h18m
    edgehub-bdcfbdbc8-qjnd2                       2/2     Running   0          4h18m
    iotedged-795d466999-qgbjl                     1/1     Running   7          4h34m

    Depending on your network infrastructure, and where you have your Kubernetes cluster, it could be that you only see 1 or 2 pods that are not in a READY-state. You can always investigate the issue by doing a kubectl describe pods iotedged-795d466999-qgbjl if that's the case.

    💡 One of the most common issues, especially when running locally, is that you need to port forward port 35000 and 35001 in your firewall for the liveness and readiness probe to function and to be able to get data from the IoT devices.

  6. Once you have 4 pods running in your cluster, you can check the logs for the simulatedtemperature sensor to see if the data is coming through correctly (replace with your podname):

    kubectl logs -n helloworld simulatedtemperaturesensor-5d67846ddc-f2825 simulatedtemperaturesensor

    This should return data coming in like this:

    SimulatedTemperatureSensor Main() started.
    Initializing simulated temperature sensor to send 500 messages, at an interval of 5 seconds.
    To change this, set the environment variable MessageCount to the number of messages that should be sent (set it to -1 to send unlimited messages).
    [Information]: Trying to initialize module client using transport type [Amqp_Tcp_Only].
    [Information]: Successfully initialized module client of transport type [Amqp_Tcp_Only].
        01/31/2022 09:06:08> Sending message: 1, Body: [{"machine":{"temperature":20.900501942099307,"pressure":0.9886647782138451},"ambient":{"temperature":21.136306856589535,"humidity":25},"timeCreated":"2022-01-31T09:06:08.803183Z"}]
        01/31/2022 09:06:14> Sending message: 2, Body: [{"machine":{"temperature":20.73035564284323,"pressure":0.9692810226023931},"ambient":{"temperature":21.314686723898486,"humidity":26},"timeCreated":"2022-01-31T09:06:14.0849109Z"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment