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.
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.