CI/CD for Spring Boot Application on Kubernetes

  • Get the code Repo from Version Control
  • Run/test the App
  • Dockerize the App
  • Test using simple Docker Run
  • Deploy to Kubernetes
  • Setup CI/CD
## Note: for quick testing you can run all these commands on online free minikube
## https://kubernetes.io/docs/tutorials/hello-minikube/
## In online minikube there is a plus button to access ports in browser.
## Use that feature to see the app in browser after running

## Deploy Spring Boot App

git clone https://github.com/spring-guides/gs-spring-boot.git

cd gs-spring-boot/complete


## create the JAR deployable for the application

./mvnw -DskipTests package


## create the container image and push it to the Container Registry.
## you can skip exporting these , if you are just using dockerhub

export DTR=registry.example.com
export USER=foo
export DTR_TOKEN=bar

## skip this if you are logiging in to dockerhub instead
docker login -u $USER -P $DTR_TOEKN $DTR


./mvnw -DskipTests com.google.cloud.tools:jib-maven-plugin:build \
  -Dimage=$DTR/$USER/hello-java:v1


 ##  test the image locally with the following command:

 docker run -ti --rm -p 8080:8080 \
  $DTR/$USER/hello-java:v1


### Once you verify that the app is running fine locally in a Docker container
### you can stop the running container by pressing Ctrl+C.


### Deploy on K8s
## note: make your image public or use imagepullsecret for k8s deployment

kubectl run hello-java \
  --image=$DTR/$USER/hello-java:v1 \
  --port=8080

## check your app is running

kubectl get deployments
kubectl get pods

## allow external traffic
## must use nodeport

kubectl expose deployment hello-java --type=NodePort


kubectl get svc

### test access the service

http://<CLUSTER_NODE_IP>:<nodePort>

### you can further do some testing by scaling up and down the number replicas.

kubectl scale deployment hello-java --replicas=3

At this point your app is up and running on Kubernetes. Next we will take this , and put it into a CI tool to setup a CI/CD pipeline , so that you can roll out new versions , and rollback when needed.

Leave a comment

Your email address will not be published. Required fields are marked *