Deploying Java on Kubernetes: A Practical Basic Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Kubernetes for a Java Developer?

Modern Java development is increasingly moving beyond simply running JAR files on a server. Microservice architecture, the need for horizontal scaling, and fault tolerance lead to the use of containers and orchestrators. Kubernetes (K8s) has become the de facto standard for managing containerized applications. In this guide, we will walk through how to deploy a typical Java application (e.g., on Spring Boot) to a Kubernetes cluster. You will learn how to package the application into a Docker image, create the necessary manifests, and run it.



1. Preparing the Java Application and Building the Docker Image

Before getting into Kubernetes, the Java application must be packaged into a container. Let's look at a simple example using Spring Boot.



1.1. Example Java Application

Let's create a minimal REST controller that will return the application version:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication@RestControllerpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }

@GetMapping("/") public String home() { return "Java App running on Kubernetes v1.0"; }}


1.2. Creating a Dockerfile

For Java applications, it is optimal to use a multi-stage build to reduce the size of the final image:

FROM maven:3.8.6-eclipse-temurin-17 AS builderWORKDIR /appCOPY pom.xml .RUN mvn dependency:go-offline -BCOPY src ./srcRUN mvn package -DskipTests

FROM eclipse-temurin:17-jre-alpineWORKDIR /appCOPY --from=builder /app/target/*.jar app.jarEXPOSE 8080ENTRYPOINT ["java", "-jar", "app.jar"]


1.3. Building and Publishing the Image

Build the image and push it to a registry (e.g., Docker Hub):

docker build -t yourusername/java-k8s-demo:1.0 .docker push yourusername/java-k8s-demo:1.0


2. Basic Kubernetes Objects for Deploying Java

To deploy a Java application, you will need at least two resources: a Deployment (manages replicas and updates) and a Service (provides network access).



2.1. Deployment Manifest

Create a file deployment.yaml:

apiVersion: apps/v1kind: Deploymentmetadata:  name: java-app  labels:    app: java-appspec:  replicas: 2  selector:    matchLabels:      app: java-app  template:    metadata:      labels:        app: java-app    spec:      containers:      - name: java-app        image: yourusername/java-k8s-demo:1.0        ports:        - containerPort: 8080        resources:          requests:            memory: "256Mi"            cpu: "250m"          limits:            memory: "512Mi"            cpu: "500m"        livenessProbe:          httpGet:            path: /actuator/health            port: 8080          initialDelaySeconds: 30          periodSeconds: 10        readinessProbe:          httpGet:            path: /actuator/health            port: 8080          initialDelaySeconds: 20          periodSeconds: 5

Important: For Spring Boot, use Actuator for health checks. Specify requests and limits so that Kubernetes distributes resources correctly.



2.2. Service Manifest

Create service.yaml for internal access to the pods:

apiVersion: v1kind: Servicemetadata:  name: java-app-servicespec:  selector:    app: java-app  ports:    - protocol: TCP      port                

Blogs

Book Recommendations