Pod

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

A Pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

Think of a Pod as a wrapper around containers, ensuring they work as a single unit.

Single Container Pod

We can start or run a particular image in a pod using kubectl run command.

➜ kubectl run nginx --image=nginx --port=80
pod/nginx created

This command will create a pod named nginx will single container named nginx using image nginx and open port 80. We can see the details using command kubectl describe.

➜ kubectl describe pod nginx 
Name:             nginx
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Tue, 04 Feb 2025 12:48:15 +0700
Labels:           run=nginx
Annotations:      <none>
Status:           Running
IP:               10.244.0.94
IPs:
  IP:  10.244.0.94
Containers:
  nginx:
    Container ID:   docker://6f804d723c05e62267a7b70896ec07dac53991f4e152874a8e713144c57c02d2
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0a399eb16751829e1af26fea27b20c3ec28d7ab1fb72182879dcae1cca21206a
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 04 Feb 2025 12:48:19 +0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6bc8f (ro)
...

We can also use yaml file to define pod configuration. The command kubectl run we previously use is more or less equal with this configuration below.

Explanation:

  • metadata.name: nginx: The name of the Pod (must be unique in the namespace).

  • spec: The specification of the Pod.

  • containers: List of containers that will run inside this Pod.

  • - name: nginx: The name of the container inside the Pod.

  • image: nginx: The Docker image used to run the container (pulled from Docker Hub by default).

  • ports: Defines which ports are opened on the container.

  • containerPort: 80: The container listens on port 80.

To apply it as usual we can use kubectl apply -f <file> command.

Multiple Container Pod

To create a pod that have multiple containers we just need to add new entries in containers section. Create new file pod.yaml and put example configuration below.

Apply the configuration file using kubectl apply and let see the details using kubectl describe command.

As you can see above we have 2 containers with name nginx and logger.

We can get the container logs using this command below.

The command above will print logs from pod nginx-with-logger specifically in container logger. To print logs from all container we can add option --all-containers=true.

Access the Pod

To access the pod we can use kubectl port-forward command.

This command will forward traffic from port 8080 in our local machine to pod named nginx-with-logger port 80.

Lets try to access it using curl and we should see default response from nginx server.

Delete Pod

To delete pod we can use kubectl delete pods <pod-name> command.

References

Last updated