Comprehensive Guide to Kubernetes Pods: Day 7 of 40-Day Kubernetes Series

Comprehensive Guide to Kubernetes Pods: Day 7 of 40-Day Kubernetes Series

In this blog, we dive deep into one of the foundational concepts in Kubernetes Pods

What is a Kubernetes Pod?

A Pod is the smallest and simplest unit in the Kubernetes object model. A Pod represents a single instance of a running process in your cluster. Although a Pod can run one or more containers, the most common scenario is for a Pod to run a single container.

Key characteristics of Pods:

  • Shared Networking: Containers inside a Pod share the same network namespace, including IP address and port space, allowing them to communicate with each other easily.

  • Shared Storage: Pods can also have storage resources shared between containers.

  • Ephemeral: Pods are designed to be temporary; they can be killed or restarted by the Kubernetes control plane when necessary (e.g., for scaling or updates).

Types of Pods

There are two main types of Pods in Kubernetes:

  1. Single Container Pods: The most common use case where a Pod contains a single container running a specific application.

  2. Multi-Container Pods: A less common scenario where multiple containers work together in the same Pod. These containers share the same environment and work closely to support the application. They can, for example, share files and communicate with each other using inter-process communication (IPC).

Creating a Pod (YAML Configuration)

To create a Pod in Kubernetes, we define it in a YAML file and then apply it to the cluster.

Here’s a basic example of a Pod definition with a single container.

apiVersion: v1

kind: Pod

metadata:

name: my-first-pod

labels:

app: my-app

spec:

containers:

- name: my-container

image: nginx

ports:

- containerPort: 80

Explanation:

  • apiVersion: Specifies the API version to be used by Kubernetes.

  • kind: Defines the type of object (Pod in this case).

  • metadata: Contains information like name and labels for the Pod.

  • spec: Describes the configuration of the containers inside the Pod.

  • containers: Lists the containers within the Pod. In this case, we are using the nginx image.

Steps to Create the Pod:

  1. Save the above configuration to a file, say my-pod.yaml.

  2. Run the following command to create the Pod:

    kubectl apply -f my-pod.yaml

  3. Verify the Pod is running:

    kubectl get pods

Working with Pods (Commands & Examples)

Once a Pod is created, there are several commands you can use to interact with it.

  1. List All Pods:

    kubectl get pods

  2. Get Pod Details:

    kubectl describe pod <pod-name>

  3. Delete a Pod:

    kubectl delete pod <pod-name>

  4. Check Logs:

    kubectl logs <pod-name>

  5. Exec into a Running Pod (Useful for debugging):

    kubectl exec -it -- /bin/bash <pod-name>

Task

Task 1: Create a Pod Using Imperative Command

In Kubernetes, an imperative command is used to perform actions directly from the command line without needing to write a YAML manifest beforehand. For Task 1, we will create a Pod using the Nginx image.

Command to Create the Pod with Nginx Image:

kubectl run nginx --image=nginx --restart=Never

Explanation:

  • kubectl run: Creates a new resource (in this case, a Pod).

  • nginx: The name of the Pod.

  • --image=nginx: Specifies the container image (nginx) to be used.

  • --restart=Never: Ensures that the resource created is a Pod (since by default, the kubectl run command creates a deployment with automatic restart unless specified otherwise).

Verify the Pod is Running:

kubectl get pods

Task 2: Create YAML from the Nginx Pod

Now that we've created the nginx Pod, we'll generate the YAML configuration for it and modify the Pod's name before creating a new Pod.

Step 1: Generate YAML from Existing Nginx Pod

kubectl get pod nginx -o yaml > nginx-pod.yaml

This command saves the YAML configuration of the existing Pod to a file named nginx-pod.yaml.

Step 2: Update the Pod Name in the YAML

Open the nginx-pod.yaml file and modify the metadata.name field from nginx to nginx-new.

apiVersion: v1

kind: Pod

metadata:

name: nginx-new

labels:

run: nginx

spec:

containers:

image: nginx

name: nginx

Step 3: Create a New Pod Using the Modified YAML

Now, we will create a new Pod with the updated name (nginx-new) using the modified YAML.

kubectl apply -f nginx-pod.yaml

Verify the New Pod is Running:

kubectl get pods

You should see both nginx and nginx-new Pods running.

Task 3: Fix YAML Errors for Redis Pod

We are given a faulty YAML manifest for a Redis Pod. Let's try applying it first and then troubleshoot any errors.

Step 1: Apply the Given YAML

apiVersion: v1 0

kind: Pod

metadata:

labels:

app: test

name: redis

spec:

containers:

image: rediss

name: redis

Apply the YAML:

kubectl apply -f redis-pod.yaml

Error Message:

Error from server (InvalidImageName): Pod "rediss" is invalid: spec.containers[0].image: Invalid value: "rediss": could not find an image "rediss" locally or in a public registry

Explanation:

The error is caused by an invalid image name rediss. The correct image name is redis.

Step 2: Fix the Image Name

Edit the YAML file to update the container image from rediss to redis:

apiVersion: v1

kind: Pod

metadata:

labels:

app: test

name: redis

spec:

containers:

image: redis

name: redis

Step 3: Apply the Corrected YAML

kubectl apply -f redis-pod.yaml

Verify the Pod is Running:

kubectl get pods

At this point, the redis Pod should be running successfully.

Conclusion

Kubernetes Pods are fundamental building blocks for running containerized applications, and mastering them is key to becoming proficient in Kubernetes. From defining Pods using YAML files to managing, debugging, and monitoring them with kubectl commands, understanding how to work with Pods helps you effectively manage applications running in your Kubernetes cluster. By following best practices like setting resource limits and leveraging auto-scaling, you ensure that your applications are scalable, resilient, and efficient.

Reference

Video:

https://www.youtube.com/watch?v=_f9ql2Y5Xcc&list=PLl4APkPHzsUUOkOv3i62UidrLmSB8DcGC&index=11