Multibase

kustomize encourages defining multiple variants - e.g. dev, staging and prod, as overlays on a common base.

It’s possible to create an additional overlay to compose these variants together - just declare the overlays as the bases of a new kustomization.

This is also a means to apply a common label or annotation across the variants, if for some reason the base isn’t under your control. It also allows one to define a left-most namePrefix across the variants - something that cannot be done by modifying the common base.

The following demonstrates this using a base that is just a single pod.

Define a place to work:

  1. DEMO_HOME = $(mktemp -d)

/base

Define a common base:

  1. $ cd $DEMO_HOME
  2. $ mkdir base
  3. $ cd base

Create a Sample Pod File and Kustomize file in base

  1. $ vim kustomization.yaml
  1. # kustomization.yaml contents
  2. resources:
  3. - pod.yaml
  1. # pod.yaml contents
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: myapp-pod
  6. labels:
  7. app: myapp
  8. spec:
  9. containers:
  10. - name: nginx
  11. image: nginx:latest

/dev

Define a dev variant overlaying base:

  1. $ cd $DEMO_HOME
  2. $ mkdir dev
  3. $ cd dev

Create a Kustomize file in dev

  1. # kustomization.yaml contents
  2. resources:
  3. - ./../base
  4. namePrefix: dev-

/staging

Define a staging variant overlaying base:

  1. $ cd $DEMO_HOME
  2. $ mkdir staging
  3. $ cd staging

Create a Kustomize file in staging

  1. # kustomization.yaml contents
  2. resources:
  3. - ./../base
  4. namePrefix: stag-

/production

Define a production variant overlaying base:

  1. $ cd $DEMO_HOME
  2. $ mkdir production
  3. $ cd production

Create a Kustomize file in production

  1. # kustomization.yaml contents
  2. resources:
  3. - ./../base
  4. namePrefix: prod-

kustomize @ root dir

Then define a Kustomization composing three variants together:

  1. # kustomization.yaml contents
  2. resources:
  3. - ./dev
  4. - ./staging
  5. - ./production
  6. namePrefix: cluster-a-

directory sturcture

  1. .
  2. ├── kustomization.yaml
  3. ├── base
  4. ├── kustomization.yaml
  5. └── pod.yaml
  6. ├── dev
  7. └── kustomization.yaml
  8. ├── production
  9. └── kustomization.yaml
  10. └── staging
  11. └── kustomization.yaml

Confirm that the kustomize build output contains three pod objects from dev, staging and production variants.

output

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. labels:
  5. app: myapp
  6. name: cluster-a-dev-myapp-pod
  7. spec:
  8. containers:
  9. - image: nginx:latest
  10. name: nginx
  11. apiVersion: v1
  12. kind: Pod
  13. metadata:
  14. labels:
  15. app: myapp
  16. name: cluster-a-prod-myapp-pod
  17. spec:
  18. containers:
  19. - image: nginx:latest
  20. name: nginx
  21. apiVersion: v1
  22. kind: Pod
  23. metadata:
  24. labels:
  25. app: myapp
  26. name: cluster-a-stag-myapp-pod
  27. spec:
  28. containers:
  29. - image: nginx:latest
  30. name: nginx

Similarly to adding different namePrefix in different variants, one can also add different namespace and compose those variants in one kustomization. For more details, take a look at multi-namespaces.