Writing Kubernetes YAML

Every Kubernetes resource is defined declaratively using YAML or JSON files containing 4 mandatory top-level root fields.

1. The 4 Mandatory Top-Level Root Fields

FieldDescriptionExample Value
apiVersionVersion of Kubernetes API endpoint object schemav1, apps/v1, networking.k8s.io/v1
kindType of object resource being createdPod, Deployment, Service, Ingress
metadataIdentification data (name, namespace, labels, annotations)name: my-web-app
specDesired target operational state of the resourceContainers, replicas, ports, storage...

2. Complete Pod Manifest Example

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
  labels:
    app: frontend
    env: production
spec:
  containers:
    - name: nginx-container
      image: nginx:1.25-alpine
      ports:
        - containerPort: 80
      resources:
        requests:
          memory: "64Mi"
          cpu: "100m"
        limits:
          memory: "128Mi"
          cpu: "250m"

Next Up

Learn Deployments & Rolling Updates: ReplicaSets, declarative scaling, and zero-downtime rollouts.

Next Lesson: Deployments & Rolling Updates →