Kustomize bases and overlays
One application, two environments
A catalog application needs one replica during development and three for staging. Copying an entire Deployment into each environment creates two places to fix the same bug. Kustomize lets you maintain shared Kubernetes resource files and describe environment differences in a separate directory. It transforms configuration before deployment; it is not a controller and does not continuously watch a cluster. A build produces resource YAML, not running Pods.
base/ kustomization.yaml deployment.yaml service.yamloverlays/ dev/kustomization.yaml dev/replicas.yaml staging/kustomization.yaml staging/replicas.jsonDeclare reusable resources
A base is a self-contained directory containing a kustomization file and its resources. The catalog Deployment specifies one replica and an app=catalog selector. The Service uses the same label, so it selects catalog Pods. The generator is introduced here only as the source of a named ConfigMap; the next lesson explains generated names. All referenced files are included in lab-fixtures/kustomize. The base has no dependency on either environment.
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- deployment.yaml- service.yamlconfigMapGenerator:- name: catalog-settings literals: - MODE=base - PAGE_SIZE=10Describe an overlay
The development overlay includes the base by directory path, sets a namespace and prefixes resource names. Its patch explicitly preserves a single replica. Staging uses a different prefix and three replicas. The namespace field sets namespace on applicable resources; it does not create that namespace. Prefixing names does not mean labels automatically acquire the same prefix. Kustomize can update recognized references, but arbitrary strings containing a name may need an explicit replacement.
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basenamespace: dev-studynamePrefix: dev-patches:- path: replicas.yamlconfigMapGenerator:- name: catalog-settings behavior: merge literals: - MODE=devRender before applying
From the supplied fixture directory, use an approved installed renderer. The standalone command and embedded kubectl implementation can have different versions; record the actual versions, and check supported fields rather than assuming parity. The two build commands use only local inputs here. Their expected results are one Deployment, one Service and one generated ConfigMap per overlay, with names and namespace adjusted. The placeholder image is deliberately not deployable. Do not pipe this example into apply. In a separately authorized lab, kubectl apply -k submits the composed resources; local rendering alone does not establish server acceptance, readiness or security.
kustomize build overlays/devkubectl kustomize overlays/stagingKeep common identity consistent
Use labels for stable selection, not just naming conventions. Kustomize supports labels and annotations for shared metadata; avoid changing existing Deployment selectors accidentally. Use labels with explicit selector behavior supported by the selected release; legacy commonLabels can also affect selectors. nameSuffix and namePrefix alter names, so inspect references and account for old objects during a rename. A rename is not an in-place update. Avoid remote bases and unreviewed plugins; reproducible composition depends on controlled inputs and a recorded renderer version.
Practice and recap
Compare the two overlay files and expected-semantics.json. Identify which files must change when a shared Service port changes, and which change when only staging needs more replicas. Run the provided local input checker if Python and PyYAML are available. The checker is not a Kustomize renderer; actual output and API tests remain required.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Kustomize bases and overlays — knowledge check
Reason about local composition and resource relationships.
Both environments need the same Service port correction. Where should it normally go?
What does setting namespace: staging-study in an overlay establish?
What is the expected output of building the staging fixture?
Which is a reliable way to compare standalone and embedded Kustomize?
Why review a prefix change on an existing Deployment?