Hello Guys! In this blog I have tried to explain how we can deploy an MQTT broker to Kubernetes cluster. To begin with this we need a Kubernetes cluster setup prior.
What is deployment and service in Kubernetes?
A deployment is responsible for managing a set of pods and keep them running. A service provides the network access to a set of pods. We can use a deployment without any service when there is no need to access the pods via any network.
The most basic unit in the Kubernetes is a pod.
Image
For deploying an MQTT broker to Kubernetes we need a docker image. So for this we are going to use Community edition of “Hivemq” broker image available at docker hub.
Now to deploy this broker to Kubernetes we need to deploy this image to Kubernetes and then add an external service which will make it possible to access this broker or server from outside the cluster.
So we need two YAML files , one for making deployment of Kubernetes broker and other for adding an external service. The YAML files are provided below.
Deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: broker-depl-name
labels:
app: myappbroker
spec:
replicas: 1
selector:
matchLabels:
app: myappbroker
template:
metadata:
labels:
app: myappbroker
spec:
containers:
- name: broker-container
image: hivemq/hivemq-ce
resources:
limits:
memory: "0.6Gi"
cpu: "300m"
requests:
memory: "0.2Gi"
cpu: "150m"
ports:
- containerPort: 1883
Service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service-broker
spec:
selector:
app: myappbroker
type: LoadBalancer
ports:
- port: 1883
targetPort: 1883
nodePort: 30005
Now if the kubernetes cluster is set up and kubectl is configured to the cluster run the following commands :
kubectl apply Deployment.yaml
kubectl apply Service.yaml
First command will create a deployment of MQTT broker i.e. after first command the MQTT broker will be up and running but to get a usable link to access the broker or server we need to make a service using second command.
Getting usable end link
After running these commands successfully run “ kubectl get service” , this command will list the services present in the Kubernetes cluster. The external ip against our service will be shown and that is the ip address of our broker.
This completes the process of deploying MQTT broker to Kubernetes. In this blog we have learnt how to deploy MQTT broker to Kubernetes. It should be noted that broker is nothing but a server, so in the same way we can deploy any server to Kubernetes.
I hope it helped!!