Deploying Spring Microservice on Kubernetes
Spring Boot is a popular framework for building microservices, while Kubernetes is a widely used container orchestration platform. In this article, we will discuss how to deploy a Spring microservice on Kubernetes.
Prerequisites:
- Basic knowledge of Spring Boot and Kubernetes
- Docker installed on your machine
- Access to a Kubernetes cluster
Step 1
Build the Spring Boot Application First, we need to build the Spring Boot application as a Docker image. In the project directory, create a Dockerfile with the following contents:
1 2 3 4 5 6 7 |
FROM openjdk:11-jre-slim ADD target/my-app.jar my-app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "my-app.jar"] |
Here, we are using the OpenJDK 11 runtime image, adding the Spring Boot JAR file to the image, exposing port 8080, and setting the entry point to run the JAR file.
Next, build the Docker image using the following command:
1 2 3 4 |
docker build -t my-app . |
This will create a Docker image with the tag my-app
.
Step 2
Push the Docker image to a container registry Before we can deploy the Docker image to Kubernetes, we need to push it to a container registry such as Docker Hub or Google Container Registry. Make sure you are logged in to the registry and then run the following command:
1 2 3 4 |
docker push my-app |
Step 3
Create a Kubernetes deployment In Kubernetes, a deployment manages a set of replicas of a containerized application. Create a deployment YAML file with the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 1 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: my-app ports: - containerPort: 8080 |
This YAML file specifies that we want to create a deployment called my-app
with one replica, selecting containers with the label app: my-app
. The template specifies that we want to create a container with the image my-app
and expose port 8080.
Run the following command to create the deployment:
1 2 3 4 |
kubectl apply -f deployment.yaml |
Step 4
Create a Kubernetes service A Kubernetes service is used to expose a deployment internally or externally. We need to create a service to allow other services to communicate with our Spring microservice. Create a service YAML file with the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
apiVersion: v1 kind: Service metadata: name: my-app spec: selector: app: my-app ports: - name: http port: 8080 targetPort: 8080 type: ClusterIP |
This YAML file specifies that we want to create a service called my-app
, selecting containers with the label app: my-app
, and exposing port 8080 internally.
Run the following command to create the service:
1 2 3 4 |
kubectl apply -f service.yaml |
Step 5
Access the Spring microservice We can now access the Spring microservice using the service IP address and port. Run the following command to get the service IP address:
1 2 3 4 5 |
kubectl get services |
Look for the my-app
service and note down the CLUSTER-IP
address.
To access the microservice, send a GET request to the IP address and port:
1 2 3 4 |
curl http://<CLUSTER-IP>:8080/ |
This should return the response from the Spring microservice.