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

Steps

  1. 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.

  2. Update the apiVersion

    The first error says apiVersion extensions/v1beta1 was removed in Kubernetes 1.22. Old tutorials still use it. For a Deployment, write apiVersion: apps/v1.

  3. Fix the name and the selector

    shop_api is not a valid name, because names may only contain lowercase letters, digits and hyphens. Change it to shop-api. The next error says the selector does not match the pod template labels: the selector looks for app: shop-api, but the pods are labelled app: api. Make them the same, or the Deployment will never find its own pods.

  4. Quote the environment values

    Kubernetes needs every env value to be text. value: 3000 is a number and value: true is a boolean, so both are errors. Quote them.

    - name: PORT
      value: "3000"
    - name: CACHE_ENABLED
      value: "true"
  5. Use Kubernetes units for resources

    cpu: 1 core and memory: 512MB are not valid amounts. CPU is written in cores or millicores (1 or 500m), and memory uses binary suffixes such as 512Mi or 1Gi. Fix both lines.

  6. Pin the image and add a probe

    The warning says bento/shop-api has no version tag, so a restart could pull a different image. Add a tag such as bento/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.

  7. Confirm and apply

    When the summary reads No problems found in 1 object, copy the manifest back and run kubectl apply --dry-run=server -f against your cluster for a final check.

Try it yourself

Opens the YAML Viewer, Formatter & Validator with this tutorial's sample loaded.

Try it now

Common mistakes

Fixing only the selector or only the labels
spec.selector.matchLabels and spec.template.metadata.labels must agree. Changing one side moves the error rather than fixing it.
Using MB and GB
Kubernetes reads M as 1,000,000 bytes and Mi as 1,048,576. Plain MB is 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.