What is a Kubernetes Pod?
Let's have a look into the smallest k8s deployable unit.
A pod is the smallest deployable unit in Kubernetes. This little thing wraps one or more Containers into actual running processes on the operating system’s kernel. Since all containers form a single unit, all of the underlying processes share the same network namespace, IP address, and storage volumes.
Most pods run a single container, to ensure isolation, which enables us to stop, start, scale in and out the number of pods individually. If we need, for example, to scale only application A, then having application B on the same pod would not be ideal.
Still, at some cases it does make sense to have multiple containers on the same model, with what is known as a “sidecar pattern”, where both containers work in collaboration with each other. An example of this would be a monitoring agent, that scrapes data from the main application and sends them to a tool such as Prometheus.
By nature, pods are ephemeral. Kubernetes can create, destroy, and reschedule them at any time. We would rarely interact with pods directly; instead, and ideally, we would instruct Kubernetes on how pods should look like and let Kubernetes itself do the job for us. That can be done with objects such as Deployments and StatefulSets.
All k8s objects are described in YAML manifests, so below is a very short example of a pod definition:
apiVersion: v1kind: Podmetadata: name: nginx-pod labels: app: nginxspec: containers: - name: nginx image: nginx:1.25 ports: - containerPort: 80Let’s see what those mean:
- apiVersion and kind tell Kubernetes exactly what object we’re defining;
- metadata holds the pod’s name and labels (optional), which are key-value pairs used to organise and select pods (example: a Services uses labels to know which pods to route).
- spec is what describes the desired state of the pod. In this example we declare a single container running the
nginx:1.25image and listening on port 80.
A pod can be on different states:
- Pending: the pod definition has been accepted, but has not yet been scheduled;
- Running: at least one container is active;
- Succeeded: all containers terminated successfully;
- Failed: at least one container terminated with an error;
- Unknown: the pod’s state cannot be determined for any reason.