Spring Boot HandBook

    Understanding Pods, Namespace, Deployment and ReplicaSet in Kubernetes

    Introduction#

    In the previous blog we have successfully setup our kubernetes cluster with the help of minikube, and now it’s time to learn some kubernetes objects, so that we can manage our deployment easily.

    We are going to learn about :

    1. Pods – The smallest deployable unit in Kubernetes
    2. Namespaces – How Kubernetes organizes resources
    3. Deployments – The easiest way to manage applications
    4. ReplicaSets – Ensuring your app runs at the right scale

    Let’s dive in and explore how these components work together to power modern applications!

    1. What is a Pod in Kubernetes?#

    A Pod is the smallest and most basic unit in Kubernetes. Think of it as a wrapper for one or more containers that work together as a single unit. It represents a single instance of a running process in your cluster.

    • A Pod can run one or multiple containers, and all of them share the same storage and network resources.
    • Since all containers inside a Pod are deployed on the same node, they can communicate easily using localhost.
    • Pods are temporary they can be created, replaced, or removed automatically, depending on how your cluster is configured.

    Key Features of Pods:#

    • Each Pod runs one or more containers, grouped together logically.
    • Containers inside a Pod share networking (same IP) and storage.
    • Pods are ephemeral, meaning they can be replaced or recreated dynamically.

    In short, Pods are the foundation of how Kubernetes manages and runs containerized applications.

    Pods in Kubernetes

    Example of Pod Definition (YAML)#

    apiVersion: v1 kind: Pod metadata: name: my-app spec: containers: - name: nginx-container image: nginx ports: - containerPort: 80

    This Pod definition in Kubernetes creates a single instance named "my-app", running a container with the nginx web server. The spec section defines the container's name (nginx-container), the Docker image to be used (nginx), and the port 80, which allows the container to handle web traffic. Unlike a Deployment, this Pod is not managed automatically if it fails, Kubernetes will not restart it unless configured separately. This is useful for basic testing or running single-instance applications.

    How to create a Pod?#

    kubectl apply -f pod.yaml

    Output:

    pod/my-app created

    Check running Pods#

    kubectl get pods

    Output:

    NAME READY STATUS RESTARTS AGE my-app 1/1 Running 0 10s

    Basic kubectl Commands for Managing Pods#

    Create a Pod#

    kubectl run my-pod --image=nginx

    List Pods#

    • Default Namespace:
    kubectl get pods
    • Specific Namespace:
    kubectl get pods -n <namespace>

    Get Pod Details#

    • Describe a Specific Pod:
    kubectl describe pod <pod-name>
    • Retrieve Logs from a Pod:
    kubectl logs <pod-name>

    Interact with a Pod#

    • Open a Shell Session Inside a Pod:(Works if the container supports it.)
    kubectl exec -it <pod-name> -- /bin/bash

    Delete a Pod#

    kubectl delete pod <pod-name>

    Create a Pod from a YAML File#

    kubectl apply -f pod.yaml

    2. What is a Namespace in Kubernetes?#

    A Namespace is a way to logically separate resources in a Kubernetes cluster. Think of it as separate folders for different teams, applications, or environments (e.g., dev, staging, production).

    Key Features of Namespaces:#

    • Allow multiple teams to work in the same cluster without conflicts.
    • Provide resource isolation and better access control.
    • Help organize workloads efficiently.
    Kubernetes Namespaces

    Built-in Kubernetes Namespaces:#

    • default – Used when no namespace is specified.
    • kube-system – Contains internal Kubernetes components.
    • kube-public – Publicly accessible resources.
    • kube-node-lease – Holds node heartbeat information.

    How to create a Namespace?#

    apiVersion: v1 kind: Namespace metadata: name: my-namespace
    kubectl apply -f namespace.yaml

    View all namespaces:

    kubectl get namespaces

    Output:

    NAME STATUS AGE default Active 10m kube-system Active 10m my-namespace Active 5s

    Run a Pod in a specific namespace:

    kubectl run nginx --image=nginx --namespace=my-namespace

    Managing Namespaces in Kubernetes#

    List Namespaces#

    • Get all namespaces:
    kubectl get namespaces

    Create and Delete Namespaces#

    • Create a namespace:
    kubectl create namespace <namespace-name>
    • Delete a namespace:
    kubectl delete namespace <namespace-name>

    Work with Pods in a Namespace#

    • List Pods in a specific namespace:
    kubectl get pods -n <namespace-name>
    • List Pods across all namespaces:
    kubectl get pods --all-namespaces

    Set the Current Namespace#

    • Change the active namespace for the current context:
    kubectl config set-context --current --namespace=<namespace-name>

    Get Namespace Details#

    • Describe a namespace:
    kubectl describe namespace <namespace-name>
    • Edit a namespace:
    kubectl edit namespace <namespace-name>

    Get All Resources Across All Namespaces#

    kubectl get all --all-namespaces

    3. What is ReplicaSet in Kubernets?#

    We have one Pod running your containerized application which is good, but what if you want multiple replica of that pods and also you want to manage the lifecycle of that pods? Here the replicaSet comes into the picture.

    A ReplicaSet in Kubernetes ensures that a specific number of identical Pods are always running in your cluster. It helps maintain high availability by automatically managing the lifecycle of Pods.

    • If a Pod fails or gets terminated, the ReplicaSet will immediately create a new one to replace it.
    • It works using label selectors, which define which Pods belong to the ReplicaSet.
    • By ensuring the correct number of replicas, a ReplicaSet helps maintain fault tolerance and scalability.

    Key Features of ReplicaSets:#

    • Maintains the desired number of running Pods.
    • Uses label selectors to identify which Pods it manages.
    • Ensures high availability of applications.

    In simple terms, a ReplicaSet is like an auto-repair system for your Pods, making sure your application runs smoothly without manual intervention.

    Kubernetes ReplicaSet

    Example of a ReplicaSet (YAML)#

    apiVersion: apps/v1 kind: ReplicaSet metadata: name: my-replicaset spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: nginx-container image: nginx ports: - containerPort: 80

    Here we have specified 3 replicas; this will create 3 nginx containers within 3 Pods. Also, in the selector, we have specified matchLabels as app: my-app, which is the same as the label defined in the template.metadata.labels section. This ensures that the ReplicaSet manages only the Pods with the label app: my-app, maintaining the desired number of replicas.

    Create a ReplicaSet:

    kubectl apply -f replicaset.yaml

    Check running ReplicaSets:

    kubectl get replicasets

    Output:

    NAME DESIRED CURRENT READY AGE my-replicaset 3 3 3 10s

    Delete a ReplicaSet:

    kubectl delete replicaset my-replicaset

    4. What is Deployment in Kubernetes?#

    A Deployment is a higher-level abstraction that provides declarative updates for Pods and ReplicaSets. It simplifies the management of application workloads by handling:

    • Defining the Desired State – Specify details such as container images, the number of replicas, and update strategies.
    • Managing ReplicaSets and Pods – Automates the creation, scaling, and replacement of Pods as needed.
    • Rolling Updates & Rollbacks – Ensures seamless updates without downtime and allows easy rollback to a previous version if needed.

    This makes Deployments an essential tool for maintaining application availability and scalability.

    Kubernetes Deployment

    Example of a Deployment (YAML)#

    apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 3 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: nginx ports: - containerPort: 80

    This Deployment in Kubernetes ensures that three instances (replicas) of a Pod are always running, each containing a container running the nginx web server. The selector ensures that only Pods labeled app: my-app are managed by this Deployment, and the template defines how each Pod should be created, specifying the container name, image (nginx), and exposed port 80.

    To ensure smooth updates without downtime, a rolling update strategy is defined. The maxSurge: 1 setting allows Kubernetes to temporarily create one extra Pod during an update, ensuring that the desired number of running Pods is maintained. The maxUnavailable: 1 setting ensures that at most one Pod is unavailable at any given time during the update process. This approach gradually replaces old Pods with new ones, ensuring continuous application availability. Additionally, if something goes wrong during an update, Kubernetes allows rollbacks to a previous stable version, making deployments safer and more reliable.

    Deploy an application:

    kubectl apply -f deployment.yaml

    Check Deployments:

    kubectl get deployments

    Output:

    NAME READY UP-TO-DATE AVAILABLE AGE my-deployment 3/3 3 3 10s

    Scale Deployment to 5 replicas:

    kubectl scale deployment my-deployment --replicas=5

    Manage Deployments in Kubernetes#

    • Create a Deployment:
    kubectl create deploy <deployment-name> --replicas=3 --image=<image-name>
    • Scale a Deployment:
    kubectl scale deployment <deployment-name> --replicas=5
    • Update a Deployment (Change Container Image):
    kubectl set image deployment/<deployment-name> <container-name>=<new-image>
    • Rollback to Previous Revision:
    kubectl rollout undo deployment/<deployment-name>
    • Check Rollout History:
    kubectl rollout history deployment/<deployment-name>
    • Rollback to a Specific Revision:
    kubectl rollout undo deployment/<deployment-name> --to-revision=<revision-number>

    Conclusion#

    In this blog, we explored four fundamental Kubernetes components:

    Pods – The basic unit that runs containers.

    Namespaces – Helps organize Kubernetes resources efficiently.

    ReplicaSets – Ensures high availability by maintaining the desired number of Pods.

    Deployments – Manage updates, scaling, and fault tolerance.

    Understanding these building blocks is essential for working with Kubernetes. Now that you have a solid grasp of these concepts, you can start deploying applications, managing workloads, and scaling efficiently in a Kubernetes cluster!

    Last updated on Feb 20, 2025