How to Deploy Spring Boot Application in Kubernetes ?

The Spring Boot framework provides an assortment of pre-configured templates and tools to make the development of Java-based applications simpler. With little configuration, it enables developers to quickly design production-ready, stand-alone apps.

Kubernetes, commonly referred to as K8s, is an open-source platform meant to automate the deployment, scaling, and management of containerized applications. It provides an extensive architecture for operating microservices, providing features such as load balancing, service discovery, automated rollouts, and self-healing. Developers can take advantage of these capabilities to make sure their Spring Boot apps are scalable, feasible, and solid by deploying them to Kubernetes.

Step by Step to Deploy Spring Boot Application in Kubernetes

Step 1: Create the GitHub repository.

Step 2: Here is my spring boot GitHub repository.

Step 3: Create the manifest file for the Kubernetes. Here i have create the manifest file for the spring boot application.

Here is the complete manifest file for the spring application and connected to the mogo database.

apiVersion: apps/v1

kind: Deployment

metadata:

  name: springapp

  labels:

    app: springapp

spec:

  replicas: 1

  selector:

    matchLabels:

      app: springapp

  template:

    metadata:

      labels:

        app: springapp

    spec:

      containers:

      – name: springcon

        image: dockerhandson/spring-app-mongo:1

        ports:

        – containerPort: 8080

        resources:

          requests:

            cpu: 200m

            memory: 256Mi

          limits:

            cpu: 300m

            memory: 512Mi

        env:

        – name: MONGO_DB_HOSTNAME

          value: mongosvc

        – name: MONGO_DB_USERNAME

          value: devdb

        – name: MONGO_DB_PASSWORD

          value: devdb@123

apiVersion: v1

kind: Service

metadata:

  name: springsvc

spec:

  type: NodePort

  selector:

    app: springapp

  ports:

  – port: 80

    targetPort: 8080

apiVersion: apps/v1

kind: Deployment

metadata:

  name: mongo

spec:

  replicas: 1

  selector:

    matchLabels:

      app: mongo

  template:

    metadata:

      labels:

        app: mongo

    spec:

      containers:

      – name: mongocon

        image: mongo

        ports:

        – containerPort: 27017

        resources:

          requests:

            cpu: 200m

            memory: 256Mi

          limits:

            cpu: 300m

            memory: 512Mi

        env:

        – name: MONGO_INITDB_ROOT_USERNAME

          value: devdb

        – name: MONGO_INITDB_ROOT_PASSWORD

          value: devdb@123

apiVersion: v1

kind: Service

metadata:

  name: mongosvc

spec:

  type: ClusterIP

  selector:

    app: mongo

  ports:

  – port: 27017

    targetPort: 27017

Deployment Resource for Spring Boot Application:

  • apiVersion: apps/v1: Specifies the API version used to create this resource.
  • kind: Deployment: Defines the resource type as a Deployment, which manages the deployment of applications.
  • metadata: Contains metadata about the deployment, including the name (springapp) and labels.
  • spec: Describes the desired state of the Deployment.
  • replicas: 1: Specifies that one replica (or instance) of the application should run.
  • selector: Defines how to identify the Pods managed by this Deployment, matching labels.
  • template: Provides the Pod template used to create Pods.
  • metadata: Contains metadata for the Pods, including labels.
  • spec: Describes the Pod’s specification.
  • containers: Lists the containers that make up the Pod.
  • name: springcon: Names the container.
  • image: dockerhandson/spring-app-mongo:1: Specifies the container image to use.
  • ports: Lists the ports exposed by the container.
  • containerPort: 8080: Exposes port 8080.
  • resources: Specifies resource requests and limits.
  • requests: Minimum resources required.
  • cpu: 200m: 200 milliCPU (0.2 CPU).
  • memory: 256Mi: 256 MiB of memory.
  • limits: Maximum resources allowed.
  • cpu: 300m: 300 milliCPU (0.3 CPU).
  • memory: 512Mi: 512 MiB of memory.
  • env: Sets environment variables for the container.
  • MONGO_DB_HOSTNAME: Hostname of the MongoDB service (mongosvc).
  • MONGO_DB_USERNAME: Username for MongoDB (devdb).
  • MONGO_DB_PASSWORD: Password for MongoDB (devdb@123).

Service Resource for Spring Boot Application:

  • apiVersion: v1: Specifies the API version.
  • kind: Service: Defines the resource type as a Service.
  • metadata: Contains metadata about the service, including the name (springsvc).
  • spec: Describes the desired state of the Service.
  • type: NodePort: Exposes the service on each node’s IP at a static port.
  • selector: Defines how to identify the Pods targeted by this Service, matching labels.
  • ports: Lists the ports that the Service exposes.
  • port: 80: The port on the Service.
  • targetPort: 8080: The port on the Pod to forward traffic to.

Deployment Resource for MongoDB:

  • apiVersion: apps/v1: Specifies the API version.
  • kind: Deployment: Defines the resource type as a Deployment.
  • metadata: Contains metadata about the deployment, including the name (mongo).
  • spec: Describes the desired state of the Deployment.
  • replicas: 1: Specifies that one replica (or instance) of the MongoDB container should run.
  • selector: Defines how to identify the Pods managed by this Deployment, matching labels.
  • template: Provides the Pod template used to create Pods.
  • metadata: Contains metadata for the Pods, including labels.
  • spec: Describes the Pod’s specification.
  • containers: Lists the containers that make up the Pod.
  • name: mongocon: Names the container.
  • image: mongo: Specifies the container image to use.
  • ports: Lists the ports exposed by the container.
  • containerPort: 27017: Exposes port 27017.
  • resources: Specifies resource requests and limits.
  • requests: Minimum resources required.
  • cpu: 200m: 200 milliCPU (0.2 CPU).
  • memory: 256Mi: 256 MiB of memory.
  • limits: Maximum resources allowed.
  • cpu: 300m: 300 milliCPU (0.3 CPU).
  • memory: 512Mi: 512 MiB of memory.
  • env: Sets environment variables for the container.
  • MONGO_INITDB_ROOT_USERNAME: Root username for MongoDB (devdb).
  • MONGO_INITDB_ROOT_PASSWORD: Root password for MongoDB (devdb@123).

Service Resource for MongoDB:

  • apiVersion: v1: Specifies the API version.
  • kind: Service: Defines the resource type as a Service.
  • metadata: Contains metadata about the service, including the name (mongosvc).
  • spec: Describes the desired state of the Service.
  • type: ClusterIP: Exposes the service on a cluster-internal IP. Only accessible within the cluster.
  • selector: Defines how to identify the Pods targeted by this Service, matching labels.
  • ports: Lists the ports that the Service exposes.
  • port: 27017: The port on the Service.
  • targetPort: 27017: The port on the Pod to forward traffic to.

Spring Boot Deployment: Deploys a single instance of a Spring Boot application, exposing port 8080, and configured to communicate with MongoDB using environment variables.

Spring Boot Service: Exposes the Spring Boot application using a NodePort service, making it accessible outside the cluster.

MongoDB Deployment: Deploys a single instance of MongoDB, exposing port 27017, and configured with initial root credentials.

MongoDB Service: Exposes the MongoDB instance using a ClusterIP service, making it accessible within the cluster.

Step 4: Here is my pods for the Kubernetes for the spring application and mongo database. Refer the below image for your reference.

kubectl get pods

Step 5: Here is the services of the both deployments.

kubectl get svc

Step 6: Here are the deployments of the both applications that is spring app and mongo database.

kubectl get deployments

Step 7: Here is the all services after applying the manifest file in the kubernetes.

kubectl get all

Step 8: Access the spring boot application.

Step 9: Insert the data into data base using the spring boot application. Here we have inserted the sample data into the mogo db databse refre the below image for your reference.

Conclusion

Deploying a Spring Boot application on Kubernetes simplifies the process of managing and scaling microservices. By leveraging Kubernetes’ robust infrastructure, we ensure that our application is resilient, scalable, and easy to manage. The integration with MongoDB demonstrates how Kubernetes can effectively handle data persistence alongside application deployment. This step-by-step guide showcases the essential steps and configurations required, providing a comprehensive foundation for deploying Java-based applications in a modern, cloud-native environment.

Deploy Spring Boot Application in Kubernetes – FAQs

How to deploy Spring Boot application on internet?

To deploy a Spring Boot application on the internet, you can host it on a cloud platform like AWS, Google Cloud, or Azure, and expose it using Kubernetes with a NodePort or LoadBalancer service type. Ensure you configure proper domain and DNS settings for internet access.

How do I deploy in Spring Boot?

Deploy a Spring Boot application by packaging it into a JAR file using mvn package or gradle build, then run it with java -jar <your-app>.jar. For Kubernetes deployment, create and apply the necessary deployment and service YAML files using kubectl apply -f <file>.yaml.

How do I Containerise a Spring Boot application?

Containerize a Spring Boot application by creating a Dockerfile that specifies the application’s runtime environment and dependencies, then build the Docker image using the docker build command.

How to use Kubernetes secrets in Spring Boot?

In Spring Boot, you can use Kubernetes secrets by configuring them as environment variables or by using the Kubernetes client to fetch them programmatically. This allows you to securely access sensitive information, such as database credentials, within your application.

How to deploy Spring Boot application in Kubernetes locally?

To deploy a Spring Boot application in Kubernetes locally, you can use tools like Minikube or Docker Desktop with Kubernetes enabled. Simply build Docker images for your Spring Boot application, create Kubernetes deployment and service YAML files, and apply them to your local Kubernetes cluster.