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.

π§° 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:
|
1 2 3 4 5 6 |
mn create-app com.kscodes.micronaut.kubernetes.helloservice \ --build maven \ --jdk 17 |
π Project Overview
This will generate:
|
1 2 3 4 5 6 7 |
. βββ 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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
|
1 2 3 4 |
./mvnw clean package |
The JAR will be created at:
|
1 2 3 4 |
target/helloservice-0.1.jar |
π³ Step 4: Create Dockerfile
Add a Dockerfile:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
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
|
1 2 3 4 |
docker build -t micronaut-k8s-app . |
If you’re using Minikube, make sure Docker points to Minikube:
|
1 2 3 4 |
eval $(minikube docker-env) |
Then rebuild:
|
1 2 3 4 |
docker build -t micronaut-k8s-app . |
π Step 6: Write Kubernetes YAML Files
1. deployment.yaml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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
|
1 2 3 4 5 |
kubectl apply -f deployment.yaml kubectl apply -f service.yaml |
Check status:
|
1 2 3 4 5 |
kubectl get pods kubectl get svc |
π Step 8: Access the Service
If using Minikube:
|
1 2 3 4 |
minikube service micronaut-service |
Or manually:
|
1 2 3 4 |
curl http://localhost:30007/hello |
Expected response:
|
1 2 3 4 |
Hello from Micronaut on Kubernetes! |
π§ͺ Optional: Health Check Endpoint
Add to application.yml:
|
1 2 3 4 5 6 7 |
endpoints: health: enabled: true sensitive: false |
Micronaut will expose /health at:
|
1 2 3 4 |
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.