Deploying Spring Microservice using Helm Charts
In this article, I will walk you through the process of deploying a Spring microservice on Kubernetes using Helm charts!
Here’s a step-by-step tutorial:
Prerequisites:
- A running Kubernetes cluster.
- Helm CLI installed on your local machine.
- A Spring Boot microservice with a Dockerfile.
Step 1: Create a Helm Chart for your Spring microservice
- Create a new directory for your Helm chart:
1 2 3 4 |
$ mkdir my-microservice-chart |
2. Change into the directory and create a Chart.yaml
file to define your chart:
1 2 3 4 5 |
$ cd my-microservice-chart $ touch Chart.yaml |
In the Chart.yaml
file, add the following lines to define your chart:
1 2 3 4 5 6 7 |
apiVersion: v2 name: my-microservice version: 0.1.0 description: A Helm chart for my Spring microservice |
3. Create a values.yaml
file to define the values that your chart will use. This file is where you will specify things like the Docker image name and version, the number of replicas, and any environment variables:
1 2 3 4 |
$ touch values.yaml |
4. Create a templates
directory to store the Kubernetes manifests that will be generated by Helm:
1 2 3 4 |
$ mkdir templates |
Step 2: Add Kubernetes manifests to your Helm Chart
- Create a
deployment.yaml
file in thetemplates
directory to define your Kubernetes Deployment:
1 2 3 4 |
$ touch templates/deployment.yaml |
In the deployment.yaml
file, add the following lines to define your Deployment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Release.Name }}-deployment labels: app: {{ .Release.Name }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app: {{ .Release.Name }} template: metadata: labels: app: {{ .Release.Name }} spec: containers: - name: {{ .Release.Name }} image: {{ .Values.image.repository }}:{{ .Values.image.tag }} imagePullPolicy: Always env: - name: SPRING_PROFILES_ACTIVE value: {{ .Values.env }} ports: - name: http containerPort: 8080 |
This Deployment will create a single Pod running your Spring microservice.
2. Create a service.yaml
file in the templates
directory to define your Kubernetes Service:
1 2 3 4 |
$ touch templates/service.yaml |
In the service.yaml
file, add the following lines to define your Service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
apiVersion: v1 kind: Service metadata: name: {{ .Release.Name }}-service labels: app: {{ .Release.Name }} spec: selector: app: {{ .Release.Name }} ports: - name: http port: 80 targetPort: http type: LoadBalancer |
This Service will expose your Spring microservice on port 80.
Step 3: Package and Deploy your Helm Chart
- Package your Helm chart into a
.tgz
file:
1 2 3 4 |
$ helm package . |
This will create a file called my-microservice-0.1.0.tgz
.
2. Deploy your Helm chart to your Kubernetes cluster:
1 2 3 4 |
$ helm install my-microservice ./my-microservice-0.1.0.tgz |
3. Check the status of your deployment:
1 2 3 4 |
$ kubectl get pods |
This should show you the Pod running your Spring microservice.
4. Check the status of your Service:
1 2 3 4 |
$ kubectl get services |
This should show you the Service exposing your Spring microservice on port 80.
5. Access your microservice:
1 2 3 4 |
$ kubectl port-forward svc/my-microservice-service 8080:80 |
This will forward traffic from your local port 8080 to the Service running on port 80.You can now access your Spring microservice by visiting http://localhost:8080
.
Step 4: Update your Helm Chart
- Update the
values.yaml
file with any changes you want to make. - Upgrade your Helm chart:
1 2 3 4 |
$ helm upgrade my-microservice ./my-microservice-0.1.0.tgz |
This will update your Deployment and Service with the new values.
3. Check the status of your deployment and Service again to make sure the changes were applied:
1 2 3 4 5 |
$ kubectl get pods $ kubectl get services |
Congratulations, you have successfully deployed a Spring microservice on Kubernetes using Helm charts!