Yet another topic to learn for GitOps. So far we have seen some important topics you should know on Kubernetes, now its time to know about another interesting tool.
What is Kustomize?
Kustomize is an increasingly popular tool for managing Kubernetes manifests. Rather than using templates, as Helm does, Kustomize works by building on existing manifests. Using this pattern, it provides various features including resource namespacing, modification of metadata, and generation of Kubernetes Secrets — all without editing the source original manifests.
To start using Kustomize you just need one or more Kubernetes manifests and a Kustomization file called kustomization.yaml. The Kustomization file is itself a manifest, which specifies a list of resources, patches to apply, and various other options.
Patches
Manifests that give complete Kubertnetes resources are listed in the Kustomization as resources.
Partial manifests can also be used as strategic merge patches, where they are combined with existing resources with matching metadata to add or edit fields. This can be a convenient way to add large blocks of YAML to a resource, as the patch file just looks like a normal YAML manifest.
JSON patches can also be used in a similar way, but with a more concise syntax that’s better suited to small changes.
Overlays
If you like to modify or create more variants, for that you can use overlays. Overlays also contain a kustomization.yaml file and can include new resource manifests, or patches for existing resources.
An overlay Kustomization must specify one or more bases. These bases must be directories containing Kustomizations which the overlay can build on. Overlays can also specify other overlays as bases, allowing them to be stacked.
Where Kustomize requires?
You may ask, as there is Helm and other tools, we have why we need Kustomize. Here will see where it uses and how it useful. Let’s consider, your team using a Helm chart from an external organization or provider. As it is external one, we should modify as per our organization needs. How we can do? We need to either fork their repo and need to make all our changes and should apply to our cluster. After a sometime you need to upgrade to latest version or any reason, like new version or security patching or some interesting new feature adaptation. In this case, to leverage those requirements, you have to fork the new Helm chart and re-apply your configuration changes. This may add more complexity to manage the changes and also adds risk in your application stability.
Here Kustomize comes to picture, it enables you to do that by creating a file that ties everything together, or optionally includes “overrides” for individual parameters. With that you can keep the changes in single files which helps to maintain it with versioning in GIT.
Here you can see where the kustomize places in your continuous deployment pipeline, with this you can enable the GIT events or GitOps. With this you can keep Helm to generate the yaml files and Kustomize to patch environment specific values based on the events. For example, you can maintain a file with changes specific to branches, like if ‘master’ branch triggers, apply to Production environment.
Comparison
Before we check further let’s compare Kustomize to native Helm and native Kubectl to better highlight the differentiated functionality that it offers.
Benefits of Using Kustomize
Reusability
Kustomize allows you to reuse one base file across all of your environments (development, staging, production) and then overlay unique specifications for each.
Fast Generation
Since Kustomize has no templating language, you can use standard YAML to quickly declare your configurations.
Easier to Debug
YAML itself is easy to understand and debug when things go wrong. Pair that with the fact that your configurations are isolated in patches, and you’ll be able to triangulate the root cause of performance issues in no time. Simply compare performance to your base configuration and any other variations that are running.
Best Practices
- Keep your custom resources and their instances in separate packages, otherwise you will encounter race conditions and your creation will get stuck. For example, many people keep both the CertManager CRD and CertManager’s resources in the same package, which can cause problems. Most of the time, reapplying the YAML fixes the issue. But it’s good practice to keep them separately.
- Try to keep the common values like namespace, common metadata in the base file.
- Organize your resources by kind, using the following naming convention: lowercase-hypenated.yaml (e.g., horizontal-pod-autoscaler.yaml). Place services in the service.yaml file.
- Follow standard directory structure, using bases/ for base files and patches/ or overlays/ for environment-specific files.
- While developing or before pushing to git, run “kubectl kustomize cfg fmt file_name” to format the file and set the indentation right.
Continue Reading it in https://foxutech.com/kustomize-its-features-and-best-practices/
If you like, you can Buy Me a Coffee.