Deploy your spring boot Application on Kubernetes

To deploy a Spring Boot REST service on Kubernetes, you can follow these steps

  1. Build a Docker image for your Spring Boot application. You can do this by creating a Dockerfile in the root directory of your project with the following contents:
FROM openjdk:11-jdk-slim
COPY target/your-application.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]

Then, run the following command to build the Docker image:

docker build -t your-image-name .
  1. Push the Docker image to a container registry, such as Docker Hub or Google Container Registry. You can do this by running the following command:
docker push your-image-name
  1. Create a Kubernetes deployment for your application. To do this, you'll need to create a deployment configuration file in YAML format. Here's an example configuration file that defines a deployment with one replica of your Docker image:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: your-app
  template:
    metadata:
      labels:
        app: your-app
    spec:
      containers:
      - name: your-app
        image: your-image-name
        ports:
        - containerPort: 8080

Save this file as deployment.yaml and apply it to your Kubernetes cluster using the kubectl command:

kubectl apply -f deployment.yaml
  1. Create a Kubernetes service for your application. This will expose the application's pod to other pods in the cluster and allow them to access it using a stable IP address and DNS name. To do this, you'll need to create a service configuration file in YAML format. Here's an example configuration file that defines a service that exposes your deployment using a NodePort:
apiVersion: v1
kind: Service
metadata:
  name: your-service
spec:
  type: NodePort
  selector:
    app: your-app
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP

Save this file as service.yaml and apply it to your Kubernetes cluster using the kubectl command:

kubectl apply -f service.yaml

Spring Boot REST service should now be deployed on Kubernetes and accessible from other pods in the cluster.