Open In App

Kubernetes Pods

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Kubernetes is an open-source container orchestration system mainly used for automated software deployment, management, and scaling. Kubernetes is also known as K8s. Kubernetes was originally developed by Google but it is now being maintained by Cloud Native Computing Foundation. It was originally designed to be interfaced with only Docker runtime but it now works with containers and CRI-O also. The main purpose of Kubernetes is to automate the operational tasks of container management. It is included with built-in commands for the deployment of applications and rolling out the required changes in the application. It is currently being used by companies like Google, Spotify, and Capital One.

What are Kubernetes Pods?

A pod is the smallest unit that exists in Kubernetes. It is similar to that of tokens in C or C++ language. A specific pod can have one or more applications. The nature of Pods is ephemeral this means that in any case if a pod fails then Kubernetes can and will automatically create a new replica/ duplicate of the said pod and continue the operation. The pods have the capacity to include one or more containers based on the requirement. The containers can even be Docker containers. The Pods in Kubernetes provide environmental dependencies which include persistent storage volumes which means it is permanent and is available to all pods in the said cluster and even configuration data that is required to run the container within the pod. 

Anatomy of a Kubernetes Pod: Demystifying the Key Components

In the following diagram, you can see cube-like structures they are called containers each of the containers will have one container. The cylinder-like structure is called volume where the data of the containers will be stored and the circles are called pods. The pods are the smallest unit in recognizable unit in Kubernetes that is the reason where Kubernetes will take care of the pods and pods will take care of the containers.

PODS

 

Introduction to Kubernetes Pods: The Building Blocks of Your Microservices

A pod in a Kubernetes cluster indicates a process that is currently operating, and a pod may contain one or more containers. All of those containers share a single IP address, as well as the pod’s storage, network, and any other requirements. A pod is a collection of one or more running containers, allowing for simple container movement within a cluster. 

The creation of a pod is due to a workload resource called controller, which means rollout, replicate, and health of the pods present in a cluster. If we consider that a node in a cluster fails then a controller detects that the pod on the mode is unresponsive and then replicates a pod or pods on other nodes to carry out the same function. The three mostly used controllers used are Jobs, Deployments, and Stateful Sets. Jobs are used for batch-type jobs that are mostly ephemeral and will run a task to completion. Deployments are used for applications that are stateless and persistent, for example, web services. StatefulSets is used for applications that are both stateful and Persistent like a database. 

If any pod has any/multiple containers then all those are scheduled together on the same server in the cluster either a physical server or VM. All the containers present in the pods will share their resources and dependencies. All these clusters can coordinate their termination and execution.  For instance, if a pod contains an init container then it runs before the application container runs leveling or setting up the required environment for applications to follow.  Generally, pods are created by controllers that can automatically manage the pod lifecycle. The pod life cycle included replacing failed pods, replicating the pods when necessary, and eliminating the pod once the purpose was completed. Controllers use the information present in the pod templates to create the pods. 

Kubernetes Pod Lifecycle: From Creation to Termination

We submit a pod request to the API server using the pod definition file, and the API server records the information for the pod in ETCD. The scheduler will schedule the unscheduled pod to the available node as soon as it is discovered. When a pod is scheduled, a running Kubelet on a node launches Docker, which then launches the container. 

The entire pod lifecycle stores in ETCD 

Scaling and Replication: Scaling Your Kubernetes Pods with Ease

Scaling and replication of the pods plays a major role in kubernetes by which we can efficiently manage the incoming traffic. WIth the help of kubernetes you perform the scaling manually and automatically with the according to the requirements.

1. Manual Kubernetes Pod Scaling

You can do the manual kubernetes pod scaling by using the kind called replicaset,repllication controller or deployment.

ReplicaSets:

A ReplicaSet is a key component of a Kubernetes application. It is a controller that ensures that a specified number of pod replicas are running at any given time. It is used to automatically replace any pods that fail, get deleted, or are terminated, ensuring the desired number of replicas are always available to serve requests. When a ReplicaSet is created, it creates the desired number of replicas of the specified pod.TO know more refer to the Kubernetes – Creating a ReplicaSet

2. Automatic scaling

For automatic scaling of pods kubernetes provides a service called horizontal pod autoscaling HPA

Networking Your Kubernetes Pods: Connecting Your Microservices

Networking in the kubernetes will plays major role in the kubernets where it will establish the communication between the two microservices like pods by which each of them can commnuicatewith each other following are some if networking concepts which are used in the kubernetes.

1.Cluster Networking

Pod-to-pod communication

ClusterIP: It is the default service and its visibility is cluster internal which means it’s not possible to use clusterIP service to reach a micro-service from the internet from outside the cluster.You can establish the connection inside the cluster between two pods.

2. Service Discovery

Kubernetes provide built in DNS for the service discovery. The particular service will assigned to particular DNS based on their service names for example as shown below.

# Accessing a service from within a pod
curl http://example-service.default.svc.cluster.local

3. Ingress

Ingress controller will acts as an load balancer to the kubernetes cluster and also it will also mange the external access to services within the cluster. Following the sample yaml file for the ingress.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: <Name of the Ingress>
spec:
rules:
- host: example.com
http:
paths:
- path: /path
pathType: Prefix
backend:
service:
name: <Name Of The Service>
port:
number: 80

Advanced Pod Techniques: Taking Your Pods to the Next Level

Pods are fundamental units block in the kubernetes following are the some of the advanced concepts of kubernets.

1. Init Containers

These are the contaienrs which are used for setting up the environment for the actually application contianers. This init container will be deployed in the kubernets so they will run one after the another this type of containers are specially designed for the proper functioning of the application containers.

2. Sidecar Containers

Sidecar containers are used for the enhancement of the maincontainers functionality and overall pod efficiency.

  • Sidecar containers will helps you to add the more capabilities to the main containers like logging, monitoring, proxy services or even data processing.
  • Sidecar will manage the processes like queuing, database connections in the background.

3. Resource Requests and Limits

You request the resources from the kubernets cluster for the pod by depending on the incoming traffic resource request like CPU and memory and so on.

4. Liveness and Readiness Probes

Liveness and Readiness Probes are are very useful services in the kubernets cluster which are used for the monitoring the pods health as follows.

  • Liveness probes: Detects if the pod is alive if not it will restart the pods.
  • Readiness probes: Checks the pod is actually ready to serve the traffic.

Pod Management Best Practices: Optimizing Your Pod Strategy

Follwing are the some of the best practices to follow while using the pods in kubernets.

1. Naming and Labeling

This pods name will be used for the further purposes while referring in the kubernetes services.

  1. Names:Try to keep the pod name unique.
  2. Labels: Sort your pods according to function, environment, or any other pertinent criteria using key-value pairs.

Using Kubernets Pods

Let’s create a YAML file for an example image (Nginx) so that it can be deployed as a container.

apiVersion: v1
Kind: pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image:
nginx:latest
ports:
- containers: 80

After creating the yaml file run the below command to deploy it as a container. 

kubectl apply -f <name of yaml file>

Types of Kubernetes Pods

A pod can be defined as the collection of containers and their storage inside a node of a Kubernetes cluster. There is a possibility to create a pod with multiple containers inside them. 

Based on the number of pods present inside them they can be classified as a Single Container Pod or Multi Container Pod.

As the name suggests a single container contains only one container whereas the multi-container pod contains multiple containers. They can be used based on their function and use a case at the respective times. The methods of creation of both types of pods are different. 

How To Scheduled a Kubernetes Pod?

The pod will not be rescheduled to a different node when it expires because it is ephemeral (lasting only a very brief time). , we shouldn’t directly construct or use pods; instead, we should deploy pod-like deployment, replica sets, and daemon sets to maintain the pod with the aid of Kubernetes services. We have to deploy the pods with the help of objects. The main objects are

Characteristics of Kubernetes Pods

A Pod represents the processes running on a cluster. If one pod is limited to a single process then it is possible to report and maintain the health of each process running within the cluster. Every Pod has some unique features like a Unique IP address, persistent storage volumes, and configuration information required to run the working. Mostly all the pods have a single container but many of them will have a few containers working closely together to execute a particular function or activity.

Benefits of Pods

  • If a pod contains many containers working towards a common goal then it is easy for them to communicate and share data among themselves. 
  • We know that all the containers in a pod will have the same network namespace due to which they can locate each other and communicate with the help o localhost. 
  • Pods can communicate with each other by using another pod’s IP address or even by referring to a resource that is located in another pod. 
  • Any pod can even include containers that run when the pod is started mainly to run any operation before the application containers run. 
  • The presence of pods has made it more Scalable as each pod and its replicas can be created and shut down automatically considering the changes in demand. 

How do Kubernetes Pods Communicate With Each Other?

The creation of a pod has made it easy for communication between various components. If a pod contains multiple containers then they can communicate with each other by using a local host. Communication with outside pods can be made by exposing a pod. Communication within the clusters of the same pod is easy because Kubernetes assign a cluster private IP address to each pod in a cluster. 

What are the Basic Kubectl Commands for Pods?

 A pod can be created by using the create command format.

Create Pod 

$ kubectl create -f [FILENAME]

In the [FILENAME], you need to insert the required filename with which you want to create your file, then a new pod with the name GeeksforGeeks will be created.

kubectl create

To Delete Kubernetes Pod

$ kubectl delete -f FILENAME 

here the pod named GeeksforGeeks will be deleted

Kubectl delete pod

To Get Kubernetes Pod 

To see the no.of pods available in the particular namespace can be seen using the below command.

Kubectl get pod <name> --namespace

FAQs On Kubernetes Pods

1. Kubernetes Pods Stuck In Terminating

You can troubleshoot by using following steps

  1. Check for Running Processes: docker ps | grep <pod_id>
  2. Inspect Containers: docker inspect <container_id>
  3. Delete the Pod Forcefully: kubectl delete pod <pod_name> –grace-period=0 –force

After troubleshooting you can perform the task according to the problem.

2.Kubernets Pods Commands

To know more kubernetes pod commands refer to the kubernete cheat sheet.

3. kubernetes Pods Crashloopbackoff

When a container within a Pod keeps crashing and restarting the Pod, it is said to be in the “CrashLoopBackOff” state in Kubernetes. When there is a problem preventing the container from operating correctly, this state is frequently seen in the pod’s status.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads