How to setup Mosquitto MQTT Broker in Kubernetes

Solution 1:

mosquitto/configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mosquitto-config
data:
  mosquitto.conf: |-
    # Ip/hostname to listen to.
    # If not given, will listen on all interfaces
    #bind_address

    # Port to use for the default listener.
    port 1883

    # Allow anonymous users to connect?
    # If not, the password file should be created
    allow_anonymous true

    # The password file.
    # Use the `mosquitto_passwd` utility.
    # If TLS is not compiled, plaintext "username:password" lines bay be used
    # password_file /mosquitto/config/passwd

mosquitto/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mosquitto
spec:
  selector:
    matchLabels:
      app: mosquitto
  template:
    metadata:
      labels:
        app: mosquitto
    spec:
      containers:
      - name: mosquitto
        image: eclipse-mosquitto:2.0
        resources:
          requests:
            cpu: "50m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 1883
        volumeMounts:
            - name: mosquitto-config
              mountPath: /mosquitto/config/mosquitto.conf
              subPath: mosquitto.conf
      volumes:
        - name: mosquitto-config
          configMap:
            name: mosquitto-config

mosquitto/service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mosquitto
spec:
  selector:
    app: mosquitto
  ports:
  - port: 1883
    targetPort: 1883

now:

$ kubectl apply -f mosquitto/
$ kubectl get pods,deployments,services -o wide
$ kubectl logs -l app=gateway-bridge -f --all-containers