How to Deploy Spring Boot Application in Kubernetes ?
Last Updated :
24 May, 2024
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.
Similar Reads
How To Deploy Python Application In Kubernetes ?
In today's IT world we are moving from the monolithic to microservice architecture to make our applications highly available and scalable to bring fault tolerance. In this transformation, containerization i.e., containerizing the application are a fundamental aspect of this micro services. In this a
6 min read
How to Deploy Node.js Application in Kubernetes?
Deploying the Node.js application on Kubernetes in a containerized manner makes it highly scalable, and fault-tolerant, and allows zero downtime. Kubernetes allows container orchestration that can be used in many ways during deployment. Load balancing is provided, which helps with automatic load tra
4 min read
How to Deploy Java Application in Kubernetes?
Kubernetes is a platform for automating deployment, scaling, and management of containerized applications. Pods are basic units that hold one or more containers. Deployments manage application deployment and updates. Services provide stable endpoints for accessing pods. Ingress manages external acce
9 min read
How To Deploy PHP Application On Kubernetes ?
Prologue to deploying a PHP applications on Kubernetes starts with understanding the containerization, where applications and their libraries are packaged into convenient, lightweight containers. Docker is generally utilized for this reason. Kubernetes then deals with these containers, abstracting a
9 min read
How to Run Spring Boot Application?
Spring Boot is built on the top of Spring and contains all the features of Spring. And it is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and set
8 min read
Deployment Of Spring Boot Application In Jenkins
In this article, we will be exploring how to automate the process of building Spring Boot applications by employing a polling trigger. We will also learn how to generate reports and store artifacts in the process. To achieve this, we will be leveraging the Jenkins server. Our goal is to create a pip
5 min read
How to Install Spring Boot Application in Hostinger?
Spring Boot is a popular platform for developing Java-based web applications, known for its robust features and ease of use. Hostinger offers high-quality, affordable hosting that is well-suited for Spring Boot applications. This article will guide you through deploying your Spring Boot application
4 min read
How to Set Context Path in Spring Boot Application?
The context path is a prefix to the URL path used to identify and differentiate between different context(s). In Spring Boot, by default, the applications are accessed by context path â/â. That means we can access the application directly at http://localhost:PORT/. For example http://localhost:8080/
6 min read
Spring Boot Application Deployment in Kubernetes with Jenkins CI/CD Pipeline
As we know in the modern world we use Continuous Integration and Continuous Deployment for fast and and efficient delivery of our application to the market. Here in this Article, we are deep-diving into how we can actually deploy Spring boot applications on Kubernetes with the help of Jenkins CI/CD.
14 min read
Deploying a React Application in Kubernetes
Kubernetes is an open-source free manager for your computer program. These programs can be in the containers. Each container holds a program and everything it needs to run. To Keep track of all these containers that contain your application this is where the Kubernetes role comes in. Kubernetes does
5 min read