CI/CD Workflow for Spring Boot Application on Kubernetes via Skaffold
Create a CI/CD workflow for a SpringBoot Application deployed to Kubernetes using Skaffold. Find out more!
Join the DZone community and get the full member experience.Join For Free
It is a known fact that developing applications with Kubernetes is cumbersome. That is why an ecosystem is being developed around it so that developers can focus on what matters most to them, i.e., writing code. In this article, I will be covering another tool, Skaffold, developed by Google.
So What Exactly Is Skaffold?
Skaffold handles the workflow for building, pushing, and deploying your application. - Skaffold.dev
What Problem Is it Trying to Solve?
As I said earlier, developing applications with Kubernetes is not such an easy task. That's where Skaffold comes into play, as it eases the development and deployment of your applications running on Kubernetes. It manages the entire workflow, and you get instant feedback while developing and deploying your application locally or on a remote Kubernetes cluster.
Following are some of the benefits of using Skaffold.
It's is a client-side utility only, so there is no cluster set up or anything to maintain.
Easy to Share
It's effortless to share among your team members, as you only need to do the following to get started:
Ease of Development
To get your application containerized, even locally, you have to do a lot of things. For your local development, you might be doing something like building, pushing, and then deploying your application on Kubernetes. And there are a different set of commands (docker blah, kubectl blah blah, etc.) and the tools you use for each of these phases of your workflow. With Skaffold, you just have one magical command
skaffold run or skaffold dev and you are golden. This doesn't mean you don't depend on those tools that require you to build and deploy applications to Kubernetes. It's just that development is a lot easier when you just run a single command.
OK. Enough theory, let's get started.
Anatomy of the Spring Boot Application
In this tutorial, I will be using a Spring Boot application which, when accessed via the
/states REST endpoint shows Indian states and their capitals. This application uses an in-memory H2 database that inserts rows at the start of the application and keeps it in memory. The source code is available here.
For this demo, the following must be installed:
- Installing Skaffold.
- Installing Docker Desktop for Mac.
- Installing kubectl (Optional).
- Minikube (Optional).
I have used macOS, but you can use any OS you like or have experience working with. I have mentioned Minikube and kubetcl installation is optional because if you already have a Docker desktop installed, you can enable Kubernetes using the steps mentioned in my other blog post.
If you want to play around without going through the installation, then you can skip the above steps and use Google Cloud Shell instead, which provides a developer-ready environment from the browser. I have covered this at the end of the article.
Getting Started With Skaffold
To start with Skaffold, you need a
skaffold.yaml file. For that, we can run the below command.
However, you will be greeted with the following error message.
The error seems to be self-explanatory. It tells us that Skaffold is looking for either a Dockerfile or Jib configuration in the project.
Skaffold currently supports the following builders:
So to resolve this error, I will be adding the Jib Maven plugin in the pom.xml file. If you are wondering what Jib is and what it's used for, you can read my previous article. You can enable Jib support by copy/pasting the below content to the pom.xml file.
Enable Jib support
As per the official documentation:
skaffold initalso recognizes Maven and Gradle projects and will auto-suggest the
jibartifact detection is disabled by default, but can be enabled using the flag
skaffold init with
--XXenableJibInit. However, it will fail again with the below error.
Since we have not created Kubernetes manifests (deployment, pod, service, etc.) and there is a known issue with Skaffold, we will have to create them manually using the below kubectl command to resolve this error,
Creating a deployment:
Creating a service:
We have created both the deployment and the service now. Please make sure to copy the output of the above command to your YAML files in the
skaffold init --XXenableJibInit
skaffold.yaml file is created.
skaffold dev Command
We have now completed the required setup to start a continuous build and deployment of our Kubernetes application. Now we can simply run the below command to start our CI/CD workflow locally.
As you can see, the application is built and deployed to the local Kubernetes cluster now. We first have to check the
NodePort of our application using the
kubectl command to access the application locally.
NodePort assigned to our application is 30925. Let's invoke the
/states REST endpoint of our application and see what happens.
This looks great!
Let's do a small code change and see if Skaffold can re-trigger the entire workflow. I will just change the
replicas from 1 to 2 and in a deployment YAML file and see if Skaffold can redeploy the application with increased
Now check again with
replicaset for our application using
As you can see, after redeployment, the number of replicas has increased to 2, which was expected.
This is similar to
skaffold dev, but the main difference is that the workflow described in
skaffold.yaml is executed just once. This is recommended for your production workflow.
Using Cloud Shell
If you have all the required dependencies (Docker, Minikube, Skaffold) installed locally then it's fine; otherwise, you can skip the installation part as I have described above in the prerequisite section by using Google's Cloud Shell to replicate your local Kubernetes environment. Cloud Shell provides a browser-based terminal/CLI and editor and it comes with Skaffold, Minikube, and Docker pre-installed, and it's free. Just run the following commands and that's it. Make sure Docker and Minikube are up and running in the Cloud Shell environment.
git clone https://github.com/yrashish/indian-states
The following are the screenshots from Cloud Shell.
As you can see above, we were able to run our Spring Boot application with Cloud Shell also and got the expected output.
In this article, I have just covered a handful of features that Skaffold provides. There are many other features worth looking at, like port-forwarding for debugging, health checks, templating deployment configurations, etc. It is worth considering Skaffold for continuous deployment of your Kubernetes applications.
If you like what you just read, then you can buy me a coffee by clicking the link in the image below:
If you like what you just read, then you can read some of my previous articles.
Published at DZone with permission of Ashish Choudhary. See the original article here.
Opinions expressed by DZone contributors are their own.