Browse topics

Pods

Version
1.37
Review
Draft

Summary#

A Pod is the smallest deployable unit in Kubernetes: one or more containers placed together on one node. Containers in the same Pod share its IP address and ports, so they reach each other over localhost, and they can share volumes.

Syntax#

sh
kubectl run NAME --image=IMAGE
kubectl get pod NAME -o yaml
kubectl delete pod NAME
yaml
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
    - name: app
      image: IMAGE

Example#

The client container fetches a page from the server container through localhost. Run it against a test cluster that can pull the image.

sh
(
  set -eu
  ns="ref-pods-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
  kubectl wait -n "$ns" --for=create serviceaccount/default --timeout=60s >/dev/null

  kubectl apply -n "$ns" -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: server
      image: busybox:1.37
      command: ["sh", "-c", "mkdir /www && echo 'hello from the server container' > /www/index.html && exec httpd -f -p 8080 -h /www"]
      readinessProbe:
        tcpSocket:
          port: 8080
        periodSeconds: 1
    - name: client
      image: busybox:1.37
      command: ["sleep", "3600"]
EOF
  kubectl wait -n "$ns" --for=condition=Ready pod/web --timeout=120s >/dev/null

  kubectl get -n "$ns" pod web -o jsonpath='phase={.status.phase}{"\n"}'
  kubectl get -n "$ns" pod web -o jsonpath='{range .spec.containers[*]}container={.name}{"\n"}{end}'
  kubectl exec -n "$ns" web -c client -- wget -qO- http://localhost:8080/
)

Expected output#

Text
pod/web created
phase=Running
container=server
container=client
hello from the server container

Common mistakes#

A Pod you create directly has no controller. If you delete it or its node fails, nothing creates a replacement. Use a Deployment, StatefulSet, DaemonSet or Job for work that must be recreated. Most Pod spec fields are also immutable, so change the controller's Pod template instead of the running Pod.

Version applicability#

The manifest uses the core v1 API and the example targets Kubernetes 1.37. For a helper that must run for the whole life of a Pod, native sidecar containers (an init container with restartPolicy: Always) are stable since Kubernetes 1.33.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0