Deploying Java on Kubernetes: A Basic Guide
Kubernetes (K8s) has become the de facto standard for orchestrating containerized applications. If you are a Java developer, knowing how to deploy your applications in Kubernetes is not just a plus, but a necessity in modern DevOps culture. In this guide, we will go through the complete path from creating a simple Java application to deploying it in a Kubernetes cluster. You will learn how to package an application into a Docker image, write the necessary manifests, and run it in a production-like environment.
1. Preparing the Java Application and Docker Image
Before deploying an application in Kubernetes, it needs to be containerized. Docker is the standard way to package Java applications with their dependencies. Let's consider a simple REST service on Spring Boot.
Let's create the HelloController.java class:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class HelloController {
@GetMapping("/") public String hello() { return "Hello from Java on Kubernetes!"; }}Now let's create a Dockerfile to build the image. We'll use a multi-stage build to minimize the size of the final image:
FROM maven:3.8.5-openjdk-17 AS buildWORKDIR /appCOPY pom.xml .COPY src ./srcRUN mvn clean package -DskipTests
FROM openjdk:17-jdk-slimWORKDIR /appCOPY --from=build /app/target/*.jar app.jarEXPOSE 8080ENTRYPOINT ["java", "-jar", "app.jar"]Build the image with the command docker build -t my-java-app:latest . and run it locally to verify: docker run -p 8080:8080 my-java-app. If everything works, you can upload the image to a registry (e.g., Docker Hub):
docker tag my-java-app:latest yourusername/my-java-app:latestdocker push yourusername/my-java-app:latest2. Basic Kubernetes Manifests
For deployment in Kubernetes, you will need at least two resources: Deployment (responsible for managing pods) and Service (provides network access).
Create the deployment.yaml file:
apiVersion: apps/v1kind: Deploymentmetadata: name: java-app-deployment labels: app: java-appspec: replicas: 2 selector: matchLabels: app: java-app template: metadata: labels: app: java-app spec: containers: - name: java-app image: yourusername/my-java-app:latest 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: 5Now create service.yaml to make the application accessible inside the cluster:
apiVersion: v1kind: Servicemetadata: name: java-app-servicespec: selector: app: java-app ports: - protocol: TCP port: 80 targetPort: 8080 type: ClusterIPIf you need to open access from outside (e.g., for testing), change the type to LoadBalancer or use NodePort.
3. Deploying in the Cluster
Make sure you have access to a Kubernetes cluster (e.g., local Minikube or a cloud service). Apply the manifests:
kubectl apply -f deployment.yamlkubectl apply -f service.yaml