Skip to main content
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—
Kubernetes clusterweb.yamlYour manifestkubectlapply · diff · explainAPI serverDecode · validateOpenAPI schemaField definitionsetcdStored objectHPAChanges replicasDeployment ctrlWrites statusPodswebRead
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)MeaningFix
error converting YAML to JSON: yaml: line 7: ...The file is not valid YAML: bad indentation, a tab, or a missing colonFix 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 installedFind 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 levelCheck the path with kubectl explain
cannot unmarshal string into Go struct field ... of type int32Wrong type, such as replicas: "3"Remove the quotes, or match the type kubectl explain shows
AlreadyExistskubectl create on an object that existsUse kubectl apply
field is immutableThe field cannot change after creation, such as a Deployment's spec.selectorCreate a new object, or delete and recreate
namespaces "shop" not foundThe target namespace does not exist yetCreate 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 fieldDrop 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

  1. web.yaml says replica: 3 instead of replicas: 3. With current kubectl defaults, what happens on apply?
  2. An HPA scales the Deployment, but your file still says replicas: 3 and you use client-side apply. What happens on every apply?
  3. You want to see exactly what apply would change, including defaults and changes from mutating admission. Which command fits?

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