Sub topic 13
Organizing manifests in files and directories
This chapter assumes you can write a Deployment, Service, and ConfigMap, and that you have run kubectl apply -f on a single file. Here you will learn how to split manifests across files, how kubectl reads a directory, and how to lay out a project for version control.
One file or many
A manifest file can hold one resource or several. Two common choices:
- One resource per file. Diffs in version control are small, and each file name tells you what it contains.
- Several related resources in one file. This suits a small app whose Deployment and Service always change together.
Neither is required. Pick one rule per project and keep to it.
Multi-document files
YAML lets you put several documents in one file by separating them with a line containing ---. kubectl treats each document as its own resource.
apiVersion: v1
kind: Namespace
metadata:
name: demo
---
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
namespace: demo
data:
LOG_LEVEL: infokubectl applies the documents in the order they appear. The Namespace comes first, so the ConfigMap can be created inside it.
Applying a directory
Pass a directory to -f and kubectl reads every .yaml, .yml, and .json file in it. Other files, such as a README, are ignored.
kubectl apply -f manifests/
kubectl apply -f manifests/ -RThe first command reads only the top level of manifests/. Add -R (recursive) to also read subdirectories.
Files are read in alphabetical order. If one resource depends on another, such as a Namespace, name the files so the dependency sorts first.
A practical layout
A small application might look like this:
manifests/
00-namespace.yaml
10-config.yaml
20-deployment.yaml
30-service.yamlThe numeric prefixes make the order explicit, so the Namespace exists before anything that uses it. For several apps or environments, use one subdirectory each and apply with -R, or apply each subdirectory on its own:
apps/
web/
deployment.yaml
service.yaml
api/
deployment.yaml
service.yamlkubectl apply -f apps/web/Keep manifests in version control
Store the manifest directory in Git. The files then act as the record of what you want running, and every change goes through a commit. Before you commit or apply, run kubectl diff -f manifests/ to preview what will change in the cluster.
Do not commit real secret values. Keep Secret manifests out of the repository, or use a tool that encrypts them.
Common errors
- Namespace not found. A file that uses
namespace: demosorted before the file that creates it. Rename the files so the Namespace sorts first. - Nothing applied from subdirectories. You omitted
-R. - A resource silently missing. The file has a
.txtor other unrecognized extension, so kubectl skipped it. - Parse error on a multi-document file. A
---line is indented, or a document is missingkind. Each document must be a complete manifest.
Exercise
You run kubectl apply -f apps/ and only the files directly inside apps/ are applied, not the ones in apps/web/. What change fixes this, and what must you check about file order?
Show answer
Add -R: kubectl apply -f apps/ -R. Files are still read in alphabetical order within each directory, so make sure any Namespace or other dependency sorts before the resources that use it.