Pods in Kubernetes☸️ - CKA

Pods in Kubernetes☸️ - CKA

Imperative & Declarative Approach

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: nginx-pod
  labels:
    env: demo
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80

Explanation:

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

    How to Check apiVersion?
    You can use kubectl explain pods and there you can find which apiVersion is your client utility i.e., kubectl is using.
  • 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>
    

    The approach of creating a YAML file and then apply if through kubectl command is a declarative approach, let’s see how we can do with an imperative approach or can say direct command for deploying commands.

Imperative Approach

In Kubernetes, an imperative command is used to perform actions directly from the command line without needing to write a YAML manifest beforehand.

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

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.

kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml

This command saves the YAML configuration of the existing Pod to a file named pod.yaml. Here dry-run will not apply the changes just run in a demo form and -o is used to specify the language for pod configuration file such as YAML or JSON.

ls

You will find a new file pod.yaml.

Different Commands

  • To edit the YAML file in object only without requirement running the apply command, see below command -
kubectl edit pod <pod-name>
  • To fetch details of any component of kuberenetes
kubectl explain <component>
'
<component> can be
pod
service
rs or replicaSet
deploy
ds or daemonSet
etc.
'
  • kubectl create -f <file-name> --> only creates pod

  • kubectl apply -f <file-name> --> creates and updates also