|
| 1 | +# thin-edge.io on Kubernetes |
| 2 | + |
| 3 | +:construction: This is a work-in-progress so don't expect everything to work out of the box in all situations. Contributions are welcomed to improve the setup. |
| 4 | + |
| 5 | +This document describes the steps required to get thin-edge.io deployed in a Kubernetes (k8s) cluster. |
| 6 | + |
| 7 | +## Pre-requisites |
| 8 | + |
| 9 | +The following pre-requisites are required to run the example: |
| 10 | + |
| 11 | +* A kubernetes cluster - [k3s](https://k3s.io/) is a good option for a single node cluster if you're just trying things out locally |
| 12 | +* kubectl - cli to interact with kubernetes |
| 13 | +* [helm](https://helm.sh/) |
| 14 | + |
| 15 | +Kubernetes know-how is also assumed, so if you have trouble with setting up Kubernetes, please consult the public Kubernetes documentation or seek help in community forums (e.g. stack overflow etc.). |
| 16 | + |
| 17 | +## Getting started |
| 18 | + |
| 19 | +The following guide should help you install thin-edge.io as a helm chart. |
| 20 | + |
| 21 | +1. Create the device certificate key pair |
| 22 | + |
| 23 | + **Option 1: Create self-signed certificates using tedge cli** |
| 24 | + |
| 25 | + ```sh |
| 26 | + mkdir device-certs |
| 27 | + tedge --config-dir $(pwd) cert create --device-id mydevice001 |
| 28 | + ``` |
| 29 | + |
| 30 | + The certificates will be stored under the `./device-certs` folder. |
| 31 | + |
| 32 | + Alternatively, you can also create device certificates using a CA and openssl. Please consult the openssl documentation on how to do this. |
| 33 | + |
| 34 | +1. Add device certificate to Cumulocity IoT's Trusted Certificates |
| 35 | +
|
| 36 | + You can uploaded it to Cumulocity IoT using the Device Management UI, or using the go-c8y-cli tool: |
| 37 | +
|
| 38 | + ```sh |
| 39 | + c8y devicemanagement certificates create --name mydevice001 --file ./device-certs/tedge-certificate.pem --status ENABLED --autoRegistrationEnabled |
| 40 | + ``` |
| 41 | +
|
| 42 | +1. Create a kubernetes namespace where the helm chart will be deployed to |
| 43 | +
|
| 44 | + ```sh |
| 45 | + kubectl create namespace tedge |
| 46 | + ``` |
| 47 | +
|
| 48 | +1. Create a Secret for the device certificate key pair |
| 49 | +
|
| 50 | + ```sh |
| 51 | + kubectl create secret generic tedge-certs --from-file=./device-certs/tedge-certificate.pem --from-file=./device-certs/tedge-private-key.pem --namespace tedge |
| 52 | + ``` |
| 53 | +
|
| 54 | +1. Install the thin-edge.io helm chart |
| 55 | +
|
| 56 | + ```sh |
| 57 | + helm install tedge-chart ./tedge --set c8y.url=example.eu-latest.cumulocity.com --namespace tedge |
| 58 | + ``` |
| 59 | +
|
| 60 | + Change the `c8y.url=` value to the MQTT endpoint of your Cumulocity IoT tenant. |
| 61 | +
|
| 62 | +1. Validate the device created in Cumulocity |
| 63 | +
|
| 64 | + If you are using go-c8y-cli, then you can open the Device Management application to your device using the following command: |
| 65 | +
|
| 66 | + ```sh |
| 67 | + c8y identity get --name mydevice001 | c8y applications open |
| 68 | + ``` |
| 69 | +
|
| 70 | +1. The mosquitto broker can be accessed by other pods in the cluster via the `mosquitto.<namespace>.svc.cluster.local` endpoint |
| 71 | +
|
| 72 | + **Using an existing service** |
| 73 | +
|
| 74 | + The MQTT service endpoint can be verified from within a deployment using the following command: |
| 75 | +
|
| 76 | + ```sh |
| 77 | + kubectl exec -n tedge -it service/mosquitto -- mosquitto_sub -h 'mosquitto.tedge.svc.cluster.local' -p 1883 -t '#' -W 5 -v |
| 78 | + ``` |
| 79 | + |
| 80 | + **Using a new pod** |
| 81 | + |
| 82 | + The MQTT service endpoint can also be verified from other pods by creating a test pod, and executing a command from it. |
| 83 | + |
| 84 | + ```sh |
| 85 | + kubectl run -n tedge --restart=Never --image eclipse-mosquitto tedge-test -- sleep infinity |
| 86 | + kubectl exec -n tedge -it pod/tedge-test -- mosquitto_sub -h 'mosquitto.tedge.svc.cluster.local' -p 1883 -t '#' -W 3 -v |
| 87 | + kubectl delete -n tedge pod/tedge-test |
| 88 | + ``` |
| 89 | + |
| 90 | + ```sh |
| 91 | + te/device/main/service/mosquitto-c8y-bridge {"@id":"example001:device:main:service:mosquitto-c8y-bridge","@parent":"device/main//","@type":"service","name":"mosquitto-c8y-bridge","type":"service"} |
| 92 | + te/device/main/service/mosquitto-c8y-bridge/status/health 1 |
| 93 | + te/device/main/service/tedge-mapper-c8y {"@parent":"device/main//","@type":"service","type":"service"} |
| 94 | + te/device/main/service/tedge-mapper-c8y/status/health {"pid":1,"status":"up","time":1710236345.2828279} |
| 95 | + te/device/main/service/tedge-agent {"@parent":"device/main//","@type":"service","type":"service"} |
| 96 | + te/device/main/service/tedge-agent/status/health {"pid":1,"status":"up","time":1710236338.9187257} |
| 97 | + te/device/main///twin/c8y_Agent {"name":"thin-edge.io","url":"https://thin-edge.io","version":"1.0.1"} |
| 98 | + te/device/main///cmd/config_snapshot {"types":["tedge-configuration-plugin","tedge-log-plugin","tedge.toml"]} |
| 99 | + te/device/main///cmd/config_update {"types":["tedge-configuration-plugin","tedge-log-plugin","tedge.toml"]} |
| 100 | + te/device/main///cmd/log_upload {"types":["software-management"]} |
| 101 | + te/device/main///cmd/restart {} |
| 102 | + te/device/main///cmd/software_list {} |
| 103 | + te/device/main///cmd/software_update {} |
| 104 | + Timed out |
| 105 | + command terminated with exit code 27 |
| 106 | + ``` |
| 107 | + |
| 108 | +## Uninstall helm chart |
| 109 | + |
| 110 | +You can uninstall the helm chart using the following command: |
| 111 | + |
| 112 | +``` |
| 113 | +helm uninstall tedge-chart --namespace tedge |
| 114 | +``` |
| 115 | +
|
| 116 | +## Project structure |
| 117 | +
|
| 118 | +Key points about the deployment design are listed below: |
| 119 | +
|
| 120 | +* The thin-edge.io helm chart consists of two deployments: |
| 121 | + * a multi-container pod with `mosquitto` broker and `tedge-mapper` along with `tedge-bootstrap` init container |
| 122 | + that performs the bootstrapping |
| 123 | + * an independent deployment of `tedge-agent` |
| 124 | +* The `mosquitto` broker and `tedge-mapper` are deployed in the same pod with `/etc/tedge` directory mounted as a shared volume, |
| 125 | + so that the mapper and the broker can access the bridge configurations created by the `tedge-bootstrap` container. |
| 126 | +* A custom mosquitto configuration is used for the `mosquitto` container so that it picks up configuration extensions |
| 127 | + from the `/etc/tedge/mosquitto-conf` directory where the bridge configurations are created. |
| 128 | +* The mosquitto broker from the first deployment is exposed as a service so that the `tedge-agent` deployment can access it. |
| 129 | + Similarly, the file-transfer-service of the `tedge-agent` is also exposed as a service for the mapper to use it. |
| 130 | +* The config settings required by the thin-edge.io components are provided via env variables. |
| 131 | +* The device certificate key pair generated and provided to the cluster as a `Secret`. |
| 132 | +* The persistent volume for the shared `/etc/tedge` directory is a local volume. |
| 133 | +
|
| 134 | +## Misc. |
| 135 | +
|
| 136 | +### Using dedicated colima instance on MacOS |
| 137 | +
|
| 138 | +If you want to use a dedicated k3s single node cluster on MacOS, then you can use colima to create a dedicated environment (different from the default colima instance). |
| 139 | +
|
| 140 | +**Containerd runtime** |
| 141 | +
|
| 142 | +You can create a colima instance using containerd as the runtime using the following command: |
| 143 | +
|
| 144 | +```sh |
| 145 | +colima start k3s --runtime containerd --kubernetes |
| 146 | +``` |
| 147 | + |
| 148 | +Afterwards you can configure kubectl to use the new context: |
| 149 | + |
| 150 | +```sh |
| 151 | +kubectl config use-context colima-k3s |
| 152 | +``` |
0 commit comments