k8s/ folder and scale to a nested, maintainable Kustomize layout that’s CI/CD friendly.
Why use Kustomize?
- Avoid running
kubectl applyin many directories. - Keep environment-specific overlays and common bases clean.
- Aggregate resources with a single root
kustomization.yaml.
- You have a
k8s/directory containing four YAML files:api-depl.yaml(API Deployment)api-service.yaml(API Service)db-depl.yaml(DB Deployment)db-service.yaml(DB Service)
k8s/, you can apply them with kubectl’s directory apply:
k8s/api/— containsapi-depl.yaml,api-service.yamlk8s/db/— containsdb-depl.yaml,db-service.yaml
kustomization.yaml that references resources across directories or files. Create k8s/kustomization.yaml and list the relative paths to all manifests:
resources are relative to the directory containing that kustomization.yaml.
Apply everything with one command
You can render the combined manifests and apply them in two common ways:
- Using the standalone Kustomize binary:
- Or using kubectl’s embedded Kustomize renderer:
api, db, cache, kafka), listing every file in the root kustomization.yaml gets unwieldy. Instead, create a kustomization.yaml inside each subdirectory and reference the directories from the root.
Example layout:
k8s/kustomization.yaml(root)k8s/api/kustomization.yamlk8s/db/kustomization.yamlk8s/cache/kustomization.yamlk8s/kafka/kustomization.yaml
kustomization.yaml:
kustomization.yaml (example: k8s/db/kustomization.yaml):
- When Kustomize encounters a directory listed in
resources, it looks for a kustomization file (for examplekustomization.yamlorkustomization.yml) inside that directory. - It reads the listed resources in the subdirectory’s kustomization file and includes those manifests in the build.
- The root
kustomization.yamltherefore aggregates all subdirectory resources without needing to list each file individually.
Best practices
- Keep
kustomization.yamlfiles close to the resources they reference. - Use directory-level
kustomization.yamlfiles as logical units (service, database, cache). - Prefer
kubectl apply -kin CI for simplicity unless you need Kustomize features not supported by kubectl’s embedded version.
You can use either
kustomize build k8s/ | kubectl apply -f - or kubectl apply -k k8s/. kubectl apply -k is convenient because it uses the Kustomize renderer embedded in kubectl, which simplifies CI/CD steps.Ensure a kustomization file (for example
kustomization.yaml or kustomization.yml) exists in any directory you reference as a resource. Paths are interpreted relative to the containing kustomization.yaml. If a path points to a directory, that directory must contain a valid kustomization file.- Use a root
kustomization.yamlto collect resources across directories. - For many directories, create a
kustomization.yamlper subdirectory and reference directories from the root. - Apply all resources in one step using
kustomize buildpiped tokubectl apply -f -orkubectl apply -kto simplify local workflows and CI/CD automation.