Kubernetes Kustomize Tutorial¶
- You can find the source code for this video in my GitHub Repo.
Unless you use Kubernetes for your personal projects or some kind of POC, you need to be able to customize Kubernetes configuration files such deployments, services, ingresses, etc., to a specific environment. For example, if you expose an application to the internet using ingress, you definitely need to update DNS for different environments. Due to the fact that there are so many projects out there, and some of them are widely used, such as Helm, the problem is still considered to be not completely solved.
One approach to configuration reuse is simply copy-paste. However, as with code, it makes it harder to benefit from ongoing improvements to the source.
Another approach to reuse is to express the source material as a parameterized template. A tool is processing the template executing any embedded scripting, and replacing parameters with desired values to generate the configuration. The challenge here is that the templates and value files are not the specification of Kubernetes API resources. They are a necessary new thing, a new language that wraps the Kubernetes API. The problem here with Helm is that different teams want different changes. So almost every specification that you include in the YAML file becomes a parameter that needs a value.
And finally, a template-free approach. Since the Kubernetes configuration is expressed as JSON or YAML, it's relatively easy to target individual keys and merge them with environment-specific values.
That's where kustomize comes into play. It's native to Kubernetes, a template-free way to customize Kubernetes objects. Kustomize has the concept of bases and overlays. A base is a directory with a kustomization.yaml file, which contains a set of Kubernetes resources such as deployments, configMaps, services, etc., and associated customization. Overlays directories contain environment-specific changes. It may be a name prefix, replica count, a new image tag, or other changes.
Kustomization uses an industry-standard declarative approach that can be described in YAML and stored in git. Since it's template free, it expresses the full power of Kubernetes API, with no need to parameterize every single line compared to Helm. It can be used for both bespoke (build in-house apps) and off-the-shelf applications such as Redis, Kafka, Postgres, and other open-source projects. Kustomize supports bulk management operations: creation, deletion, and updates. It is built into kubectl, so you can create and update resources using the kubectl apply -k command and similar command to delete. Since Kustomize is provided as a standalone golang package and cli tool as well as it is built into kubectl, it's easy to integrate with user's tools and workflows.
Convert Deployment to Kustomize Base¶
Kustomize secret generator¶
Kustomize image name or tag¶