Building and Deploying Containerized Application to Kubernetes using Jib and Skaffold

Irvi Aini
Google Cloud - Community
4 min readAug 18, 2022

Jib allow Java/Kotlin developers to integrate the step of containerization using their tools of choice like Maven or Gradle by adding a plugin on the build script. Jib basically handles all of the step packaging your application into Dockerimage without using Dockerfile 🎉.

Above: Docker workflow. Bottom: Jib workflow [1]

So how exactly Jib do it?

Jib creates a set of filesystem layers that contain the Java runtime, your application, and all of its dependencies, as well as the metadata that the container engine uses to know how to start the Java Virtual Machine (JVM). We can see the following layer as follows when building a container using Jib:

  • Library dependencies defined in the build script (mvn or gradle). This layer contains the addition of all of the .jar files to the /app/libs folder.
  • Resource files. We can see /app/resources and all of the associated files from our resources folders.
  • Class files from your compiled application code. The .class files that came from the compile phase of the build are in this layer under /app/classes.
  • Java JVM argument files.

Depending on how your customize your build file, there can be other layers on the Jib generated lib as well, see the Jib FAQ.

In this article I’d like to share on how we can create a Java application using Dropwizard, Gradle, Jib, and Skaffold.

Directory setup for the project.

For this project, we will define all of the configuration needed to build an image in the artifact section. Other than that skaffold also support many tagging mechanism that you can use, for example INPUT_DIGEST which we will use in the example.

Skaffold configuration for the deployment.
The content of build.gradle script. As we take a look into it we can notice that it’s actually pretty similar to all of the things that need to be written inside of a Dockerfiile.

Note that to be able to push the image to the registry we can use a helper method as what is described in the Jib FAQ. Jib by default, however will take a look into the following places to see available credentials before pushing the image into container registry:

  • Docker credential file (as generated by docker login or podman login) at:
$XDG_RUNTIME_DIR/containers/auth.json, $XDG_CONFIG_HOME/containers/auth.json or $HOME/.config/containers/auth.json$DOCKER_CONFIG/config.json$HOME/.docker/config.json.

This is one of the configuration files for the docker or podman command line tool. See configuration files document, credential store and credential helper sections, and this for how to configure auth. For example, you can do docker login to save auth in config.json, but it is often recommended to configure a credential helper (also configurable in config.json).

If Jib was able to retrieve auth information from a Docker credential file, you should see a log message similar to Using credentials from Docker config (/home/myuser/.docker/config.json) where you can verify which credential file was picked up by Jib.

  • Jib configurations
Configuring credential helpers: <from/to><credHelper> (Maven) / from/to.credHelper (Gradle)
Specific credentials (not recommend): <from/to><auth><username>/<password> or in settings.xml (Maven) / from/to.auth.username/password (Gradle)
These parameters can also be set through properties: Maven / Gradle
Define the k8s context where we want to deploy the manifest on skaffold.yaml

While developing our application, we can also run the following command when we would like to test if the changes was made properly using Skaffold

Using port-forward to run the manifest
Skaffold architecture

Using skaffold we can also define a different build step for each environment that we have. For example we have different manifests for both of dev and prod environment, we can define it as follows:

deploy:
kubeContext: kind-kubernetes-days-id-demo
kustomize:
paths: [ "kubernetes/dev" ]
profiles:
- name: prod
deploy:
kustomize:
paths: [ "kubernetes/prod" ]
patches:
- op: replace
path: /build/tagPolicy
value:
inputDigest: { }
- op: remove
path: /build/artifacts/0/hooks

References:

[1] Google. (n.d.). Building java containers with jib | google cloud. Google. Retrieved August 18, 2022, from https://cloud.google.com/java/getting-started/jib

[2] Architecture and design. Skaffold. (n.d.). Retrieved August 18, 2022, from https://skaffold.dev/docs/design/

[3] Google. (n.d.). Introducing jib — build Java Docker Images better | google cloud blog. Google. Retrieved August 18, 2022, from https://cloud.google.com/blog/products/application-development/introducing-jib-build-java-docker-images-better

--

--

Irvi Aini
Google Cloud - Community

Machine Learning, Natural Language Processing, and Open Source.