Ultimate Guide to Kubernetes: Learn All About Pods

Abhishek JhaAbhishek Jha
4 min read

Introduction

Think of a pod as a single unit of work in Kubernetes. It's the smallest and simplest thing you can create or manage. A pod is like a tiny ship that carries containers, which are the actual applications.

Imagine you have an app, like a website or a game. To run this app, you put it inside a container, which is like a box with everything the app needs to work. Then, you put this container inside a pod, which takes care of running the app and keeping it connected to the rest of the system.

Pods can hold one or more containers that share resources like storage and network. They work together as if they are part of the same ship. If one container fails, the pod can help restart it or replace it, making sure your app keeps running smoothly.

In short, pods in Kubernetes are like little ships carrying containers, ensuring your applications run reliably and efficiently.

Running Container with Docker.

Before understanding pod let's see how we can run container with docker.

Note: I hope you understand docker if in case you want to understand docker please follow this guide. Docker zero to hero

Let's say we have a Nginx container build available at the docker host.

  1. Pull the Nginx image:

     docker pull nginx
    
  1. Run the container:

     docker run --name mynginx -p 80:80 -d nginx
    

    Let's break down the options:

    • --name mynginx: This assigns a name to the container for easier identification. You can choose any name you like.

    • -p 80:80: This maps port 80 on the host machine (your computer) to port 80 inside the container. You can change the host port (e.g., 8080) if port 80 is already in use.

    • -d: This runs the container in detached mode, meaning it runs in the background and doesn't block your terminal.

Validation

  1. Checking if the Nginx container is running or not

     docker ps -a
    

  2. check if we can curl the Nginx server from localhost.

Pod

Now we understand how we can run the container with docker. Let's try to look at how to run a container inside the pod in K8s.

  1. Create the YAML file:

    Create a new file named nginx-pod.yaml with the following content:

     apiVersion: v1
     kind: Pod
     metadata:
       name: nginx-pod
     spec:
       containers:
       - name: nginx
         image: nginx:latest  # Replace with specific version if needed
         ports:
         - containerPort: 80
       restartPolicy: Always  # Optional: Restarts the pod automatically on failure
    

    Explanation:

    • apiVersion: Specifies the Kubernetes API version used (V1 in this case).

    • kind: Indicates the type of resource being defined (Pod in this case).

    • metadata: Defines metadata for the pod, including its name (nginx-pod).

    • spec: Defines the desired state of the pod, including its containers and configuration.

    • containers: Defines the containers running inside the pod.

      • name: Name assigned to the container (nginx).

      • image: Specifies the Docker image to use (nginx:latest).

      • ports: Exposes the container port (80).

    • restartPolicy: (Optional) Configures restart behaviour for the pod (Always will automatically restart).

  2. Apply the YAML file:

    Use the kubectl apply command to deploy the pod based on your YAML file:

     kubectl apply -f nginx-pod.yaml
    

Validation

  1. Checking Pod status after applying

Conclusion

In conclusion, understanding Kubernetes pods is essential for efficiently managing containerized applications. Pods serve as the fundamental building blocks in Kubernetes, encapsulating one or more containers to ensure seamless operation and resource sharing. By mastering the creation and management of pods, you can ensure your applications run reliably and efficiently, leveraging Kubernetes' powerful orchestration capabilities. Whether you're transitioning from Docker or starting fresh with Kubernetes, grasping the concept of pods will significantly enhance your ability to deploy, scale, and maintain your applications in a cloud-native environment.

In the coming articles, we will look at all other core concepts of K8s so that we can understand k8s in-depth and able to manage the Production Graded Cluster in your organisation.

Till Then happy Learning and bye..

0
Subscribe to my newsletter

Read articles from Abhishek Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Jha
Abhishek Jha