Using Micronaut with Kubernetes (K8s)

Kubernetes (K8s) is the standard for deploying and managing containerized applications in the cloud. Micronaut, with its fast startup, small memory footprint, and easy cloud integration, is a great match for Kubernetes.

In this post, youโ€™ll learn everything about Using Micronaut with Kubernetes:

  • Create a Micronaut app
  • Package it with Docker
  • Write Kubernetes YAML files
  • Deploy to a K8s cluster
  • Access the service via NodePort

We will use the package com.kscodes.micronaut.kubernetes throughout.

Using Micronaut with Kubernetes

๐Ÿงฐ What You Need

  • Java 17+
  • Docker installed
  • Minikube or a local K8s cluster (for testing)
  • Maven 3.6+
  • kubectl installed
  • Micronaut CLI (optional)

๐Ÿ“ฆ Step 1: Create Micronaut App

Generate a Micronaut app with Maven:


mn create-app com.kscodes.micronaut.kubernetes.helloservice \
  --build maven \
  --jdk 17

๐Ÿ“ Project Overview

This will generate:


.
โ”œโ”€โ”€ src/main/java/com/kscodes/micronaut/kubernetes/HelloController.java
โ”œโ”€โ”€ src/main/resources/application.yml
โ”œโ”€โ”€ pom.xml

๐Ÿ‘จโ€๐Ÿ’ป Step 2: Add a Simple Controller

File: HelloController.java


package com.kscodes.micronaut.kubernetes;

import io.micronaut.http.annotation.*;

@Controller("/hello")
public class HelloController {

    @Get
    public String greet() {
        return "Hello from Micronaut on Kubernetes!";
    }
}

โš™๏ธ Step 3: Build JAR with Maven

Run:


./mvnw clean package

The JAR will be created at:


target/helloservice-0.1.jar

๐Ÿณ Step 4: Create Dockerfile

Add a Dockerfile:


FROM eclipse-temurin:17-jdk-jammy

WORKDIR /app

COPY target/helloservice-*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

๐Ÿ— Step 5: Build Docker Image


docker build -t micronaut-k8s-app .

If you’re using Minikube, make sure Docker points to Minikube:


eval $(minikube docker-env)

Then rebuild:


docker build -t micronaut-k8s-app .

๐Ÿ“„ Step 6: Write Kubernetes YAML Files

1. deployment.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: micronaut-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: micronaut-app
  template:
    metadata:
      labels:
        app: micronaut-app
    spec:
      containers:
        - name: micronaut-container
          image: micronaut-k8s-app
          ports:
            - containerPort: 8080

2. service.yaml


apiVersion: v1
kind: Service
metadata:
  name: micronaut-service
spec:
  type: NodePort
  selector:
    app: micronaut-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30007

๐Ÿš€ Step 7: Deploy to Kubernetes


kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Check status:


kubectl get pods
kubectl get svc

๐ŸŒ Step 8: Access the Service

If using Minikube:


minikube service micronaut-service

Or manually:


curl http://localhost:30007/hello

Expected response:


Hello from Micronaut on Kubernetes!

๐Ÿงช Optional: Health Check Endpoint

Add to application.yml:


endpoints:
  health:
    enabled: true
    sensitive: false

Micronaut will expose /health at:


http://localhost:30007/health

๐Ÿ“š External Resources

โœ… Conclusion

Using Micronaut with Kubernetes allows you to build scalable, fast, and cloud-ready Java apps. With Micronautโ€™s small footprint and Kubernetesโ€™ power, your services can deploy quickly and reliably.

By following this guide with Maven and the custom package com.kscodes.micronaut.kubernetes, youโ€™re set up for both local testing and production deployment in the cloud.