Pods in Kubernetes

Pods in Kubernetes

ยท

3 min read

What is a Pod?

Pods are the smallest and most basic deployable objects in Kubernetes. A Pod represents a single instance of a running process in your Kubernetes cluster

Pods contain one or more containers, such as Docker containers. When a Pod runs multiple containers, the containers are managed as a single entity and share the Pod's resources.

Pods also contain shared networking and storage resources for their containers:

  • Network: Pods are automatically assigned unique IP addresses. Pod containers share the same network namespace, including IP address and network ports. Containers in a Pod communicate with each other inside the Pod on localhost.
  • Storage: Pods can specify a set of shared storage volumes that can be shared among the containers.

How to create the Pod

Pods we create two ways either command line or through manifest files,

  • Sample manifest file

    apiVersion: v1
    kind: Pod
    metadata:
    name: static-web
    labels:
      role: myapp
    spec:
    containers:
      - name: web
        image: nginx
        ports:
          - name: web
            containerPort: 80
            protocol: TCP
    
  • Create the pod

Kubectl create -f <file.yaml>
  • How to check the pod status
kubectl get pod
  • How to check the pod labels
    kubectl get pod --show-labels
    
  • How to delete the pod
    Kubectl delete pod <pod name>
    

When you run kubectl get pod to inspect a Pod running on your cluster, a Pod can be in one of the following possible phases:

Pending: Pod has been created and accepted by the cluster, but one or more of its containers are not yet running. This phase includes time spent being scheduled on a node and downloading images.

Running: Pod has been bound to a node, and all of the containers have been created. At least one container is running, is in the process of starting, or is restarting.

Succeeded: All containers in the Pod have terminated successfully. Terminated Pods do not restart.

Failed: All containers in the Pod have terminated, and at least one container has terminated in failure. A container "fails" if it exits with a non-zero status.

Unknown: The state of the Pod cannot be determined.

Controlling which nodes a Pod runs on

By default, Pods run on nodes in the default node pool for the cluster. You can configure the node pool a Pod selects explicitly or implicitly:

You can explicitly force a Pod to deploy to a specific node pool by setting a node selector in the Pod manifest. This forces a Pod to run only on Nodes in that node pool.

You can specify resource requests for the containers you run. The Pod will only run on nodes that satisfy the resource requests. For instance, if the Pod definition includes a container that requires four CPUs, the Service will not select Pods running on Nodes with two CPUs.

Sample YAML file assigning Pods to Nodes

apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/os
            operator: In
            values:
            - linux
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: another-node-label-key
            operator: In
            values:
            - another-node-label-value
  containers:
  - name: with-node-affinity
    image: k8s.gcr.io/pause:2.0

Reference ๐Ÿ‘‰ Assigning Pods to Nodes

Assign Memory Resources to Containers and Pods

By default pod, there is no limitation for memory and CPU. You can set a limit to control the memory and CPU

It is strongly recommended that you configure limits for your Pods, based on the requirements of the actual workloads

apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
  - name: app
    image: images.my-company.example/app:v4
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
  - name: log-aggregator
    image: images.my-company.example/log-aggregator:v6
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

Reference ๐Ÿ‘‰ Resource Management for Pods

High level overview of the pod

POD1.jpg

Hope you have got an idea about Kubernetes Pod and how to create it

Happy Learning โœŒ

Thank you !๐Ÿ™

Did you find this article valuable?

Support Cloudnloud Tech Community by becoming a sponsor. Any amount is appreciated!

ย