Components
Overlays answer "what is different in production?". Some differences are features that several environments switch on independently: autoscaling, a PodDisruptionBudget, debug logging, an extra sidecar. With plain bases and overlays you end up copying the same patches into several overlays, or building fragile chains of overlays. Components are reusable bundles of resources and patches that any overlay can mix in.
Why bases can't do it
The obvious attempt is to make each feature a base that includes the app base, and let production include both features. Kustomize rejects it, because the app base would be included twice (the session keeps only the end of a long error message):
mkdir -p base with-hpa with-pdb prod
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: linkly-config\n' > base/cm.yaml
printf 'resources: [cm.yaml]\n' > base/kustomization.yaml
printf 'resources: [../base]\n' > with-hpa/kustomization.yaml
printf 'resources: [../base]\n' > with-pdb/kustomization.yaml
printf 'resources: [../with-hpa, ../with-pdb]\n' > prod/kustomization.yaml
kustomize build prod 2>&1 | grep -o 'may not add resource.*'may not add resource with an already registered id: ConfigMap.v1.[noGrp]/linkly-config.[noNs]
A component is not built on its own: it's applied to whatever the including kustomization has accumulated so far, so it can patch objects it doesn't own and can be combined freely.
linkly's components
Two features for linkly: autoscaling (an HPA, and the Deployment's fixed replicas removed so the two don't fight) and disruption-budget (a PodDisruptionBudget). Staging uses neither, production uses both:
mkdir -p base components/autoscaling components/disruption-budget overlays/staging overlays/production
cat > base/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: linkly
spec:
replicas: 2
selector: {matchLabels: {app.kubernetes.io/name: linkly}}
template:
metadata: {labels: {app.kubernetes.io/name: linkly}}
spec:
containers:
- name: linkly
image: ghcr.io/acme/linkly:1.4.0
resources:
requests: {cpu: 100m, memory: 64Mi}
EOF
printf 'resources: [deployment.yaml]\n' > base/kustomization.yaml
cat > components/autoscaling/hpa.yaml <<'EOF'
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: linkly
spec:
scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: linkly}
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource: {name: cpu, target: {type: Utilization, averageUtilization: 70}}
EOF
cat > components/autoscaling/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources: [hpa.yaml]
patches:
- target: {kind: Deployment, name: linkly}
patch: |-
- op: remove
path: /spec/replicas
EOF
cat > components/disruption-budget/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources: [pdb.yaml]
EOF
cat > components/disruption-budget/pdb.yaml <<'EOF'
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: linkly
spec:
maxUnavailable: 1
selector: {matchLabels: {app.kubernetes.io/name: linkly}}
EOF
printf 'namespace: linkly-staging\nresources: [../../base]\n' > overlays/staging/kustomization.yaml
cat > overlays/production/kustomization.yaml <<'EOF'
namespace: linkly-prod
resources: [../../base]
components:
- ../../components/autoscaling
- ../../components/disruption-budget
EOF
for env in staging production; do
echo "=== $env: $(kustomize build overlays/$env | yq -N '.kind' | paste -sd' ')"
kustomize build overlays/$env | yq -N 'select(.kind == "Deployment") | "replicas: " + (.spec.replicas // "unset (HPA)")'
done
kustomize build overlays/production | kubeconform -strict -summary=== staging: Deployment
replicas: 2
=== production: Deployment PodDisruptionBudget HorizontalPodAutoscaler
replicas: unset (HPA)
Summary: 3 resources found parsing stdin - Valid: 3, Invalid: 0, Errors: 0, Skipped: 0
Note the apiVersion: components use kustomize.config.k8s.io/v1alpha1 and kind: Component; kustomizations default to v1beta1/Kustomization even when the header is omitted, as in the overlays here.
Order matters
Kustomize applies, in order: the resources (building bases first), then the components in the listed order, then the overlay's own transformations (namespace, prefixes, labels, images, patches). So:
- a component can patch anything from the overlay's resources and from components listed before it;
- the overlay's
namespaceandnamePrefixapply to objects a component adds (the HPA above landed inlinkly-prod); - two components that patch the same field: the later one wins.
Good component candidates
| Component | Adds or changes |
|---|---|
autoscaling |
HPA; removes fixed replicas |
disruption-budget |
PodDisruptionBudget |
monitoring |
ServiceMonitor (Prometheus Operator); metrics port annotations |
debug |
LOG_LEVEL=debug, an ephemeral debug sidecar, relaxed probes |
network-policy |
default-deny plus linkly's allowed flows |
ingress-instead-of-route |
swaps the HTTPRoute for an Ingress ($patch: delete + a new resource) for clusters without Gateway API |
Keep components independent where you can; one that only works after another should say so in a comment at the top of its kustomization.yaml.
Test yourself
Why does including ../with-hpa and ../with-pdb (both built on ../base) fail?
- Kustomize can't include two directories
- The base's objects would be added twice, with the same ids
- HPAs and PDBs conflict
- Bases must be remote
What is the kind of a reusable feature bundle in Kustomize?
- Kustomization
- Overlay
- Component
- Patch
Does an overlay's namespace: apply to an HPA added by a component?
- Yes: the overlay's transformations run after its components
- No, components keep their own namespace
- Only with includeSelectors
- Only if the component sets a namespace