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)
- caching (requires Redis) — enable only for premium and standalone
- external database (Postgres) — enable only for dev and premium
Project layout
Example repository structure used in this lesson:Implementing a component
Each component folder contains the resources needed for the feature and akustomization.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 componentkustomization.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
apideployment 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 baseapi 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’skustomization.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
premiumoverlay would include both../../components/dband../../components/cachingif it requires both features. - The
standaloneoverlay would include only../../components/cachingin 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:.