Pod

Pod

Pods are the smallest and simplest Kubernetes objects. They represent a single instance of a running process in your cluster and are the building blocks of Kubernetes applications. Each pod contains one or more containers, such as Docker containers, which share the pod's resources and network.

Key Concepts

  1. Single Container Pods:

    • Most pods contain a single container. This is the most common use case, where each pod runs one containerized application.
  2. Multi-Container Pods:

    • Pods can contain multiple containers that need to work together. These containers share storage and network resources and can communicate with each other through inter-process communications.
  3. Shared Resources:

    • Containers within a pod share an IP address and port space, and they can also share storage volumes. This makes it easy for the containers to interact closely.
  4. Ephemeral Nature:

    • Pods are designed to be ephemeral. They are created, destroyed, and re-created as needed by the Kubernetes control plane. This helps in maintaining the desired state of your application.

Checing Pods in Namespace

To see the pods running in your current namespace, use the following command:

kubectl get pods

If you need to specify a different namespace, add the -n flag followed by the namespace name:

kubectl get pods -n namespace-name

Replace namespace-name with the name of your desired namespace. This will list all the pods in the specified namespace, providing details about their current state.

Declarative vs. Imperative Management

Kubernetes allows you to manage pods using two primary approaches: declarative and imperative. Understanding these approaches will help you effectively manage your pods.

Imperative Management

Imperative management involves using commands to directly modify the state of pods in the cluster. This approach is more hands-on and often used for quick changes or administrative tasks. Create a pod:

kubectl run nginx-example --image=nginx:latest --port=80

Delete a pod:

kubectl delete pod nginx-example

Declarative Management

Declarative management involves defining the desired state of your pods using configuration files (typically YAML or JSON). Kubernetes ensures that the actual state matches the desired state specified in these files. Example YAML:

pod.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx-example
  name: nginx-example
spec:
  containers:
  - image: nginx:latest
    name: nginx-example
    ports:
    - containerPort: 80

Pod Yaml explanation

  • kind: Specifies the type of Kubernetes object being created. For a Pod, the kind is always Pod
  • apiVersion: Defines the API version to use for the object. For a Pod, it's typically v1
pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
    run: nginx-example
name: nginx-example
spec:
containers:
- image: nginx:latest
    name: nginx-example
    ports:
    - containerPort: 80

Let's deploy a pod

kubectl apply -f /path/to/yamls/pod.yaml

If it's working we can easily delete it:

kubectl delete pod nginx-example