#1
When building applications with Micronaut, integrating object storage like MinIO can enhance your system's capabilities, especially for handling large files or backups. This guide walks you through configuring MinIO in a Micronaut application using Docker for development and Kubernetes for production, utilizing profile-based application.yml files for clean environment separation.

1. Add Necessary Dependencies

First, ensure your pom.xml includes the required dependencies:
<dependencies>
  <!-- Micronaut Object Storage for S3-compatible APIs -->
  <dependency>
    <groupId>io.micronaut.objectstorage</groupId>
    <artifactId>micronaut-object-storage-aws</artifactId>
  </dependency>

  <!-- YAML configuration support -->
  <dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-runtime</artifactId>
  </dependency>

  <!-- Kubernetes integration -->
  <dependency>
    <groupId>io.micronaut.kubernetes</groupId>
    <artifactId>micronaut-kubernetes-client</artifactId>
  </dependency>
</dependencies>
These dependencies enable S3-compatible object storage support, YAML-based configuration, and Kubernetes integration in your Micronaut application.

2. Start MinIO Using Docker for Development

For local development, you can run MinIO using Docker:
docker run -p 9000:9000 -p 9001:9001 \
  -e "MINIO_ROOT_USER=devuser" \
  -e "MINIO_ROOT_PASSWORD=devpass123" \
  -v minio-data:/data \
  minio/minio server /data --console-address ":9001"
This command starts MinIO with a web console accessible at http://localhost:9001 and the API at http://localhost:9000.

3. Configure application-dev.yml for Development

Create a file named application-dev.yml in src/main/resources/ with the following content:
micronaut:
  application:
    name: minio-app
  object-storage:
    default:
      provider: aws
      region: us-east-1
      access-key-id: devuser
      secret-access-key: devpass123
      endpoint: http://localhost:9000
      path-style-access: true
This configuration sets up the object storage client to connect to your local MinIO instance using the AWS S3-compatible API.
To run the application with the development profile:
./mvnw mn:run -Dmicronaut.environments=dev

4. Configure application-prod.yml for Production with Kubernetes

For production, especially when deploying to Kubernetes, it's best to avoid hardcoding sensitive information. Instead, use environment variables or Kubernetes Secrets and ConfigMaps.
Create a file named application-prod.yml in src/main/resources/:
micronaut:
  application:
    name: minio-app
  object-storage:
    default:
      provider: aws
      region: us-east-1
      access-key-id: ${MINIO_ACCESS_KEY}
      secret-access-key: ${MINIO_SECRET_KEY}
      endpoint: ${MINIO_ENDPOINT}
      path-style-access: true
In your Kubernetes deployment, you can set these environment variables using a ConfigMap and Secret:

ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: minio-config
data:
  MINIO_ENDPOINT: http://minio-service:9000

Secret:

apiVersion: v1
kind: Secret
metadata:
  name: minio-secret
type: Opaque
data:
  MINIO_ACCESS_KEY: cHJvZHVzZXI=  # base64 for 'produser'
  MINIO_SECRET_KEY: cHJvZHBhc3MxMjM=  # base64 for 'prodpass123'

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: minio-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: minio-app
  template:
    metadata:
      labels:
        app: minio-app
    spec:
      containers:
        - name: minio-app
          image: your-docker-image
          env:
            - name: MINIO_ENDPOINT
              valueFrom:
                configMapKeyRef:
                  name: minio-config
                  key: MINIO_ENDPOINT
            - name: MINIO_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: minio-secret
                  key: MINIO_ACCESS_KEY
            - name: MINIO_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: minio-secret
                  key: MINIO_SECRET_KEY
This setup ensures that your application retrieves the necessary credentials and endpoint information securely from Kubernetes resources.

5. Running the Application in Production

When deploying to Kubernetes, ensure that the MICRONAUT_ENVIRONMENTS environment variable is set to prod so that Micronaut picks up the correct configuration:
env:
  - name: MICRONAUT_ENVIRONMENTS
    value: prod
This tells Micronaut to use the application-prod.yml configuration file.
By separating your configurations into application-dev.yml and application-prod.yml, you can manage environment-specific settings cleanly and securely. Utilizing Docker for local development and Kubernetes for production allows for scalable and maintainable deployments. Micronaut's support for environment profiles and integration with Kubernetes makes it straightforward to manage these configurations effectively.
Note: Always ensure that sensitive information, such as access keys and secrets, are managed securely using appropriate tools and practices, especially in production environments.

image quote pre code