I used Flux CD v2 to deploy Spring Boot to my EKS cluster

Marc Enschede
3 min readMay 31, 2021

Continuous Deployments rocks. And FluxCD rocks. As a hard-core Spring Boot fan I wanted to use Flux CD v2 to deploy a Spring Boot application on a EKS cluster. And I wanted to use GitHub Actions. Here’s what I found out.

Bootstap FluxCD on Kubernetes

First of all, I had to make sure Kubernetes and Flux were working. I used EKS on AWS and installed Flux on my laptop using brew. Then I had to load Flux v2 onto Kubernetes. I had to use the following commands

export GITHUB_TOKEN=******
export GITHUB_USER=enschede
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=scratch-infra \
--branch=main \
--path=app-cluster \
--personal

Of course, you should know your own GitHub username and a GitHub token can be obtained from GitHub.

The bootstrap command creates a repository scratch-infra on GitHub, loads some configuration files and starts Flux on Kubernetes. Flux is configured to have a look at the repo every minute for configuration changes in the app-cluster folder of the main branch in the repo.

After cloning the repo on my local laptop, it looked like this

It is good to note that the bootstap command is idempotent. You can repeat the command to load FluxCD to your Kubernetes cloud after you created a fresh one.

Adding an app

Now that I had FluxCD running on my cluster, I wanted of course to add an app. To do so, I had to add two configuration files to the repo.

The files had to contain the following content

Basically, both files are telling flux that there is a Kustomization file in the ./deployment folder in the master branch of the https://github.com/enschede/springbootflux repository. The Kustomization file has to be inspected every 30 seconds to see if it is updated.

(I guess it is clear that these files are to be committed and pushed to GitHub)

The Spring Boot app

Now that FluxCD know that something is going on in the springboorflux repo, I had to make sure to put this repo in place. It looks like this.

I guess the Spring Boot app as well a the Kubernetes deployment files familiar, so I will focus on the kustomization file.

This kustomization tell FluxCD that:

  • To override all namespaces to springbootflux-dev
  • To add -dev to all names in the Kubernetes definition files
  • To use the 6 filesmentioned under resources
  • To override the number of replica’s to 1
  • And to override the tag of the mentioned image to enschede/springbootflux

After committing and pushing this, the Spring Boot started.

Watching Kubernetes and FluxCD

Of course I wanted to know if the deployment went well. To do so I use 2 familiar commands:

kubectl get all -n springbootflux-dev 
watch flux get kustomizations

The deployment, replicaset, pod and configs were there.

Continues Deployments with GitHub Actions

Although the Spring Boot app was running on EKS, there was no Continues Deployment. So I added a small GitHub Action workflow.

The deploy.yml file contains the following workflow

In the last step, the kustomize command updates the kustomize file. The image tag is updated to ${{ github.sha }}. After doing so the change is committed and pushed. FluxCD can pick up this change and install the new docker image on the cluster!

Happy coding!

--

--

Marc Enschede

Java, Spring Boot and Kotlin developer. The backend guy!