Skip to main content

📄 YAML

Almost everything in Kubernetes is described in YAML files: you write what you want ("2 pods with this image") and the cluster makes it happen. This page shows how to read, generate, validate and apply these files.

info

YAML files are applied with kubectl. The Nuvion Panel does not have a YAML editor for cluster resources.


🚀 Before you start​

📖 The structure of every Kubernetes YAML​

apiVersion: apps/v1
kind: Deployment
metadata:
name: exemplo
spec:
replicas: 1
FieldWhat it is
apiVersionThe resource's API version
kindThe type (Pod, Deployment, Service...)
metadataName, labels (labels) and namespace
specWhat you want to exist

📏 YAML rules​

  • Use 2 spaces to indent. Never use Tab.
  • Lists start with - (dash and space).
  • Text with : or special characters goes in quotes.
  • Several resources in the same file are separated by ---.

🚀 1. See the YAML of an existing resource​

kubectl get deployment NAME -o yaml

The output has extra fields, filled in by the cluster (such as status). It is for reference.

🚀 2. Generate a ready-made template​

Instead of writing from scratch, let kubectl generate it and save it to a file:

kubectl create deployment meu-app --image=nginx:1.27 --replicas=2 --dry-run=client -o yaml > meu-app.yaml

--dry-run=client creates nothing in the cluster, it only generates the text.

🚀 3. Look up a field's documentation​

kubectl explain deployment.spec.replicas
kubectl explain deployment.spec.strategy

This command explains each field, without leaving the terminal.

🚀 4. Validate before applying​

kubectl apply -f meu-app.yaml --dry-run=server
kubectl diff -f meu-app.yaml

The first asks the cluster to validate the file without creating anything. The second shows what will change compared to what already exists.

🚀 5. Apply​

kubectl apply -f meu-app.yaml

apply creates the resource if it does not exist and updates it if it does. Use the same command to create and to change.

🚀 6. Change an existing resource​

There are two ways:

WayCommandWhen to use
Edit the fileChange meu-app.yaml and run kubectl apply -f meu-app.yamlRecommended: the file remains the source of truth
Edit directly in the clusterkubectl edit deployment meu-appQuick adjustments and testing

🚀 7. Delete using the file​

kubectl delete -f meu-app.yaml

✅ How to know it worked​

kubectl get deployment meu-app

The Deployment appears with READY 2/2.

🔒 Best practices​

  • Store YAML files in a repository (Git), without passwords. For sensitive data, use Secrets.
  • Run kubectl diff before applying in production.
  • Use one file per application, with clear names.

❗ Common problems​

MessageCauseSolution
error converting YAML to JSONIndentation error or Tab in the fileReview the spaces (2 per level)
unknown field "..."Field misspelled or in the wrong placeUse kubectl explain to see the correct name
no matches for kindIncorrect apiVersion or kindCheck with kubectl api-resources
selector does not match template labelsmatchLabels differs from labelsUse the same label in both
field is immutableField that cannot be changed after creationDelete and create again

🧹 Cleanup​

kubectl delete -f meu-app.yaml
rm meu-app.yaml

🧠 Questions?​

Talk to our SaveinCloud technical support team!