Deploying Java on Kubernetes: A Basic Guide for Beginners

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 on Kubernetes: A Basic Guide for Beginners

\n\n

Modern Java development is unthinkable without containerization and orchestration. Kubernetes (K8s) has become the de facto standard for managing containerized applications. In this guide, we will walk through the process of deploying a simple Java application to a Kubernetes cluster step by step. You will learn how to package your code into a Docker image, create the necessary manifests, and run the application in a production-like environment.

\n\n

1. Preparing the Java Application and Docker Image

\n\n

Let's start with a simple Spring Boot application. Create a Dockerfile in the root directory of your project:

\n\n
FROM openjdk:17-jdk-alpine\nWORKDIR /app\nCOPY target/my-app.jar app.jar\nEXPOSE 8080\nENTRYPOINT ["java", "-jar", "app.jar"]
\n\n

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

\n\n
mvn clean package -DskipTests\ndocker build -t myregistry/my-java-app:v1 .\ndocker push myregistry/my-java-app:v1
\n\n

Make sure the image works locally:

\n\n
docker run -p 8080:8080 myregistry/my-java-app:v1
\n\n

2. Basic Kubernetes Objects

\n\n

To deploy a Java application, we need three key resources:

\n\n
    \n
  • Deployment — manages replicas and updates
  • \n
  • Service — provides network access to pods
  • \n
  • Ingress — (optional) external traffic routing
  • \n
\n\n

Creating a Deployment (deployment.yaml)

\n\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: java-app-deployment\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: myregistry/my-java-app:v1\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

Creating a Service (service.yaml)

\n\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

3. Deploying to a Kubernetes Cluster

\n\n

Apply the manifests to your cluster (make sure kubectl is configured):

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

Check the status of pods and the service:

\n\n
kubectl get pods\nkubectl get svc java-app-service
\n\n

For external access (e.g., for testing), use port-forward:

\n\n
kubectl port-forward service/java-app-service 8080:80
\n\n

Now the application is available at http://localhost:8080.

\n\n

4. Scaling and Updating

\n\n

Kubernetes makes it easy to scale the application:

\n\n
kubectl scale deployment java-app-deployment --replicas=5
\n\n

To update the image (rolling update), change the tag in the Deployment and apply:

\n\n
kubectl set image deployment/java-app-deployment java-app=myregistry/my-java-app:v2\nkubectl rollout status deployment/java-app-deployment
\n\n

If necessary, roll back the changes:

\n\n
kubectl rollout undo deployment/java-app-deployment
\n\n

Conclusion

\n\n

We have covered the basic process of deploying a Java application on Kubernetes.

Blogs

Book Recommendations