Deploying Java Applications on Kubernetes: Step-by-Step 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

Deploying Java Applications on Kubernetes: From Dockerfile to a Working Service

\n\n

Modern Java development is unthinkable without containerization and orchestration. Kubernetes (K8s) has become the standard for managing microservices, and the ability to deploy Java applications in a cluster is a mandatory skill for a backend developer. In this article, we will walk through the complete deployment cycle in practice: from building a Docker image with a Java application to configuring load balancing via Ingress.

\n\n

1. Preparing the Java Application and Dockerfile

\n\n

Before getting into Kubernetes, your Java application must be packaged into a Docker image. Let's consider a classic case — a Spring Boot application built into a JAR file.

\n\n

Building the Project

\n

Suppose we have a project that is built using Maven. Run the command in the project root:

\n
mvn clean package -DskipTests
\n

As a result, the file my-app-1.0.0.jar will appear in the target/ folder.

\n\n

Creating a Dockerfile

\n

To minimize the image size and increase security, we use a multi-stage build.

\n
# Stage 1: build\nFROM maven:3.8.6-openjdk-17 AS builder\nWORKDIR /app\nCOPY pom.xml .\nRUN mvn dependency:go-offline -B\nCOPY src ./src\nRUN mvn package -DskipTests\n\n# Stage 2: final image\nFROM openjdk:17-jdk-slim\nWORKDIR /app\nCOPY --from=builder /app/target/my-app-1.0.0.jar app.jar\nEXPOSE 8080\nENTRYPOINT ["java", "-jar", "app.jar"]
\n\n

Building and Publishing the Image

\n
docker build -t my-java-app:1.0.0 .\ndocker tag my-java-app:1.0.0 your-dockerhub/my-java-app:1.0.0\ndocker push your-dockerhub/my-java-app:1.0.0
\n\n

2. Creating Kubernetes Manifests

\n\n

Kubernetes uses a declarative approach. We will describe the desired state of the system in YAML files.

\n\n

Deployment — Managing Pods

\n

Create a file deployment.yaml:

\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: java-app-deployment\n  labels:\n    app: java-app\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: java-app\n  template:\n    metadata:\n      labels:\n        app: java-app\n    spec:\n      containers:\n      - name: java-app\n        image: your-dockerhub/my-java-app:1.0.0\n        ports:\n        - containerPort: 8080\n        resources:\n          requests:\n            memory: "256Mi"\n            cpu: "250m"\n          limits:\n            memory: "512Mi"\n            cpu: "500m"\n        livenessProbe:\n          httpGet:\n            path: /actuator/health\n            port: 8080\n          initialDelaySeconds: 30\n          periodSeconds: 10\n        readinessProbe:\n          httpGet:\n            path: /actuator/health\n            port: 8080\n          initialDelaySeconds: 20\n          periodSeconds: 5
\n\n

Service — Providing Access

\n

Create a file service.yaml:

\n
apiVersion: v1\nkind: Service\nmetadata:\n  name: java-app-service\nspec:\n  selector:\n    app: java-app\n  ports:\n  - protocol: TCP\n    port: 80\n    targetPort: 8080\n  type: ClusterIP
\n\n

ConfigMap — Externalizing Configuration

\n

To store environment variables, create configmap.yaml:

\n
apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: app-config\ndata:\n  SPRING_PROFILES_ACTIVE: "prod"\n  DB_URL: "jdbc:postgresql://postgres-service:5432/mydb"
\n\n

3. Deploying to the Cluster and Verification

\n\n

Now let's apply all manifests to the cluster. Make sure you have kubectl configured and are connected to the correct context.

\n\n

Applying Manifests

\n
kubectl apply -f configmap.yaml\nkubectl apply -f deployment.yaml\nkubectl apply -f service.yaml
\n\n

Checking Status

\n
kubectl get pods\nkubectl get deployments\nkubectl get services
\n

You should

Blogs

Book Recommendations