Kustomize: Quick Intro

When it comes to explaining what kustomize is, I prefer the intro from their GitHub repository:

kustomize lets you customize raw, template-free YAML files for multiple purposes, leaving the original YAML untouched and usable as is.

The important points are this:

Installation

Installation is super easy, with the following command working great on Linux based systems, installing it in te current working directory:

curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash

You may want to manually move the binary to a more appropriate directory. On my own system I prefer these types of commands to all live in ~/opt/bin, so I would issue the following command:

mv kustomize ~/opt/bin

A quick test to see if everything is working: kustomize version and the output should look something like this:

{Version:kustomize/v4.5.4 GitCommit:cf3a452ddd6f83945d39d582243b8592ec627ae3 BuildDate:2022-03-28T23:12:45Z GoOs:linux GoArch:amd64}

Quick Start

In a previous project I created called pyk8sdemo-app, I have added a very basic example in the kustomize root directory kubernetes_manifests/kustomization-demo/base

There are several phrases with very specific meanings, like the term root, and you can find a glossary online that explains it perfectly.

In this example, in the kubernetes_manifests/kustomization-demo/base directory, you have your standard manifest files as well as the kustomization.yaml file.

The full file structure is listed below:

.
├── base
│   ├── flask-demo-app-deployment.yaml
│   ├── flask-demo-app-namespace.yaml
│   ├── flask-demo-app-service.yaml
│   └── kustomization.yaml
└── overlays
    ├── full
    │   └── kustomization.yaml
    └── pod-only
        ├── delete-flask-demo-app-service.yaml
        └── kustomization.yaml

In the base/kustomization.yaml file, is just a simple reference to all the manifest files that make up the default deployment. You can apply these manifests to a cluster and your application should deploy.

In the overlays/ directory we get other directories, where each one signifies a specific customization or change based on the base/ configurations.

In this specific example, there are two deployment scenarios: a full deployment, which will basically just deploy the default manifests as-is, and a pod-only customization which effectively leaves out the Service from the default deployment.

These are very similar to the kubernetes_manifests/troubleshooting-app.yaml and kubernetes_manifests/troubleshooting-app-pod-only.yaml manifests, which you can also use.

Deploying a scenario

Command listed below assume you are in the kubernetes_manifests/kustomization-demo/ directory.

To deploy the default scenario, the command would be:

kustomize build overlays/full | kubectl apply -f -

To deploy the Pod only scenario, the following command can be used:

kustomize build overlays/pod-only | kubectl apply -f -

Final thoughts

I still prefer Helm charts to properly package and parameterize deployments, but sometimes it's overkill. Or perhaps you are just fine sticking to normal manifest files. Either way, kustomize is a really good way to cater for different deployment scenarios or environments without the need to use Helm or perhaps even other more sophisticated tools.

Tags

kubernetes, cli, kustomize