Important Note -

You do not need to memorize YAML structure. Always copy from official documentation and make your changes: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/


Sample Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: node-demo-app
  template:
    metadata:
      labels:
        app: node-demo-app
    spec:
      containers:
      - name: node-demo-app
        image: daksh12398/node-demo-app:01
        ports:
        - containerPort: 3000

Each Part Explained

apiVersion — which version of Kubernetes API to use. For Deployments always apps/v1

kind — what you are creating. Can be Pod, Deployment, Service etc.

metadata.name — the name of your Deployment or Pod. This is what shows up in kubectl get deployments or kubectl get pods

spec.replicas — how many Pods to run. 2 means 2 Pods always running

selector.matchLabels — tells the Deployment which Pods belong to it. Must match the labels in template.

template — the blueprint for each Pod that gets created

template.metadata.labels — label attached to each Pod. Must match matchLabels above

containers.name — name of the container inside the Pod. This is what you use in kubectl set image

containers.image — your Docker Hub image with tag

containerPort — port your app runs on inside the container


Important Rule — Labels Must Match

matchLabels and template.labels must have the same value. If they do not match, Kubernetes will throw an error: