Pages

Tuesday, January 2, 2024

Pods vs Containers: What Are the Differences?

What is a Kubernetes Pod?

A pod is the fundamental building block of Kubernetes. Remember, the core purpose of Kubernetes is to manage containerized applications. As a web application grows, more containers are required to run it. Thus, the need for Kubernetes. These containerized applications need a place to reside — and that place is in a pod.

In order for a pod to be created successfully, it needs to have the following instructions: an apiVersion, a kind (i.e, the type of service it is), metadata, and a list of containers it will manage. That declarative code to create a Pod looks like this:

apiVersion:v1
kind:Pod
metadata:
  name:my-pod 
spec:
  containers:
  - name:busybox
    image:busybox:latest ##This is the container.
restartPolicy:never
The imperative code looks like this: kuectl run my-pod —image=busybox:latest —restart=Never

These two pieces of code do the same thing. However, for complex pod requirements, you will probably want to create it using a YAML file as in the former example. This is by far the simplest example of pod creation. It is important to know that pods are ephemeral. That means once they are created, they can’t be edited. Instead, the pod is killed and restarted with the new configurations.

The essential difference between a pod and a container is that a container resides inside of a pod

What is a Container in Kubernetes?

A container is that actual application (or piece of application) that you wish to run in Kubernetes. It could be a database, a web application, or a backend service. It can be anything, but it must be a Docker container. Other containerization software can be used, but for all practical purposes, they are Docker containers.

What is a Docker Image?

All containers are pulled from images. In the Docker world, think of an image like a Java class. It is a set of instructions on how that container should be created. So in a Kubernetes pod, we are just providing instructions that say, “Hey, create a container based on the image I provided.” The image is being pulled from the Docker registry.

So we know that a pod holds a container, and a container is an image of the particular application we want to run. Now, let’s discuss what a Pod does beyond that.

How Do Pods Manage Containers?

Pods let you manage the entire lifecycle of your containerized application. Kubernetes is amazing in this respect because it provides a single place to configure an application’s resources, replicas, ports, and more. Here is an example of assigning CPU to a container:

apiVersion: v1
kind: Pod
metadata:
  name: my-web-app-pod
spec:
  containers:
  - name: my-web-app-container
    image: nginx:latest
    resources:
      limits:
cpu: “1"
memory: “200Mi"
      requests:
        cpu: “0.5”
        memory: “100Mi"

In the example above, we are assigning CPU and memory allocation to a container. In other words, the pod is managing how many resources the application can consume. 

If we were not using Kubernetes, it is very possible this application could take up 100% of the CPU or memory and crash the server — but Kubernetes is doing its job. It is managing the application by assigning the desired amount of resources, along with an upper bound. 

A pod can also provide command line arguments for an image to execute upon startup. A pod can also provide security context to a container. For example, a pod can tell the container, “When you finally start up, only run as User 2000, and don’t let the user gain root access!” That would look something like this:

apiVersion:v1
kind:Pod
metadata:
  name:my-web-app-pod
spec:
  containers:
  - name:my-web-app-container
    image:nginx:latest
    securityContext:
      runAsUser:2000
      allowPrivilegeEscalation:false


A pod’s whole reason for existing is to manage a container (or containers) within it

1. Overview

Kubernetes is an open-source container orchestration platform. It eliminates the manual processes associated with deploying and scaling containerized applications. Furthermore, this system allows you to manage and coordinate containerized applications on a cluster of machines through small units called pods. A pod typically includes several containers, which together form a functional unit.

In this article, we’ll learn the difference between a pod and a container in Kubernetes.

2. Containers

Containers are standardized executable components that combine application source code with operating system libraries. A container could be a database, a web application, or a backend service…

In Kubernetes, each container is isolated from other processes and runs on a computer, physical server, or virtual machine. Furthermore, containers are pretty lightweight, fast, and portable because, unlike a virtual machine, containers do not need to include an operating system in each instance and can instead leverage the functionalities and resources of the host machine’s operating system.

3. Pods

The pod is one of the fundamentals of Kubernetes because it’s the primary component that developers may manipulate. It represents a set of processes running within a cluster node. A pod is, therefore, a basic execution unit of a Kubernetes application. It is the smallest and simplest object model. a pod within a node has:

A local IP address.

One or more Linux containers. For instance, Docker is commonly used as a container runtime.

One or more volumes that are associated with these containers are persistent storage resources.

4. What’s the Difference Between a Pod and a Container?

Because we’re not supposed to pack multiple processes into a single container, we need a higher-level structure that will allow us to tie and wrap containers together and manage them as a single unit. This is the reasoning behind the pods. Simply put, a Kubernetes pod is a collection of containers.

Furthermore, a container pod allows it to run closely related processes together. And provides them with almost the same environment, as if they were all running in a single container while keeping them virtually isolated. This way, we can get the best of both worlds. We can take advantage of all the functionality containers provide while giving processes the illusion of working together.

Pods provide two types of shared resources for their containers: network and storage.

4.1. Network

Each pod in Kubernetes is identified by a unique IP address. Furthermore, All Pod containers share the same network namespace including IP address and network ports.

To communicate inside the pod, containers can simply exchange using localhost. However, when pod containers communicate with components outside the pod, they must share how to use network resources such as ports.

4.2. Storage

We can instantiate a set of shared storage volumes for a pod that will be accessible by all of its containers. This feature allows containers to share data efficiently. Also, storage volumes allow the pod’s persistent data to survive in case one of the containers has problems.

5. Pods and Containers Life Cycle

Kubernetes automatically manages the lifecycle of containers and distributes them across the entire hosting infrastructure. Also, it has the ability to restart containers automatically whenever they fail. Kubernetes provides this feature through Restart Policies, which allow the user to define when a pod’s containers should be restarted.

Let’s see the different states of a pod in relation to the containers:

The Pending state: the very first thing that happens when a pod is created is that it enters a state called pending. In this state, the pod is ready to run but has not yet been assigned to a server. There are probably several machines that are ready to receive this pod. It’s the role of the scheduler to examine the resource needs of the pod (CPU capacity, memory, etc.) and to assign it to a particular machine

The Running state: a pod reaches this state when it has been assigned to a machine, the images of the containers that make up the pod have been downloaded, and all the containers have been started

The Succeeded state: this state occurs when all containers in the pod have been successfully stopped and are not going to be restarted

The Failed state: this state is reached when all containers in the pod are stopped, and at least one of the containers has terminated with an error

The Unknown state: This mainly occurs when the pod fails to communicate with the node it is supposed to be running on. In this case, it’s impossible to know the pod status.

https://intellipaat.com/blog/interview-question/kubernetes-interview-questions-answers/

###

Pods and containers are both fundamental concepts in container orchestration systems like Kubernetes, but they serve different purposes:

1. **Containers**:
   - **Definition**: A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and dependencies.
   
   - **Characteristics**:
     - Containers provide a consistent and isolated runtime environment for applications, ensuring that they run consistently across different environments.
     - They leverage operating system-level virtualization to package and isolate applications, allowing multiple containers to run on the same host without interference.
     - Common container technologies include Docker, containerd, and rkt.
   
   - **Use Cases**:
     - Containers are used for packaging and deploying applications in a portable and reproducible manner.
     - They are commonly used in microservices architectures, cloud-native applications, and DevOps workflows to streamline development, deployment, and operations.
2. **Pods**:
   - **Definition**: A pod is the smallest deployable unit in Kubernetes, consisting of one or more containers that are tightly coupled and share the same network namespace, IPC namespace, and other resources.
   
   - **Characteristics**:
     - Pods provide a logical abstraction that encapsulates one or more containers and represents a single instance of an application or a microservice.
     - Containers within the same pod share the same network interface and IP address, allowing them to communicate with each other using localhost.
     - Pods are scheduled and managed by Kubernetes, which ensures that they are deployed and run on appropriate nodes in the cluster.
   
   - **Use Cases**:
     - Pods are used for deploying multi-container applications where the containers need to share resources or work together closely.
     - They are commonly used for sidecar container patterns, where a main application container is paired with one or more auxiliary containers to provide additional functionality (e.g., logging, monitoring, networking).

In summary, containers are the building blocks for packaging and running applications, while pods are higher-level abstractions that represent one or more containers running together and sharing resources within a Kubernetes cluster. Pods provide a more flexible and scalable approach to deploying and managing multi-container applications, allowing developers to define complex application topologies and deployment patterns.

 

No comments:

Post a Comment