Skaffold with Quarkus and Kubernetes
Suppose you are testing a set of Quarkus based microservices. They run as Docker images inside a Kubernetes cluster on your local machine, e.g. using MiniKube or Rancher Desktop. At some point you find a bug in one of the services, or you find that you need an extra logging in code. Now you would like to quickly modify the code and redeploy it in your Kubernetes cluster. Typically your Docker image is build using a build pipeline on a build server, which may take several minutes. Can this be done any faster and easier? With Skaffold you can!
Skaffold
Skaffold is an open source tool from Google to quickly deploy your application as a Docker image in your Kubernetes cluster. With a single command it builds the Docker image on the fly and deploys it into your cluster.
Setup
When you create a Quarkus project it already provides a Dockerfile. Basically building a Docker image can therefor be done like this:
./gradle assemble
docker build -f src/main/docker/Dockerfile.jvm
This is usually taken care of by the build pipeline of your build server, which also publishes the image to a Docker registry.
To run the services in Kubernetes you create/update the configmaps.yml
(to override application.properties
) and <my.service.name>.yml
deployment descriptor files (see for example Kubernetes Crash Course for Absolute Beginners).
Configure Skaffold
Now repeat the set of parameters from the <my.service.name>.yml
file als quarkus.kubernetes.*
parameters in your application’s application.properties
file.
See the following pages on how to do that:
Next you also need to set up a skaffold.yml
file in the module’s root directory.
The following file can be used as a template:
apiVersion: skaffold/v2beta1
kind: Config
metadata:
name: <my.service.name>
build:
artifacts:
- image: <docker.registry.image.url>
docker :
dockerfile : src/main/docker/Dockerfile.jvm
context : .
deploy:
kubectl:
manifests:
- build/kubernetes/kubernetes.json
The <docker.registry.image.url>
is the same as in <my.service.name>.yml
.
Run
Now you are all set. Once you finished updating the code and want to quickly redeploy your service into your cluster run:
./gradle assemble
skaffold run --tail
This builds the new binary and deploys it into your cluster in a matter of seconds!