Complete Guide to Deploying a Spring Boot Application on Kubernetes with Helm Chart
Introduction:
Deploying a Spring Boot application to Kubernetes can be a challenging task, especially for beginners. Luckily, Kubernetes provides a great tool called Helm Chart that makes it easier to deploy and manage complex applications. In this article, we’ll walk you through the process of deploying a Spring Boot application using Helm Chart.
Step 1: Setting Up Minikube
To begin, you’ll need to set up a local Kubernetes cluster using Minikube. This will allow you to test your application on a local environment before deploying it to a production environment. Follow these steps to set up Minikube:
Minikube is a lightweight Kubernetes distribution that can be run on a local machine. To install Minikube, follow the official installation guide.
Once Minikube is installed, start the cluster by running the following command:
minikube start
This command will start a single-node Kubernetes cluster on your local machine.
Step 2: Installing Helm
Once you have Minikube set up, you’ll need to install Helm, the package manager for Kubernetes.
Helm is a package manager for Kubernetes that makes it easy to install, upgrade, and manage applications on Kubernetes. To install Helm, follow the official installation guide.
Once Helm is installed, initialize the Helm client by running the following command:
helm init
This command will install Tiller, the server-side component of Helm, on your Kubernetes cluster.
Step 3: Creating a Sample Spring Boot Application
Next, we’ll create a sample Spring Boot application and package it as a Docker image. Here are the steps to create a sample Spring Boot application:
For this tutorial, we’ll create a simple Spring Boot application that exposes a REST API. If you already have a Spring Boot application, you can skip this step.
To create a new Spring Boot application, we’ll use the Spring Initializr. Open a web browser and navigate to the Spring Initializr homepage. Fill in the required fields to generate a new Spring Boot project. In this tutorial, we’ll use the following values:
- Project: Maven
- Language: Java
- Spring Boot: 2.5.2
- Group: com.example
- Artifact: springboot-helm
- Packaging: Jar
- Dependencies: Spring Web
Click the “Generate” button to generate the project. Extract the downloaded archive to a directory on your local machine.
Packaging the Spring Boot Application
Before we can deploy the Spring Boot application to Kubernetes, we need to package it as a Docker image. To do this, we’ll use the Dockerfile that was generated by the Spring Initializr.
Create a new file named Dockerfile in the root directory of the Spring Boot project with the following contents:
FROM openjdk:11
EXPOSE 8080
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
This Dockerfile uses the OpenJDK 11 base image, exposes port 8080, and sets the entrypoint to run the Spring Boot application.
Build the Docker image by running the following command:
docker build -t springboot-helm .
This command will build the Docker image and tag it with the name “springboot-helm”.
Step 4: Creating a Helm Chart and Deploying the Application
Now that we have a Docker image for the Spring Boot application, we can deploy it to Kubernetes using a Helm chart. In this section, we’ll create a Helm chart that deploys the Spring Boot application to Kubernetes.
- Create a new directory named
helm-charts
in the root directory of the Spring Boot project. - Create a new file named
Chart.yaml
in the helm-charts directory with the following contents:
apiVersion: v2
name: springboot-helm
- Create a new file named
values.yaml
in the helm-charts directory with the following contents:
image:
repository: springboot-helm
tag: latest
pullPolicy: IfNotPresent
service:
name: springboot-helm
type: NodePort
port: 8080
ingress:
enabled: false
replicaCount: 1
resources:
limits:
cpu: 100m
memory: 256Mi
requests:
cpu: 100m
memory: 256Mi
nodeSelector: {}
tolerations: []
affinity: {}
This file defines the values that will be used by the Helm chart to deploy the Spring Boot application to Kubernetes. It sets the Docker image repository to “springboot-helm
” and the service type to NodePort
.
- Create a new file named
templates.yaml
in the helm-charts directory with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "springboot-helm.fullname" . }}
labels:
app: {{ include "springboot-helm.name" . }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ include "springboot-helm.name" . }}
template:
metadata:
labels:
app: {{ include "springboot-helm.name" . }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 8080
resources:
{{ toYaml .Values.resources | indent 12 }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "springboot-helm.fullname" . }}
labels:
app: {{ include "springboot-helm.name" . }}
spec:
type: {{ .Values.service.type }}
ports:
- name: http
port: 8080
targetPort: 8080
selector:
app: {{ include "springboot-helm.name" . }}
This file defines the Kubernetes resources that will be deployed by the Helm chart. It creates a Deployment
and a Service
for the Spring Boot application.
- Install the Helm chart by running the following command:
helm install springboot-helm ./helm-charts
This command will install the Helm chart and deploy the Spring Boot application to Kubernetes.
- Verify that the application is running by running the following command:
minikube service springboot-helm --url
This command will output the URL of the Spring Boot application. Open a web browser and navigate to this URL to view the running application.
Conclusion
In this tutorial, we showed you how to deploy a Spring Boot application to Kubernetes using Helm. We started by setting up Minikube and Helm, then created a sample Spring Boot application and packaged it as a Docker image. Finally, we created a Helm chart and used it to deploy the Spring Boot application to Kubernetes.
Helm makes it easy to deploy and manage complex applications on Kubernetes, and Spring Boot is a great fit for deploying microservices on Kubernetes. By combining the two, you can create a powerful platform for building and deploying cloud-native applications.