Init Container Build Pattern: Knative build with plain old Kubernetes deployment
With Kubernetes evolving at supersonic speed and seeing a lot of adoption in the enterprise world, the developer community is now looking for solutions to common Kubernetes problems, such as patterns. In this article, I will explore a new Kubernetes pattern using Init Containers.
Let’s start with the use case that gave birth to this problem: Quarkus—Supersonic and Subatomic Java—has excited the Java developer community with its amazing speed and all new native build artifact for Java applications. As one of those excited developers, I want to quickly build and deploy a Quarkus application on to Kubernetes.
Before we deploy the Quarkus application on Kubernetes, however, we need to solve the following problems:
- How to build a container image natively on Kubernetes.
- How to make the Kubernetes deployment wait for build image before deploying the application.
Analyzing these problems, we see that they do not apply only to this use case. These are common issues for developers who want to build and deploy Java or other applications on Kubernetes.
So, what is the solution? Let’s address them one by one:
Everything you need to grow your career.
With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.SIGN UP
How to build a container image natively on Kubernetes
At this point, Kubernetes does not provide any out-of-the-box way to build applications natively on Kubernetes, that is, something like kubectl build myapp. This made us look for something that can do a build on Kubernetes; fortunately, we have Knative Build that does the work for us.
“What? Knative build? I’m not running a serverless application.” That’s a usual reaction from developers when we talk about Knative build. To be clear, Knative build is not just for the serverless world; it can be used with any Kubernetes cluster without the need for any other Knative components and Istio. With just Knative build installed in a Kubernetes cluster, the builds can be run to make Linux container images out of the given source code.
How to make the Kubernetes deployment wait for build image before deploying the application
The Kubernetes deployment has to wait for the build to complete before starting to deploy itself. The solution to this problem is to use Init Containers. Adding an init container to the Kubernetes deployment makes the pod’s containers start in a sequence based on the completion state of the previous container in the chain.
If one of the init containers fails, then the whole deployment is deemed failed with the init container being restarted. The following diagram explains how the pattern works inside Kubernetes.
For example, here is a Kubernetes deployment using init containers (see line #31-36):
You can find a complete end-to-end example here and instructions on how to get a Quarkus application built and deployed to your Kubernetes cluster. My colleagues Roland Huß and Bilgin Ibryam have written an excellent book, called Kubernetes Patterns, which explains many patterns that are applicable to everyday cloud-native application development.
If you want to learn more about Knative basics, check out our multi-hour detailed Knative tutorial, which offers demonstrations and explains concepts in a simple and easy way.
To learn more, visit our Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets (e.g. containers), books (e.g. microservices), and product downloads that can help you with your microservices and/or container application development.