Kustomize overlays per environment

Keep a Kustomize base and one overlay per environment in git, and give each overlay its own app and deployment.

The situation

Your manifests live in a Kustomize base, and each environment is an overlay that patches it: fewer replicas in staging, a pinned image tag in production. Kustomize has no values, so what differs between environments lives in overlay files, reviewed in git like everything else.

An app has one Path, and every deployment of the app builds it. So each overlay is its own app: web-staging builds deploy/overlays/staging and web-production builds deploy/overlays/production, each with a deployment on its own environment (Kustomize).

Two deployments of different overlay apps cannot take each other's snapshots, because promotion needs both to hold the same apps. This setup renders each overlay separately from the same repository, at the commit its branch points at. If you want a byte-for-byte promotion from staging to production, use one app and put the differences in Helm values.

Repository layout

kustomization.yaml
deployment.yaml
service.yaml
kustomization.yaml
replicas.yaml
kustomization.yaml
replicas.yaml

Settings

The manifests

deploy/base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml
  - service.yaml
deploy/base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: ghcr.io/example/shop-web:latest
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
deploy/overlays/staging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base
patches:
  - path: replicas.yaml
images:
  - name: ghcr.io/example/shop-web
    newTag: 1.5.0-rc1
deploy/overlays/staging/replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1

The production overlay is the same with replicas: 6 and newTag: 1.4.2. Neither overlay sets namespace:; Wardn applies in the deployment's namespace, and the operator refuses an object that names a different one. Refer to the base by its directory, as above.

Wardn gives the renderer the whole repository, so ../../base resolves. It then builds the directory named by Path. Wardn looks the newTag image up and pins it to a digest in the snapshot (What every render guarantees); the registry must be readable without a login.

The sandbox checks every kustomization file in the repository, not only the one it builds. A helmCharts, generators, transformers or remote resources entry in an unrelated directory fails this build too. Commit remote bases into the repository, and render charts with a Helm app.

Two sources, two apps, two deployments

Open /apps/new twice. Each time choose A new repository, Git repository, the repository URL, and:

Source › PathApp › NameRendered with
Firstdeploy/overlays/stagingweb-stagingKustomize
Seconddeploy/overlays/productionweb-productionKustomize

Give the sources distinct Source name values, such as shop-staging and shop-production. On each app's page choose New deployment: web-staging runs on the Environment staging, web-production on the Environment production, both in namespace shop, Track a branch main.

A push to main renders both deployments. The objects of one only change when that overlay or the base changes. Production environments take snapshots only from a protected branch, so protect main, or name the branch on the environment (Guardrails).

Check that it worked

kubectl --context kind-staging -n shop get deploy web -o jsonpath='{.spec.replicas} {.spec.template.spec.containers[0].image}{"\n"}'
kubectl --context kind-prod    -n shop get deploy web -o jsonpath='{.spec.replicas} {.spec.template.spec.containers[0].image}{"\n"}'

Staging prints 1 and ghcr.io/example/shop-web:1.5.0-rc1@sha256:...; production prints 6 and the 1.4.2 tag with its own digest. Change the production overlay's newTag, push, and open the deployment's Snapshots tab: the new snapshot's page shows the objects that changed (Compare snapshots).

If the deployment is Held for review with few objects, a patch or resources entry dropped them: open the held snapshot to see how many (Held snapshots). If the apply is refused for a namespace, a kustomization sets namespace: to something other than the deployment's.

Next

Related: Apps, Sources and apps, Environments.

On this page