Contents

Customising Kubernetes Resources with Kustomize

Kubernetes is a popular container orchestration system that allows you to deploy, scale, and manage containerized applications. One of the challenges of using Kubernetes is managing the large number of YAML files that are used to define and configure resources such as Pods, Deployments, Services, and ConfigMaps. Kustomize is a tool that simplifies the management of Kubernetes resources by providing a flexible and modular approach to customising YAML files.

In this article, we’ll introduce Kustomize and discuss how it can help you manage your Kubernetes resources more effectively. We’ll cover the basic concepts of Kustomize, including resources, patches, and overlays, and show how you can use them to customise your Kubernetes resources.

What is Kustomize?

Kustomize is a tool that allows you to customise and generate Kubernetes YAML files based on a set of base resources. Kustomize works by applying a series of patches to the base resources to modify or add new resources. Kustomize also supports overlays, which are used to apply additional customisations on top of the base resources and patches.

Kustomize is built into the kubectl command-line tool, which makes it easy to use and integrate with other Kubernetes workflows. Kustomize also supports integration with other Kubernetes tools and platforms, including Helm, Skaffold, and GitOps workflows.

Syntax

The syntax of Kustomize is based on YAML, which is a human-readable data serialization language. Kustomize uses YAML files to define how to customise Kubernetes resources. These YAML files are called Kustomization YAML files and typically have the filename kustomization.yaml.

In the Kustomization YAML file, you can define a list of resources, patches, and overlays. Each resource, patch, and overlay is defined as a YAML document. The Kustomize syntax is designed to be modular, allowing you to define resources and customisations separately and combine them as needed.

In addition to the Kustomization YAML file, you can also define individual resource YAML files and patch YAML files. These YAML files follow the standard Kubernetes syntax.

Resources

The resources field in Kustomize specifies a list of base resources that are used as the starting point for customisation. These resources can be specified as file paths or URLs. When Kustomize generates the final YAML manifest, it includes all of the resources specified in the resources field.

Here’s an example of a Kustomization file that specifies a Deployment and a Service as base resources:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- deployment.yaml
- service.yaml

Patches

The patches field in Kustomize specifies patches that are applied to the base resources to modify them. A patch can be a JSON or YAML document that specifies changes to the base resources, or it can be a strategic merge patch (SMP) that specifies a set of changes to apply to a resource. The patches field can be used to modify existing resources or add new resources.

Here’s an example of a Kustomization file that includes a patch to modify the number of replicas for a Deployment:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- deployment.yaml
- service.yaml

patches:
- target:
    kind: Deployment
    name: nginx
  patch: |-
    spec:
      replicas: 3    

Overlays

The overlays field in Kustomize specifies a list of directories that contain additional resources and customisations that are applied on top of the base resources. An overlay can include additional resources, patches, and other customisation files that are applied after the base resources and patches. Overlays allow you to customise the base resources in a modular way without modifying the original resources directly.

Here’s an example of a Kustomization file that includes an overlay to add a label to a Deployment:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- deployment.yaml
- service.yaml

overlays:
- path: prod/
  patches:
  - target:
      kind: Deployment
      name: nginx
    patch: |-
      metadata:
        labels:
          env: prod      

Kustomize commands

Let’s go over some of the most commonly used Kustomize commands.

kustomize build

The kustomize build command allows you to generate the final YAML files for your Kubernetes resources based on your Kustomization YAML file. This command generates the final YAML files by applying all patches and overlays defined in your Kustomization YAML file.

Here’s an example:

$ kustomize build path/to/kustomization/directory

kustomize edit

The kustomize edit command allows you to modify the Kustomization YAML file directly from the command line. This command can be used to add, remove or modify resources, patches, and overlays in your Kustomization YAML file.

Here’s an example:

$ kustomize edit add resource path/to/resource.yaml

kustomize create

The kustomize create command allows you to create a new Kustomization YAML file from scratch. This command will create a new directory and a basic Kustomization YAML file in that directory.

Here’s an example:

$ kustomize create --autodetect

kustomize diff

The kustomize diff command allows you to view the differences between the final YAML files generated by Kustomize and the YAML files generated without Kustomize.

Here’s an example:

$ kustomize build path/to/kustomization/directory | diff path/to/non-kustomize/generated/yaml

kustomize validate

The kustomize validate command allows you to validate your Kustomization YAML file. This command checks for syntax errors and ensures that all resources, patches, and overlays are correctly defined.

Here’s an example:

$ kustomize validate path/to/kustomization/directory

These are just a few of the most commonly used Kustomize commands. Kustomize provides many more commands and options, so be sure to check out the documentation for more information.

Conclusion

Kustomize is a powerful tool that provides a flexible and modular approach to customising Kubernetes resources. It allows you to manage complex YAML configurations easily, enabling you to make changes without modifying the original YAML files. Kustomize simplifies the management of Kubernetes resources and reduces the risk of errors when managing large YAML files.

In this article, we’ve covered the basic concepts of Kustomize, including resources, patches, and overlays, and provided examples of how to use them to customise your Kubernetes resources. We’ve also discussed why Kustomize is a better option than modifying your YAML files directly, as it allows you to create customisations in a modular and easy-to-manage way.