Skip to main content
In this lesson we cover Kustomize components — a way to package optional, reusable configuration so you can enable features selectively across overlays. A component is a self-contained bundle of Kubernetes resources, generators, and patches that represent an optional feature. Use components when a feature should be applied to some overlays but not others. If a feature must be present in every overlay, keep it in the base instead.
Components are reusable collections of Kubernetes resources, patches, secrets, and other Kustomize configuration related to a specific optional feature. They help you avoid copying the same configs across multiple overlays.

Visual example

Suppose our application has three overlays:
  • dev
  • premium
  • standalone (self-hosted)
A shared base configuration is used by all overlays. We also have two optional features:
  • caching (requires Redis) — enable only for premium and standalone
  • external database (Postgres) — enable only for dev and premium
Because each feature applies to a subset of overlays, components are a good fit: place the resources and patches for each feature into its own component folder, then import the appropriate component(s) into the overlays that need them.

Project layout

Example repository structure used in this lesson:

Implementing a component

Each component folder contains the resources needed for the feature and a kustomization.yaml whose kind is Component. The apiVersion should match the version of Kustomize you are using — many recent releases use kustomize.config.k8s.io/v1beta1. Example: Postgres deployment for the db component

Component kustomization

A component kustomization.yaml must set kind: Component and list any resources, generators, and patches that implement the feature. In this example, the db component:
  • declares the Postgres Deployment as a resource,
  • generates a Secret for the DB password, and
  • provides a patch to modify the base api deployment to read that secret.
Ensure the apiVersion and kind: Component are compatible with your Kustomize version. Component support and fields may vary between releases — test locally with kustomize build for the overlay that imports the component.

Patch the base deployment

If a component must modify resources defined in the base, include patches in the component that target those base resources. For example, the base api deployment needs an environment variable to read the DB password from the generated secret:

Including a component in an overlay

To enable a feature for a particular overlay, import the component in that overlay’s kustomization.yaml under the components section. The overlay should still import the base via resources as usual. Example: the dev overlay needs the external database component:
  • The premium overlay would include both ../../components/db and ../../components/caching if it requires both features.
  • The standalone overlay would include only ../../components/caching in this example.

Overlay to components matrix

Summary

  • Components are reusable bundles of Kustomize resources, generators, and patches representing optional features.
  • Use components to enable features in some overlays but not others, keeping each feature’s code in a single place.
  • Overlays import the base and then list the components they require under components:.

Watch Video

Practice Lab