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
| Field | Description | Example Value |
|---|---|---|
apiVersion | Version of Kubernetes API endpoint object schema | v1, apps/v1, networking.k8s.io/v1 |
kind | Type of object resource being created | Pod, Deployment, Service, Ingress |
metadata | Identification data (name, namespace, labels, annotations) | name: my-web-app |
spec | Desired target operational state of the resource | Containers, 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 →