|
| 1 | +--- |
| 2 | +description: In this guide, you create Kafka topics and use Zilla to mediate MQTT broker messages onto those topics. |
| 3 | +--- |
| 4 | + |
| 5 | +# Running an MQTT Kafka broker |
| 6 | + |
| 7 | +In this guide, you create Kafka topics and use Zilla to mediate MQTT broker messages onto those topics. |
| 8 | + |
| 9 | +Specifically, you will: |
| 10 | + |
| 11 | +[Verify prerequisites](#prerequisites) to run this guide. |
| 12 | +[Install and run](#tl-dr) Zilla with Kafka or use your own. |
| 13 | +[Create topics](#check-the-kafka-topics) for the MQTT broker messages. |
| 14 | +[Watch Kafka](#listen-for-messages) for new messages on the topics. |
| 15 | +[Pub & Sub](#send-a-greeting) with an MQTT client. |
| 16 | + |
| 17 | +## Tl;Dr |
| 18 | + |
| 19 | +Download and run the Zilla [zilla-examples/mqtt.kafka.broker](https://github.com/aklivity/zilla-examples/tree/main/mqtt.kafka.broker) example using this install script. It will start Zilla and everything you need for this guide. |
| 20 | + |
| 21 | +```bash:no-line-numbers |
| 22 | +wget -qO- https://raw.githubusercontent.com/aklivity/zilla-examples/main/startup.sh | sh -s -- mqtt.kafka.broker |
| 23 | +``` |
| 24 | + |
| 25 | +::: note |
| 26 | +Alternatively, download [mqtt.kafka.broker](https://github.com/aklivity/zilla-examples/releases/latest/download/mqtt.kafka.broker.tar.gz) or the [startup.sh](https://github.com/aklivity/zilla-examples/releases/latest/download/startup.sh) script yourself. |
| 27 | +::: |
| 28 | + |
| 29 | +### Prerequisites |
| 30 | + |
| 31 | +Before proceeding, you should have [Compose](https://docs.docker.com/compose/gettingstarted/) or optionally [Helm](https://helm.sh/docs/intro/install/) and [Kubernetes](https://kubernetes.io/docs/tasks/tools/) installed. |
| 32 | + |
| 33 | +::: details Detailed prerequisites |
| 34 | + |
| 35 | +- A connection to the internet |
| 36 | +- Docker version 1.13.0+ or later is installed and running |
| 37 | +- Docker Desktop or Docker Desktop for Windows on WSL 2 |
| 38 | +- Container host resources: 1 CPU, 1GB memory |
| 39 | + |
| 40 | +Optional: |
| 41 | + |
| 42 | +- Kafka 3.0+ hosted with the Docker network allowed to communicate |
| 43 | +- Helm 3.0+ |
| 44 | +- Kubernetes 1.13.0+ |
| 45 | + |
| 46 | +::: |
| 47 | + |
| 48 | +### Check the Kafka topics |
| 49 | + |
| 50 | +Run the docker command under the `Verify the Kafka topics created` section of the script output. Verify these topics are listed. Read more on the data in these topics in [the overview](../../concepts/kafka-proxies/mqtt-proxy.html#step-2-pub-sub-message-reflect-with-kafka). |
| 51 | + |
| 52 | +```output:no-line-numbers |
| 53 | +mqtt-messages |
| 54 | +mqtt-retained |
| 55 | +mqtt-sessions |
| 56 | +``` |
| 57 | + |
| 58 | +### Listen for messages |
| 59 | + |
| 60 | +Run the docker command under the `Start a topic consumer to listen for messages` section of the script output. If you didn't use your own Kafka, you can also see all the topics in the [Kafka UI](http://localhost:8080/ui/clusters/local/all-topics). |
| 61 | + |
| 62 | +### Send a greeting |
| 63 | + |
| 64 | +Using [eclipse-mosquitto](https://hub.docker.com/_/eclipse-mosquitto) subscribe to the `zilla` topic. |
| 65 | + |
| 66 | +```bash:no-line-numbers |
| 67 | +docker run -it --rm eclipse-mosquitto \ |
| 68 | +mosquitto_sub -V 'mqttv5' --topic 'zilla' \ |
| 69 | +--host 'host.docker.internal' --port 7183 --debug |
| 70 | +``` |
| 71 | + |
| 72 | +In a separate session, publish a message on the `zilla` topic. |
| 73 | + |
| 74 | +```bash:no-line-numbers |
| 75 | +docker run -it --rm eclipse-mosquitto \ |
| 76 | +mosquitto_pub -V 'mqttv5' --topic 'zilla' --message 'Hello, world' \ |
| 77 | +--host 'host.docker.internal' --port 7183 --debug --insecure |
| 78 | +``` |
| 79 | + |
| 80 | +Send messages with the retained flag. |
| 81 | + |
| 82 | +```bash:no-line-numbers |
| 83 | +docker run -it --rm eclipse-mosquitto \ |
| 84 | +mosquitto_pub -V 'mqttv5' --topic 'zilla' --message 'Hello, retained' --retain \ |
| 85 | +--host 'host.docker.internal' --port 7183 --debug --insecure |
| 86 | +``` |
| 87 | + |
| 88 | +Then restart the `mosquitto_sub` above. The latest retained message is delivered, and the other messages are not. |
| 89 | + |
| 90 | +## Creating this example yourself |
| 91 | + |
| 92 | +### Start a Kafka instance |
| 93 | + |
| 94 | +You can use your own Kafka or set up a local Kafka with [resource.kafka.compose](https://github.com/aklivity/zilla-examples/releases/latest/download/resource.kafka.compose.tar.gz) and follow the setup instructions in the `README.md`. |
| 95 | + |
| 96 | +Export these env variables or overwrite them with your remote Kafka if you skipped the local setup. |
| 97 | + |
| 98 | +```output:no-line-numbers |
| 99 | +export KAFKA_HOST=host.docker.internal |
| 100 | +export KAFKA_PORT=29092 |
| 101 | +``` |
| 102 | + |
| 103 | +### Bootstrap Kafka |
| 104 | + |
| 105 | +Create these topics in the Kafka environment. |
| 106 | + |
| 107 | +```bash:no-line-numbers |
| 108 | +\ |
| 109 | +/bin/kafka-topics.sh --bootstrap-server $KAFKA_HOST:$KAFKA_PORT --create --if-not-exists --topic mqtt-sessions |
| 110 | +/bin/kafka-topics.sh --bootstrap-server $KAFKA_HOST:$KAFKA_PORT --create --if-not-exists --topic mqtt-messages --config cleanup.policy=compact |
| 111 | +/bin/kafka-topics.sh --bootstrap-server $KAFKA_HOST:$KAFKA_PORT --create --if-not-exists --topic mqtt-retained --config cleanup.policy=compact |
| 112 | +``` |
| 113 | + |
| 114 | +### Create your config |
| 115 | + |
| 116 | +Create a new file called `zilla.yaml` and append the below yaml to it. |
| 117 | + |
| 118 | +### Entrypoint |
| 119 | + |
| 120 | +This will configure Zilla for accepting all of the `mqtt` traffic. |
| 121 | + |
| 122 | +```yaml{14-16} |
| 123 | +<!-- @include: ./mqtt_kafka_broker_zilla.yaml{-20} --> |
| 124 | +``` |
| 125 | + |
| 126 | +::: right |
| 127 | +[More on binding-tcp](../../reference/config/bindings/binding-tcp.md) |
| 128 | +[More on binding-mqtt](../../reference/config/bindings/binding-mqtt.md) |
| 129 | +::: |
| 130 | + |
| 131 | +### Service definition |
| 132 | + |
| 133 | +The service definitions will define how the clients using this service will interact with Kafka through Zilla. |
| 134 | + |
| 135 | +```yaml{7-9} |
| 136 | +<!-- @include: ./mqtt_kafka_broker_zilla.yaml{22-31} --> |
| 137 | +``` |
| 138 | + |
| 139 | +::: right |
| 140 | +[More on binding-mqtt-kafka](../../reference/config/bindings/binding-mqtt-kafka.md) |
| 141 | +[More on topic data](../../concepts/kafka-proxies/mqtt-proxy.html#step-2-pub-sub-message-reflect-with-kafka) |
| 142 | +::: |
| 143 | + |
| 144 | +### Add a Kafka sync layer |
| 145 | + |
| 146 | +The Zilla [cache_client and cache_server](../../reference/config/bindings/binding-kafka.html#kind) helps manage the smooth data transfer between the service definition and Kafka. |
| 147 | + |
| 148 | +```yaml{11-12} |
| 149 | +<!-- @include: ./mqtt_kafka_broker_zilla.yaml{33-46} --> |
| 150 | +``` |
| 151 | + |
| 152 | +::: right |
| 153 | +[More on binding-kafka cache](../../reference/config/bindings/binding-kafka.md#cache-behavior) |
| 154 | +::: |
| 155 | + |
| 156 | +### Point to a Running Kafka instance |
| 157 | + |
| 158 | +This will define the location and connection for Zilla to communicate with Kafka. |
| 159 | + |
| 160 | +```yaml{10-11} |
| 161 | +<!-- @include: ./mqtt_kafka_broker_zilla.yaml{47-} --> |
| 162 | +``` |
| 163 | + |
| 164 | +::: details Full zilla.yaml |
| 165 | + |
| 166 | +```yaml |
| 167 | +<!-- @include: ./mqtt_kafka_broker_zilla.yaml --> |
| 168 | +``` |
| 169 | +
|
| 170 | +::: |
| 171 | +
|
| 172 | +::: right |
| 173 | +[More on binding-kafka client](../../reference/config/bindings/binding-kafka.md#client-behavior) |
| 174 | +::: |
| 175 | +
|
| 176 | +### Start Zilla |
| 177 | +
|
| 178 | +With your `zilla.yaml` config, follow the [Zilla install instructions](../install.md) using your method of choice. Set the necessary Kafka environment variables. |
| 179 | + |
| 180 | +::: code-tabs#bash |
| 181 | + |
| 182 | +@tab Docker |
| 183 | + |
| 184 | +```bash:no-line-numbers |
| 185 | +--env KAFKA_HOST="$KAFKA_HOST" --env KAFKA_PORT="$KAFKA_PORT" |
| 186 | +``` |
| 187 | + |
| 188 | +@tab Helm values.yaml |
| 189 | + |
| 190 | +```yaml:no-line-numbers |
| 191 | +# use the values from $KAFKA_HOST $KAFKA_PORT variables |
| 192 | +env: |
| 193 | + - name: KAFKA_HOST |
| 194 | + value: "\"host.docker.internal\"" |
| 195 | + - name: KAFKA_PORT |
| 196 | + value: "\"29092\"" |
| 197 | +``` |
| 198 | + |
| 199 | +::: |
| 200 | + |
| 201 | +### Adding TLS |
| 202 | + |
| 203 | +You can add TLS to this broker by adding a vault and tls binding as described in the [Server Encryption](../../concepts/config-intro.md#server-encryption-tls-ssl) section. |
| 204 | + |
| 205 | +## Remove the running containers |
| 206 | + |
| 207 | +Find the path to the `teardown.sh` script(s) in the `use the teardown script(s) to clean up` section of the example output and run it. If you didn't provide an external Kafka endpoint, there will be scripts for both Zilla and the local Kafka installs. |
0 commit comments