Docker, Kubernetes, and GraalVM: Native Deployment

Docker, Kubernetes, and GraalVM: Native Deployment
1. Multi-Stage Docker: The "Clean" Build
Don't include your source code or Maven in your final image!
- Stage 1 (Build): Use a full JDK and Maven/Gradle to compile the app.
- Stage 2 (Run): Use a tiny "Alpine" Linux image with only a JRE (Java Runtime Environment). Result: You go from a 500MB image to a 100MB image, which is faster to push and more secure (hackers have fewer tools to use if they break in).
2. Kubernetes: The Orchestrator
Java and K8s are a match made in heaven, but you must tune them:
- Resource Limits: If you don't tell K8s about your memory, the JVM will try to use the "Whole Server," and K8s will kill it ($OOMKilled$).
- Liveness/Readiness Probes: Tell K8s exactly when your app is "Ready" to receive traffic. In Spring Boot, use Actuator to provide these endpoints automatically.
3. GraalVM Native Image (AOT)
This is the "Superpower" of modern Java. Instead of a JAR, GraalVM produces a Native Linux Binary.
- It performs "Points-to Analysis" to find every single piece of code your app actually uses.
- It deletes everything else (including 90% of the Java Standard Library).
- Start-up: $0.02$ seconds. (Perfect for AWS Lambda or scaling up for a sudden traffic surge).
4. The Cloud-Native Buildpack
You don't even need to write a Dockerfile anymore.
Using Buildpacks (integrated into Spring Boot Maven/Gradle plugins), you can just run:
./mvnw spring-boot:build-image
Spring will automatically handle the multi-stage build, the layer optimization, and even the GraalVM compilation for you.
Frequently Asked Questions
Is GraalVM always better? No. A JIT-compiled JAR is actually Faster for long-running, CPU-heavy tasks because it can optimize code based on live data. GraalVM (AOT) is better for Microservices and Serverless where start-up speed and small RAM usage are more important than peak throughput.
How do I debug a Native Image?
It's hard! You lose standard tools like jstack or jmap. You should always test your app as a "Normal JAR" first, and only use the Native Image for the final production deployment.
Key Takeaway
Java is no longer a "Server-room" dinosaur. By mastering Docker orchestration and GraalVM AOT compilation, you build Java systems that are as lean and fast as Go or Rust, while keeping the massive power and library ecosystem of the world's most popular enterprise language.
Read next: Java JUnit and Mockito: Building Bulletproof Tests →
Part of the Java Enterprise Mastery — engineering the cloud.
