Hike News
Hike News

MQTT K8s Setup on Ubuntu 20.04

Install the k8s cluster

1
2
microk8s enable dns storage helm3
microk8s status

Install the Mqtt helm chart

1
2
3
4
microk8s helm repo add truecharts https://charts.truecharts.org/
microk8s helm pull truecharts/mosquitto --version 8.0.11

microk8s helm install my-mosquitto truecharts/mosquitto --version 8.0.11

Check the status of the installed application.

1
2
3
4
5
microk8s status
microk8s kubectl show pods
microk8s kubectl
microk8s kubectl logs
microk8s kubectl describe pods

Service yaml file

We need to expose the service to the outside world.
Thankfully microk8s has a built in loadbalancer called metallb

Replace Y with the MQTT port number. Default 1883
Replace Z with the MQTT API number Default 9001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Service
metadata:
name: mqtt-service
spec:
type: LoadBalancer
selector:
app.kubernetes.io/name: mosquitto
ports:
- name: http
protocol: TCP
port: Y
targetPort: Y
- name: https
protocol: TCP
port: Z
targetPort: Z
externalIPs:
- X.X.X.X

More information here
https://medium.com/swlh/kubernetes-external-ip-service-type-5e5e9ad62fcd

Apply the service

1
microk8s kubectl apply -f ./mqtt-service.yaml 

Confirm the service is active

1
microk8s kubectl describe services mqtt-service

Local test client

You can test the connection locally on the server with this simple CLI mqtt client
Test client
https://mqttx.app/cli

Firewall Rules

This assumes you are using ufw.
ufw is bascally a wrapper for IPTABLES. If you have ever used IPTABLES before you understand why ufw exists.

1
2
3
sudo ufw default allow routed 
sudo ufw allow from X.X.X.0/X to any port Y proto tcp
sudo ufw status

Test external Connections

Once again try to connect to port Y with https://mqttx.app/cli

References

https://kubernetes.io/docs/concepts/services-networking/service/