#1
Integrating MQTT into your Micronaut application can streamline communication for IoT and real-time data processing. This guide walks you through setting up MQTT using Docker for development and Kubernetes for production, leveraging application.yml files tailored for each environment.

1. Adding MQTT and Kubernetes Dependencies

First, ensure your pom.xml includes the necessary dependencies:
<dependencies>
  <!-- Micronaut MQTT integration -->
  <dependency>
    <groupId>io.micronaut.mqtt</groupId>
    <artifactId>micronaut-mqtt-v3</artifactId>
  </>dependency>

  <!-- Micronaut Kubernetes client -->
  <dependency>
    <groupId>io.micronaut.kubernetes</groupId>
    <artifactId>micronaut-kubernetes-client</artifactId>
  </dependency>
</dependencies>
These dependencies enable MQTT communication and Kubernetes integration within your Micronaut application.

2. Setting Up MQTT Broker with Docker for Development

For development, you can use Eclipse Mosquitto as your MQTT broker. Run the following Docker command to start the broker:
docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
  • MQTT Broker: tcp://localhost:1883
  • WebSocket: ws://localhost:9001
This setup provides a local MQTT broker accessible for testing and development purposes.

3. Configuring application-dev.yml

Create a file named application-dev.yml in src/main/resources/ with the following content:
micronaut:
  application:
    name: mqtt-app

mqtt:
  client:
    server-uri: tcp://localhost:1883
    client-id: dev-client
    user-name: devuser
    password: devpass
    clean-session: true
    automatic-reconnect: true
    keep-alive-interval: 30
This configuration sets up the MQTT client to connect to the local broker with specified credentials and connection settings.
To run the application with the development profile:
./mvnw mn:run -Dmicronaut.environments=dev
Micronaut will automatically use the application-dev.yml file for configuration.

4. Preparing for Production with Kubernetes

In a production environment, it's advisable to manage sensitive information like MQTT credentials using Kubernetes Secrets and ConfigMaps.

4.1. Create Kubernetes Secret for MQTT Credentials

apiVersion: v1
kind: Secret
metadata:
  name: mqtt-secret
type: Opaque
data:
  MQTT_USERNAME: cHJvZHVzZXI=       # 'produser' base64 encoded
  MQTT_PASSWORD: cHJvZHBhc3M=       # 'prodpass' base64 encoded

4.2. Create Kubernetes ConfigMap for MQTT Server URI

apiVersion: v1
kind: ConfigMap
metadata:
  name: mqtt-config
data:
  MQTT_SERVER_URI: tcp://mqtt-broker:1883
These Kubernetes resources store your MQTT configuration securely and make them accessible to your application.

5. Configuring application-prod.yml

Create a file named application-prod.yml in src/main/resources/ with the following content:
micronaut:
  application:
    name: mqtt-app

mqtt:
  client:
    server-uri: ${MQTT_SERVER_URI}
    client-id: prod-client
    user-name: ${MQTT_USERNAME}
    password: ${MQTT_PASSWORD}
    clean-session: false
    automatic-reconnect: true
    keep-alive-interval: 60
This configuration uses environment variables provided by Kubernetes to set up the MQTT client for production.

6. Deploying Micronaut Application to Kubernetes

When deploying your Micronaut application to Kubernetes, ensure the environment variables are correctly set using the previously created Secret and ConfigMap:
env:
  - name: MICRONAUT_ENVIRONMENTS
    value: prod
  - name: MQTT_SERVER_URI
    valueFrom:
      configMapKeyRef:
        name: mqtt-config
        key: MQTT_SERVER_URI
  - name: MQTT_USERNAME
    valueFrom:
      secretKeyRef:
        name: mqtt-secret
        key: MQTT_USERNAME
  - name: MQTT_PASSWORD
    valueFrom:
      secretKeyRef:
        name: mqtt-secret
        key: MQTT_PASSWORD
This setup ensures that your application retrieves the necessary configuration from Kubernetes resources during deployment.
By separating your MQTT configuration into application-dev.yml and application-prod.yml, you can maintain clean and secure environments for development and production. Utilizing Docker for local development and Kubernetes for production allows for scalable and manageable deployments.
Remember to:
  • Use Docker to run a local MQTT broker during development.
  • Store sensitive information like credentials in Kubernetes Secrets.
  • Use ConfigMaps for non-sensitive configuration data.
  • Reference these Kubernetes resources in your application's environment variables.

This approach provides a robust and flexible setup for integrating MQTT into your Micronaut applications across different environments.


image quote pre code