Mosquitto Broker in a Kubernetes envirnoment

Viewed 3818

I am working on bringing up Eclipse mosquitto broker docker image as a kubernetes container with the below YAML configuration. However, I am unable to have any sort of logging working for this broker, to enable some debugging. Is there a way to pass "command" to ask this docker image to the configuration file provided instead of using the default one ? Can anyone share a commonly used YAML file for starting the broker with persistance/volume/logging capabilities ?

 apiVersion: v1
 kind: Pod
 metadata:
   name: infra-pod
   labels:
     app: infra
 spec:
   containers:
     - name: mosquitto-broker
       image: eclipse-mosquitto
       ports:
         - containerPort: 1883
         - containerPort: 8883
2 Answers

Below you will find an example how volumes are mounted for mosquitto deployment. Please before you start trying them out visit kubernetes documents about volumes to understand a bit more how they are used and which one is suitable for your environment.

        volumeMounts:
        - name: mosquitto
          mountPath: /srv/mqtt/config
        - name: localtime
          mountPath: /etc/localtime
        - name: mosquitto-data
          mountPath: /srv/mqtt/data
        - name: mosquitto-log
          mountPath: /srv/mqtt/log
      volumes:
      - name: mosquitto
        persistentVolumeClaim:
          claimName: mosquitto
      - name: mosquitto-data
        persistentVolumeClaim:
          claimName: mosquitto-data
      - name: mosquitto-log
        persistentVolumeClaim:
          claimName: mosquitto-log
      - name: localtime
        hostPath:
          path: /home/test

To provide some custom config you have configure your pod to use Kubernetes configMap.What you are doing below is to add ConfigMap name under the volumes section of the Pod specification. This adds the ConfigMap data to the directory specified as volumeMounts.mountPath.

      volumeMounts:
      - name: password-file
        mountPath: /.config/mosquitto/auth/password_file.txt
        subPath: password_file.txt
      - name: config-file
        mountPath: /.config/mosquitto/mosquitto.conf
        subPath: mosquitto.conf 
   ----
      volumes:
      - name: config-file
        configMap:
          name: mosquitto-config
      - name: password-file
        configMap:
          name: mosquitto-password        
---

In the example above there is another field used called subPath which is used to mount specified file into pod directory. This is being used to avoid mouting volume and top of the existing directory. You can more about here.

Please note that those yamls are purely informational and serves as an example how to pass config and mount volumes. You will have to adjust them to you needs.

Thanks much Thomas for your detailed response. These concepts did help me in getting the config file to work in kubernetes environment. I mounted a volume of the hostpath into my container and the config file began working.. Will try out ConfigMaps in a week..

Pasting the YAML file below which make it work for me :

 apiVersion: v1
 kind: Pod
 metadata:
   name: mqtt-broker
   labels:
     app: ipc
 spec:
   containers:
     - name: mosquitto-broker
       image: eclipse-mosquitto
       ports:
         - containerPort: 1883
         - containerPort: 8883
       volumeMounts:
        - name: mosquitto
          mountPath: "/mosquitto/config"
        - name: mosquitto-data
          mountPath: "/mosquitto/data"
        - name: mosquitto-log
          mountPath: "/mosquitto/log"
   volumes:
      - name: mosquitto
        hostPath:
          path: /mosquitto/config
      - name: mosquitto-data
        hostPath:
          path: /mosquitto/data
      - name: mosquitto-log
        hostPath:
          path: /mosquitto/log
Related