Kubernetes Pods
Hello there! In this micro-blog, I explore what a Kubernetes Pod is and how we work with them. Let's get started.
What is a Kubernetes Pod?
According to Kubernetes documentation, Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes. 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. A Pod's contents are always co-located and co-scheduled, and run in a shared context.
Note: Containers in a pod share networking and storage.
Deploying a pod
The process of deploying a pod is pretty simple. We need to define a YAML file and then apply this configuration to the Kubernetes cluster. If you have not deployed your cluster yet, you can do so by following my blogpost. The sample YAML file that you would need in order to deploy a pod is provided below.
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
In order to deploy the pod on to the cluster, go to the controller node and create the YAML file in a directory of your choice. Issue the following command to apply the file to the cluster.kubectl apply -f pod.yaml
In order to introspect pod information, use the following command.kubectl get pods -o wide
Check pod details by using the following command.kubectl describe pods myapp-pod
Use the following command in order to enter the shell of the container in a pod.kubectl exec -it myapp-pod -- sh
Use the following command to delete a pod from the cluster.kubectl delete -f pod.yml
Working with pods
Pods in a Kubernetes cluster are used in two main ways:
- Pods that run a single container. The "one-container-per-Pod" model is the most common Kubernetes use case; in this case, you can think of a Pod as a wrapper around a single container; Kubernetes manages Pods rather than managing the containers directly.
- Pods that run multiple containers that need to work together. A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers form a single cohesive unit of service—for example, one container serving data stored in a shared volume to the public, while a separate sidecar container refreshes or updates those files. The Pod wraps these containers, storage resources, and an ephemeral network identity together as a single unit.
In Kubernetes, we rarely create and deploy individual pods. This is because Pods are designed as relatively ephemeral, disposable entities. Instead, we use workload resources that create and manage a pod for you. A controller for the resource handles replication and rollout and automatic healing in case of Pod failure. For example, if a Node fails, a controller notices that Pods on that Node have stopped working and creates a replacement Pod. The scheduler places the replacement Pod onto a healthy Node. Some examples of workload resources are:
- DaemonSets
- ReplicaSets
- Deployments
- StatefulSets
We will discuss more about these workload resources in my upcoming blog posts.
Hope this helps! ☺️