Lesson 5 of 9 · 24m
Designing an Environment Layout That Scales
Structure bases and overlays so dev, staging, and production differ intentionally instead of diverging into unmaintained copies.
The fastest way to make GitOps painful is to copy an application directory once per environment and let them drift apart. Six months later nobody knows which replica count, ingress annotation, or security context was intentional. A scalable layout separates common shape from environment-specific variance. Your directory tree should teach the operational model before anyone opens a diff.
Bases describe what the application is
apps/checkout-api/base/{deployment,service,kustomization}.yaml
apps/checkout-api/overlays/{dev,staging,prod}/
clusters/prod/apps/kustomization.yamlThe base says what is invariant: container command, probes, service shape, security posture, resource requests. Overlays say what legitimately varies: replica count, ingress host, image tag or digest, resource limits, feature flags, and environment-specific secret references.
A base should not contain replicas when an HPA owns scaling, and it should not contain a production hostname just because production was written first. Those decisions leak.
Kustomize overlays make differences reviewable
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: checkout
resources:
- ../../base
images:
- name: ghcr.io/sudosecurityorg/checkout-api
newTag: 1.42.0
patches:
- target: {kind: Deployment, name: checkout-api}
patch: |-
- op: replace
path: /spec/template/spec/containers/0/resources/limits/memory
value: 512MiKeep patches small and boring. A 400-line strategic merge patch is usually a sign the base was never really common. When staging needs persistent special behavior, either make that behavior a first-class configuration input or admit that staging runs a different application shape.
Cluster composition is separate from application composition
The clusters/prod tree decides which applications and platform components belong to that cluster. The apps/checkout-api tree decides how the application is constructed. Mixing those layers creates circular confusion: application teams edit cluster bootstrap while platform teams patch runtime behavior.
Prefer configuration variance over branch variance
Branch-per-environment looks appealing until hotfixes and cherry-picks multiply. main should generally represent reviewed truth, with environment overlays selecting versions and parameters. Branching can model release trains, but environment branches often become unmerged truths that nobody can reconcile.
There are exceptions, especially when regulated change windows demand environment branches. If you use them, enforce merge direction and never let an environment branch become a permanent fork.
Render before you merge
Reviewers cannot reliably mentally evaluate nested bases, Helm charts, generators, and patches. Produce rendered manifests in CI:
kustomize build apps/checkout-api/overlays/prod > /tmp/rendered.yaml
kubeconform -strict -summary /tmp/rendered.yamlFor Helm-inflated resources, render with the exact values files used by the target environment. The thing you validate should be as close as possible to the thing the controller applies.
Field notes
- If a change requires editing all three overlays the same way, it probably belongs in the base.
- If dev, staging, and prod use different CRD versions or controller features, environment parity is already broken.
- Namespace names and ingress hosts are environment values; container security posture usually is not.
- Do not hide production-only privilege in a patch nobody reviews because overlays feel harmless.
- When a diff is hard to review, the layout — not the reviewer — is often the problem.
Was this lesson helpful?