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:
DEMO_HOME = $(mktemp -d)
/base
Define a common base:
$ cd $DEMO_HOME
$ mkdir base
$ cd base
Create a Sample Pod File and Kustomize file in base
$ vim kustomization.yaml
# kustomization.yaml contents
resources:
- pod.yaml
# pod.yaml contents
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: nginx
image: nginx:latest
/dev
Define a dev variant overlaying base:
$ cd $DEMO_HOME
$ mkdir dev
$ cd dev
Create a Kustomize file in dev
# kustomization.yaml contents
resources:
- ./../base
namePrefix: dev-
/staging
Define a staging variant overlaying base:
$ cd $DEMO_HOME
$ mkdir staging
$ cd staging
Create a Kustomize file in staging
# kustomization.yaml contents
resources:
- ./../base
namePrefix: stag-
/production
Define a production variant overlaying base:
$ cd $DEMO_HOME
$ mkdir production
$ cd production
Create a Kustomize file in production
# kustomization.yaml contents
resources:
- ./../base
namePrefix: prod-
kustomize @ root dir
Then define a Kustomization composing three variants together:
# kustomization.yaml contents
resources:
- ./dev
- ./staging
- ./production
namePrefix: cluster-a-
directory sturcture
.
├── kustomization.yaml
├── base
│ ├── kustomization.yaml
│ └── pod.yaml
├── dev
│ └── kustomization.yaml
├── production
│ └── kustomization.yaml
└── staging
└── kustomization.yaml
Confirm that the kustomize build
output contains three pod objects from dev, staging and production variants.
output
apiVersion: v1
kind: Pod
metadata:
labels:
app: myapp
name: cluster-a-dev-myapp-pod
spec:
containers:
- image: nginx:latest
name: nginx
apiVersion: v1
kind: Pod
metadata:
labels:
app: myapp
name: cluster-a-prod-myapp-pod
spec:
containers:
- image: nginx:latest
name: nginx
apiVersion: v1
kind: Pod
metadata:
labels:
app: myapp
name: cluster-a-stag-myapp-pod
spec:
containers:
- image: nginx:latest
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.