YAML Viewer, Formatter & Validator · Tutorial
How to fix a Kubernetes Deployment that won't apply
8 min readintermediateUpdated
You copied a Deployment from an old blog post or an older project and kubectl apply fails with an error that is hard to read. This tutorial runs the manifest through the YAML Viewer, Formatter & Validator's Kubernetes check and fixes each problem in turn, until the Deployment is valid and follows good practice.
What you need
- The YAML Viewer, Formatter & Validator, Validate tab, checking as Kubernetes manifest
- The manifest that fails, or the sample from the Try it button
Steps
Paste the manifest
Paste the YAML into the left box. On the Validate tab with Check as: Kubernetes manifest, the summary under the list reads 7 errors and 1 warning in 1 object, and the problems are listed on the right in line order. Click any of them to jump to that line.
Update the apiVersion
The first error says
apiVersion extensions/v1beta1 was removed in Kubernetes 1.22. Old tutorials still use it. For a Deployment, writeapiVersion: apps/v1.Fix the name and the selector
shop_apiis not a valid name, because names may only contain lowercase letters, digits and hyphens. Change it toshop-api. The next error says the selector does not match the pod template labels: the selector looks forapp: shop-api, but the pods are labelledapp: api. Make them the same, or the Deployment will never find its own pods.Quote the environment values
Kubernetes needs every
envvalue to be text.value: 3000is a number andvalue: trueis a boolean, so both are errors. Quote them.- name: PORT value: "3000" - name: CACHE_ENABLED value: "true"Use Kubernetes units for resources
cpu: 1 coreandmemory: 512MBare not valid amounts. CPU is written in cores or millicores (1or500m), and memory uses binary suffixes such as512Mior1Gi. Fix both lines.Pin the image and add a probe
The warning says
bento/shop-apihas no version tag, so a restart could pull a different image. Add a tag such asbento/shop-api:2.4.1. With Show tips ticked, you also see that the container has no readiness or liveness probe. Adding one lets Kubernetes know when the pod is ready for traffic.Confirm and apply
When the summary reads No problems found in 1 object, copy the manifest back and run
kubectl apply --dry-run=server -fagainst your cluster for a final check.
Try it yourself
Opens the YAML Viewer, Formatter & Validator with this tutorial's sample loaded.
Common mistakes
- Fixing only the selector or only the labels
spec.selector.matchLabelsandspec.template.metadata.labelsmust agree. Changing one side moves the error rather than fixing it.- Using MB and GB
- Kubernetes reads
Mas 1,000,000 bytes andMias 1,048,576. PlainMBis not accepted at all. - Forgetting the --- between objects
- Several objects in one file must be separated by a line with
---. Without it, the second object's keys overwrite the first and the validator reports duplicate keys.
Next steps
Go back to the YAML Viewer, Formatter & Validator, or keep going with one of these:
FAQ
Is selector a required field?
Yes, for apps/v1 Deployments, StatefulSets and DaemonSets. Older API versions filled it in automatically, which is why old examples often leave it out.
Can I validate several objects at once?
Yes. Paste the whole file with --- between objects and every object is checked.