Tech Blog

Software, Technology

How to deploy MQTT broker to Kubernetes Cluster

By 42Gears Team3 min read
← Back to blog

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!!

Keep reading

You may be interested in

Browse all articles →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →
Angular constructor vs ngOnInit
September 15, 20223 min read

Angular constructor vs ngOnInit

Overview: If you are new to angular or have been working with it for a while, you will have questions like: Is it necessary to inject dependencies in the constructor and bind data? When should we use ngOnInit ? What is the difference between constructor and OnInit?. So, in this blog we will answer all […]

By Priyanka SRead More →
Angular – Difference between ng-container and ng-template
July 13, 20222 min read

Angular – Difference between ng-container and ng-template

Overview: ng-container and ng-template, both are angular elements which will work like containers with HTML elements, but with different purpose/use cases. Lets see what is the use of ng-container and ng-template below: ng-container: In some cases when you do not want to add an extra HTML element to the DOM , for example like div […]

By Priyanka SRead More →