Skip to main content

Pods in Kubernetes

A very common beginner's mistake is to think that Kubernetes manages containers (Docker containers) directly.

Kubernetes does not touch bare containers. The most atomic and minuscule unit that K8S knows how to schedule, turn on, clone or kill is called a POD.

What is a Pod?

A Pod is a wrapper or capsule. Think of the Pod as an "apartment" and the containers as the "humans" living inside. Eminently, a Pod exists uniquely and exclusively to harbor and run a Docker container inside.

Why does this extra layer exist?

Because sometimes (very few, but it happens), two containers are so tightly and genetically linked that they need to physically cohabitate in the same isolated environment.

Imagine a main container running your Web Server, but it mandatorily requires a "Helper" sidecar container (a log-monitor that intercepts everything the server writes to upload it to the base control). Kubernetes encapsulates both within a single Pod.

Thus, Kubernetes guarantees that the two will always live on the same physical machine (the same Worker Node), they will share the exact same and single IP address ("localhost"), and if one is killed, the whole family will disappear and an exactly identical one will be created in another neighborhood.


Writing a Pod manifest (.yaml)

In Kubernetes, instead of typing fifty crazy characters on the command line as we did in Docker, we define absolutely "What We Want" Declaratively in writing. Descriptive and imperative .yaml files (Just like a menu you send to your boss).

# We define the type of object we are requesting to create.
apiVersion: v1
kind: Pod

# We give it a real identity: its name for the community and trackable labels to quickly locate it.
metadata:
name: pod-my-nginx-page
labels:
application: webfront

# We define and break down WHO will live in this apartment
spec:
containers:
- name: container-nginx # My first human
image: nginx:latest # Comes direct and magically from worldwide DockerHub
ports:
- containerPort: 80 # To open its own public window

Running it in real life

To deliver "the restaurant order" to our beloved Master Node so that he delegates to do this magic to his slaves (workers), you must talk to him with the famous administrative tool or magic wand: kubectl

kubectl apply -f pod-form-file.yml

And to see them all proudly created in the military base?

kubectl get pods