Skip to content

Kubernetes Deployment YAML explained

November 2, 2026 · 2 min read

A Deployment YAML tells Kubernetes which container to run and how many copies to keep alive. You save it as a file, run kubectl apply -f, and change the copy count by editing one line and applying again.

A Deployment, line by line

Save this as kc-site.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kc-site
spec:
  replicas: 3
  selector:
    matchLabels:
      app: kc-site
  template:
    metadata:
      labels:
        app: kc-site
    spec:
      containers:
        - name: nginx
          image: nginx:1.29
          ports:
            - containerPort: 80

Create it, then scale it by changing replicas: 3 to replicas: 4 and applying again:

kubectl apply -f kc-site.yaml
# deployment.apps/kc-site created

kubectl apply -f kc-site.yaml   # after editing replicas to 4
# deployment.apps/kc-site configured

kubectl get pods -l app=kc-site

What each part does

  • apiVersion: apps/v1 and kind: Deployment say which type of object this is. Deployments live in the apps API group.
  • metadata.name is the name of the Deployment itself.
  • spec.replicas is how many pods to keep running. Kubernetes starts or deletes pods until the count matches.
  • selector.matchLabels says which pods belong to this Deployment: every pod labeled app: kc-site.
  • template is the pattern for each pod. Its metadata.labels tag the pods, and its spec.containers list the containers, here one nginx container listening on port 80. The pod has no name because Kubernetes generates one for each.

When you apply the edited file, Kubernetes compares it with what is running and changes only the difference. That is why the second apply prints configured and just one new pod starts. Keep the file in Git as the record of what you want.

You can also generate a starting file instead of typing it:

kubectl create deployment kc-site --image=nginx:1.29 --replicas=3 --dry-run=client -o yaml

Common mistake

The labels under template.metadata.labels must include everything in selector.matchLabels. If you write app: kc-shop in the template but leave app: kc-site in the selector, the API server rejects the file with a "selector does not match template labels" error. Make the two match.

Also, a Deployment's selector cannot be changed after creation. If you need a different one, run kubectl delete -f kc-site.yaml and apply the file again.

Takeaway

A Deployment is a selector, a pod template, and a replica count. Edit the file, run kubectl apply -f, and Kubernetes makes the cluster match.

More on DevOps & Cloud