Deploying Java on Kubernetes: A Basic Guide for Beginners
\n\nModern 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\n1. Preparing the Java Application and Docker Image
\n\nLet's start with a simple Spring Boot application. Create a Dockerfile in the root directory of your project:
FROM openjdk:17-jdk-alpine\nWORKDIR /app\nCOPY target/my-app.jar app.jar\nEXPOSE 8080\nENTRYPOINT ["java", "-jar", "app.jar"]\n\nBuild the image and push it to a registry (e.g., Docker Hub):
\n\nmvn clean package -DskipTests\ndocker build -t myregistry/my-java-app:v1 .\ndocker push myregistry/my-java-app:v1\n\nMake sure the image works locally:
\n\ndocker run -p 8080:8080 myregistry/my-java-app:v1\n\n2. Basic Kubernetes Objects
\n\nTo 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
Creating a Deployment (deployment.yaml)
\n\napiVersion: 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\nCreating a Service (service.yaml)
\n\napiVersion: 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\n3. Deploying to a Kubernetes Cluster
\n\nApply the manifests to your cluster (make sure kubectl is configured):
kubectl apply -f deployment.yaml\nkubectl apply -f service.yaml\n\nCheck the status of pods and the service:
\n\nkubectl get pods\nkubectl get svc java-app-service\n\nFor external access (e.g., for testing), use port-forward:
\n\nkubectl port-forward service/java-app-service 8080:80\n\nNow the application is available at http://localhost:8080.
4. Scaling and Updating
\n\nKubernetes makes it easy to scale the application:
\n\nkubectl scale deployment java-app-deployment --replicas=5\n\nTo update the image (rolling update), change the tag in the Deployment and apply:
\n\nkubectl set image deployment/java-app-deployment java-app=myregistry/my-java-app:v2\nkubectl rollout status deployment/java-app-deployment\n\nIf necessary, roll back the changes:
\n\nkubectl rollout undo deployment/java-app-deployment\n\nConclusion
\n\nWe have covered the basic process of deploying a Java application on Kubernetes.