Kubernetes coreLesson 3 of 13
Reading and Applying a Manifest
Follow one file, web.yaml, from your editor to the cluster. See what each top-level field means, where a typo gets caught, and how create, apply, diff and server-side apply treat the same file differently.
Command—
FieldapiVersion · kind · metadata · spec
Result—
RequestResponse / reportWatchAppears in a later step
What you will learn
- YAML maps, lists, indentation and multi-document files
- apiVersion and kind, and how to find the right pair
- metadata: name, namespace, labels and annotations
- Strict field validation catches typos on the server
- create versus apply, and previewing with kubectl diff
- Field ownership and conflicts in server-side apply
Common apply errors
| Message (shortened) | Meaning | Fix |
|---|---|---|
error converting YAML to JSON: yaml: line 7: ... | The file is not valid YAML: bad indentation, a tab, or a missing colon | Fix the line shown. Indent with spaces only |
no matches for kind "Deployment" in version "extensions/v1beta1" | That group/version does not serve this kind, or the CRD is not installed | Find the right pair with kubectl api-resources. Install the CRD first |
strict decoding error: unknown field "spec.replica" | A typo, or a field at the wrong indentation level | Check the path with kubectl explain |
cannot unmarshal string into Go struct field ... of type int32 | Wrong type, such as replicas: "3" | Remove the quotes, or match the type kubectl explain shows |
AlreadyExists | kubectl create on an object that exists | Use kubectl apply |
field is immutable | The field cannot change after creation, such as a Deployment's spec.selector | Create a new object, or delete and recreate |
namespaces "shop" not found | The target namespace does not exist yet | Create the namespace first, or put it earlier in the file |
Apply failed with 1 conflict: conflict with "..." | Server-side apply: another field manager owns that field | Drop the field from your file, or take it over with --force-conflicts |
Try it yourself
kubectl api-resources | grep -i deploy # NAME, APIVERSION, NAMESPACED, KIND
kubectl explain deployment.spec.strategy # Field type and meaning from the cluster's schema
kubectl explain deployment.spec --recursive | head -40
kubectl apply -f web.yaml --dry-run=server # Full validation and admission, nothing saved
kubectl diff -f web.yaml # What apply would change (exit code 1 = differences)
kubectl apply -f web.yaml
kubectl get deploy web -o yaml | grep -A3 last-applied # Client-side apply's record of your file
kubectl apply -f web.yaml --server-side
kubectl get deploy web -o yaml --show-managed-fields | grep -B2 -A8 managedFields # Who owns which field
Check yourself
Further reading: Objects in Kubernetes · Declarative management with kubectl apply · Server-Side Apply · Field validation · Desired State and the Control Loop · Per-environment deploys with Kustomize