Sub topic 11
Applying manifests with kubectl apply
This chapter assumes you can read a manifest (apiVersion, kind, metadata, spec), know what a Deployment and a Service are, and have kubectl pointed at a cluster you can safely change, such as a local one.
What apply does
kubectl apply reads a manifest and makes the cluster match it. If the object does not exist, apply creates it. If it exists, apply compares your file with the live object and sends only the changes. You describe the desired state, and the controllers in the cluster reconcile toward it.
This differs from kubectl create, which fails if the object already exists. It also differs from imperative commands such as kubectl scale, which change the live object without updating your file. Over time the file and the cluster then disagree.
A first apply
Create the namespace first, because the other objects refer to it. Save the Namespace manifest as namespace.yaml, then apply it.
kubectl apply -f namespace.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yamlEach command prints one line per object, such as deployment.apps/web created. Run the same command again with no edits and you see unchanged. That repeatable result is called idempotency, and it is why you can safely re-run apply in scripts and pipelines.
Now change replicas: 2 to replicas: 3 in deployment.yaml and apply again. The output says configured. Only the replica count changes, and the existing Pods keep running.
Check the result:
kubectl get deployment web -n demo
kubectl rollout status deployment/web -n demoChoosing what to apply
The -f flag accepts several kinds of input:
| Input | Example | Effect |
|---|---|---|
| One file | `-f deployment.yaml` | Applies every document in the file |
| A directory | `-f manifests/` | Applies each `.yaml`, `.yml`, and `.json` file in it |
| A directory tree | `-f manifests/ -R` | Also descends into subdirectories |
| A URL | `-f https://example.com/app.yaml` | Fetches and applies the file |
| Standard input | `-f -` | Reads the manifest from a pipe |
| Kustomize directory | `-k overlay/` | Builds the kustomization, then applies it |
A directory is applied in file name order, not in dependency order. Name files with numeric prefixes such as 00-namespace.yaml if order matters. For a namespace-scoped object, either set metadata.namespace in the file or pass -n. If both are present and they disagree, kubectl reports an error.
Common errors
namespaces "demo" not found: the namespace does not exist yet. Apply the Namespace first.no matches for kind ... in version ...: theapiVersionis wrong, or the cluster does not have that resource type. Check withkubectl api-resources.field is immutable: some fields cannot change after creation, such as a Deployment'sspec.selector. Delete and recreate the object.error converting YAML to JSON: usually indentation or a tab character. YAML allows only spaces.- Edits in the file have no effect: you probably applied the wrong file or context. Check
kubectl config current-context.
Exercise
You apply deployment.yaml and see deployment.apps/web created. You run the same command again without editing the file. What does kubectl print, and why?
Show answer
It prints deployment.apps/web unchanged. Apply compares the file with the live object, finds no difference, and sends no change. This is why apply is idempotent and safe to repeat.
Exercise
Your manifests/ directory holds deployment.yaml and namespace.yaml. Applying the directory fails with namespaces "demo" not found. How do you fix it without changing the manifests' content?
Show answer
Files are applied in name order, so deployment.yaml runs before namespace.yaml. Rename the files with numeric prefixes, such as 00-namespace.yaml and 10-deployment.yaml. You can also apply the namespace file first.