How to Deploy Java Applications on Kubernetes Effectively

Learn how to deploy Java applications on Kubernetes using two methods: manual deployment with Docker and YAML manifests, or automated deployment with Devtron. This guide covers containerization, CI/CD pipelines, and Kubernetes configurations for scalable and reliable Java apps.

6 days ago   •   6 min read

By Siddhant Khisty
In this article

Java is a popular programming language that has been used for over two decades, and many industries are building and running their applications using Java, and its different frameworks such as SpringBoot, Maven, etc. It has become one of the most stable and reliable programming languages in the world and has a rich ecosystem of libraries, frameworks, and various development tools. Thanks to its extensible nature.

As all applications and business-critical workloads move to the cloud and Kubernetes, it is important to follow the deployment trends for existing Java applications to ensure that they can function and scale reliably. 

In this guide, we’ll walk you through both methods step by step and cover best practices for optimizing your deployment. Let’s dive in and make your Kubernetes deployment seamless!

  1. Devtron for Automated Deployment
  2. Using Kubernetes Manually

Fun Fact; According to various Kubernetes surveys and reports, more than 60% of Kubernetes deployments fail because of misconfigurations such as incorrect container image names, incorrect RBAC permissions, missing environment variables, or incorrect resource requests and limits.

💡
Master Kubernetes Deployments for Java – Take control with Devtron. Start Your Journey Today!

Deploying Java Applications on Kubernetes

Deploying a Java application to Kubernetes involves several steps. Let’s first review the overall process and then discuss the various steps in depth.

  1. Write and build the Java Applications
  2. Containerize the Java Application
  3. Push the container to a Container Registry such as DockerHub
  4. Create the required YAML Manifest for Kubernetes Resources
  5. Apply the YAML manifest to the Kubernetes clusters

Let’s dive deeper into all the required steps and deploy a simple Java application to Kubernetes.

Prerequisites

Before proceeding with the deployment process, please make sure that you have the following prerequisites

Once you have the prerequisites installed, you can proceed to write the Dockerfile and create the container image.

Method 1: Deploy Java applications with Devtron

Devtron is an end-to-end Kubernetes management solution that can help simplify the DevOps lifecycle right from the build stage, to deployments and beyond. Deploying a Java application, it can reduce a lot of the manual steps that we have seen above. Devtron will handle creating the Dockerfile, the Kubernetes manifests, building, deploying, and scanning all while following the best practices. 

Let’s take a look at how you can deploy the same Java application using Devtron.

💡
New to Kubernetes? Learn the Basics with Devtron’s Kubernetes Guide

Step 1: Create a Devtron application and add the Git Repository

  1. From Devtron’s home page, create a new Devtron application.
  2. Add the Git Repository containing the Java application code.
Create app in Devtron
[Fig.1] Create app in Devtron
💡
Check out the documentation to learn more about the application creation process.

Step 2: Configure the Build 

  1. Devtron will pull code from the repository and build the Docker container.
  2. You need to configure an OCI Container Registry.
  3. Choose from three build options:
    • Use an existing Dockerfile
    • Create a Dockerfile (using Devtron's template for Java applications)
    • Use Buildpacks
Create a Docker Image
[Fig.2] Create a Docker Image

Step 3: Deployment Configurations

  1. Devtron provides a pre-configured YAML template for Kubernetes deployment.
  2. Configure ingress, autoscalers, and other deployment settings.
Configure Deployment Manifest
[Fig.3] Configure Deployment Manifest

Step 4: Create the Build and Deploy Pipelines

  1. The CI pipeline will build the application and push the image to a registry.
  2. The CD pipeline will trigger deployments in the Kubernetes cluster.

Configure Pre and Post Stages (e.g., security scanning, unit testing).

 Configure Deployment Pipeline
[Fig.4] Configure Deployment Pipeline
💡
Please check the documentation to learn more about the pipeline configurations.

Step 5: Trigger the Build and Deployment Pipelines

  1. Select the Git branch and trigger the build stage.
  2. Once the build is complete, trigger the deployment stage.
  3. Devtron will deploy the application and show:
    • Deployment status
    • Application health
    • Kubernetes resource details
    • Security vulnerabilities
    • Rollback options in case of errors

Once the application is deployed, you will be able to see the application's health, deployment status, security vulnerabilities, the Kubernetes resources of the application, and more.

Build-and-Deploy
[Fig.5] Build and Deploy
💡
See Your Kubernetes Cluster Like Never Before – Achieve full-stack visibility with Devtron. Try It Today!

Method 2: Deploying Java Application Manually to Kubernetes

Step 1: Create the Dockerfile

A Dockerfile is a set of instructions to build a container image. Below is the Dockerfile to containerize your Java application:

########################### Build Container ###########################


# Base Image of Build Container
FROM gradle:4.7.0-jdk8-alpine AS build


# Changing the ownership of files and copying files in the container
COPY --chown=gradle:gradle . /home/gradle/src


# Moving into workdir
WORKDIR /home/gradle/src


# Compiling & building the code
RUN gradle build --no-daemon


########################### Prod Container ###########################


# Base Image for Prod Container
FROM openjdk:8-jre-slim


# Exposing Port of this container
EXPOSE 8080


# Creating a dir
RUN mkdir /app


# Copying only the jar files created before
COPY --from=build /home/gradle/src/build/libs/*.jar /app/my-app.jar


# Execute the Java executable
CMD exec java -jar /app/my-app.jar
💡
The openjdk image has now been deprecated. For the base image, you can use the ibmjava or eclipse-temurin image instead.

Step 2: Build and Push the Docker Image

Run the following command to build the Docker image:

docker build -t devtron/javaapp:v1 .

Push the image to DockerHub:

docker push devtron/javaapp

Step 3: Creating the Kubernetes Deployment and Service Manifests

Create a deployment.yaml file:

cat <<EOF | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
 name: java-deployment
spec:
 replicas: 3
 selector:
   matchLabels:
     app: java
 template:
   metadata:
     labels:
       app: java
   spec:
     containers:
     - name: java-container
       image: devtron/javaapp:v1
       ports:
       - containerPort: 8080
EOF

Create a service.yaml file:

cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Service
metadata:
 name: java-service
spec:
 selector:
   app: java
 ports:
   - protocol: TCP
     port: 80
     targetPort: 8080
 type: NodePort
EOF

Step 4: Deploy to Kubernetes

Run the following command to apply the manifests:

kubectl apply -f deployment.yaml service.yaml

Your Java application is now deployed to Kubernetes!

Common Challenges and Solutions

1. Container Image Size Management

  • Use Multi-Stage Builds to separate build and runtime environments.
  • Use Lightweight Base Images like Alpine or Distroless to reduce size.

2. Resource Management

  • Set Memory and CPU Limits to avoid overconsumption.
  • Implement Autoscaling (HPA) to handle varying workloads.

3. Deployment Strategies

  • Rolling Updates to ensure zero-downtime deployments.
  • Graceful Shutdown Handling to avoid breaking live traffic.

Conclusion

In this blog, we have explored the different steps that have to be taken for configuring and deploying a Java application to a Kubernetes cluster. To summarize, we saw:

  • How to build a Dockerfile for a Java application
  • Create the Kubernetes Manifest for the Java application
  • Deploy a Java application to Kubernetes
  • Simplify Java Application Deployments with Devtron

Devtron can help accelerate deployment velocity, while also ensuring that deployments are more reliable. For working with Kubernetes, Devtron can act as the single solution you need. 

Check out Devtron’s GitHub page, and start deploying applications to Kubernetes.

FAQ

What are the prerequisites for how to deploy java applications to kubernetes effectively?

You need a containerized Java application (using Docker or Buildpacks), a Kubernetes cluster (like AKS, EKS, or GKE), and a deployment strategy (Helm, Kustomize, or YAML manifests). Proper resource configuration, logging, and monitoring are essential for stability.

How can I troubleshoot issues during how to deploy java applications to kubernetes effectively?

Check pod logs (kubectl logs), inspect events (kubectl describe pod), verify resource limits, and use kubectl exec for debugging. Monitor application health with probes and Kubernetes dashboard tools.

What tools are recommended for how to deploy java applications to kubernetes effectively?

Recommended tools include Docker or Buildpacks for containerization, Helm or Kustomize for deployment management, and Prometheus with Grafana for monitoring. Use Skaffold or ArgoCD for CI/CD automation.

What are the best practices for how to deploy java applications to kubernetes effectively?

Use lightweight base images, configure resource limits, enable liveness and readiness probes, externalize configurations via ConfigMaps and Secrets, and implement rolling updates. Optimize for observability with structured logging and metrics.

Related articles

Spread the word

Keep reading