- A top-level
k8s/directory containing three subfolders:api/— Kubernetes manifests for the API servicecache/— Kubernetes manifests for Redisdb/— Kubernetes manifests for MongoDB
Example Deployment (Mongo)
- This is a complete Deployment manifest for the MongoDB container (example shows environment variables sourced from a ConfigMap named
db-credentials).
- The simplest approach is to apply each directory directly with
kubectl apply -f <dir>. This is straightforward for small projects.
-f flags in one command:
- A common way to consolidate manifests is to add a
Kustomization.yamlat the root ofk8s/that lists resources from subdirectories. This produces a single combined set of YAML documents.
k8s/Kustomization.yaml (single root approach):
You can use either the standalone
kustomize CLI piped into kubectl apply -f - or the built-in kubectl apply -k <directory>. Both produce the same result.- For better modularity and maintainability, create a
Kustomization.yamlinside each subdirectory (api/,cache/,db/) and then have the rootk8s/Kustomization.yamlreference those directories. Kustomize will read the subdirectoryKustomization.yamlfiles.
api/Kustomization.yaml:
cache/Kustomization.yaml:
db/Kustomization.yaml:
k8s/Kustomization.yaml to reference the directories (Kustomize will look for their Kustomization.yaml files):
- Build combined manifests:
- Apply with kubectl (preferred shorthand):
Summary
- Both a single root
Kustomization.yamland per-directoryKustomization.yamlfiles are valid. For growing projects, the per-directory (modular) approach is recommended. - Use
kustomize build <dir>to preview merged manifests and apply with eitherkustomize build <dir> | kubectl apply -f -or the shorthandkubectl apply -k <dir>.
- Kustomize: https://learn.kodekloud.com/user/courses/kustomize
- Kubernetes docs: https://kubernetes.io/docs/