Kubernetes Tutorial · Kubernetes Basics · Lesson 4 of 26
Pods
The smallest deployable unit: one or more containers sharing a network namespace and volumes.
What a pod is
A pod runs one or more containers that share an IP address, ports (localhost) and volumes. Most pods have
one main container; helpers (sidecars, init containers) share its pod.
apiVersion: v1
kind: Pod
metadata:
name: hello
labels: {app: hello}
spec:
containers:
- name: main
image: busybox:1.37
command: ["sh", "-c", "echo hello from $(hostname); sleep 3600"]
Pods are disposable: when a node dies, its pods are gone. You rarely create pods directly; controllers (Deployments, Jobs, StatefulSets) create and replace them for you.
kubectl create namespace demo-pods > /dev/null
trap 'kubectl delete namespace demo-pods --wait=false > /dev/null' EXIT
cat > pod.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: hello
labels:
app: hello
spec:
initContainers:
- name: prepare
image: busybox:1.37
command: ["sh", "-c", "echo 'prepared by the init container' > /work/msg"]
volumeMounts: [{name: work, mountPath: /work}]
containers:
- name: main
image: busybox:1.37
command: ["sh", "-c", "cat /work/msg; echo 'main container running'; sleep 3600"]
volumeMounts: [{name: work, mountPath: /work}]
volumes:
- name: work
emptyDir: {}
EOF
kubectl -n demo-pods apply -f pod.yaml
kubectl -n demo-pods wait --for=condition=Ready pod/hello --timeout=120s > /dev/null
kubectl -n demo-pods logs hello
kubectl -n demo-pods get pod hello -o jsonpath='{.status.phase} on node {.spec.nodeName}{"\n"}'
Output
pod/hello created prepared by the init container main container running Running on node rc-docs-control-plane
Run this on your computer — see “Get Started” for setup.
See Pods.
Test yourself
Why do you rarely create bare pods in production?
Pods are deprecatedNothing recreates them when they die; controllers like Deployments doPods can't have labelsPods can't run images
Show answer
Answer: Nothing recreates them when they die; controllers like Deployments do
A Deployment's ReplicaSet replaces pods that disappear.