1. Overview

Kubernetes is one of the most popular choices for streamlining container orchestration for microservices and distributed systems. At its core, Kubernetes manages various entities, each serving a different purpose.

In this tutorial, we’ll explore two essential entities foundational to understanding Kubernetes: Pods and Deployments. We’ll delve into their roles, differences, and functionalities within the ecosystem.

2. Pods

A Pod represents the smallest unit of work in Kubernetes, providing the specifications for running one or more containers. Frequently, a pod consists of a single container.

However, running multiple containers inside a pod is possible, and containers within a pod can share storage and network resources. When running more than the main service container in a pod, best practice recommends running helper containers, which handle cross-cutting concerns like logging, monitoring, or authentication. These secondary containers are referred to as sidecar containers.

A pod can be terminated for various reasons, such as exceeding its resource limits or the failure of its host node. In these situations, Kubernetes kills the pod. Notably, the system will not automatically restart a standalone pod that is terminated.

Let’s look at a basic pod definition for running a single container of nginx:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14.2

This creates a pod uniquely identified as nginx-pod within the cluster. Secondly, the nginx:1.14.2 image is specified to run in the nginx-container under the spec section.

3. Deployments

Now, we’ll explore Deployments. A Deployment is a Pod declaration and a ReplicaSet combined. A ReplicaSet is another Kubernetes object that maintains a specific number of pod instances to run at any given time.

A Deployment serves as a higher-order object designed for managing and updating instances of a Pod. While encapsulating features offered by the ReplicaSet entity, such as ensuring a specified number of pod replicas, Deployments bring capabilities for rolling updates without downtime or rollback in case something goes wrong.

Unlike a pod, Deployments represent the desired state of the system. Kubernetes is constantly comparing the requested state of a Deployment versus the actual state in order to make the actual state match the requested state. This behavior is responsible for re-running failed pods in a deployment or changing the number of running pods as the number of requested replicas is updated.

Let’s look at a Deployment configuration for running two replicas of nginx:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14.2
        ports:
        - containerPort: 80

This sets up a Deployment named nginx-deployment, designed to ensure two replicas are always running. This works by matching the app label in the template to the matchLabels in the selector block.

4. Contrasting Pods and Deployments

Now, let’s take a look at the key differences between Pods and Deployments:

  • Pods are the most basic deployment unit in Kubernetes, while Deployments are more complex, representing high-level abstractions for managing sets of pods.
  • The ephemeral nature of Pods makes managing their lifecycle individually a significant effort. Deployments provide automated lifecycle management of the pods they contain, reducing manual workload.
  • While Pods are subject to termination upon node failure or resource overuse, Deployments continuously monitor and replace failed pods to maintain the desired state.
  • Pods are defined independently; Deployments, however, wrap pod definitions, providing an additional management layer.
  • Pods require manual updates and intervention for deploying and scaling. Deployments enable automated updates, rollbacks, and scaling, offering enhanced cluster management.

5. Choosing Pods or Deployments

In a real-world scenario, when running an application on a Kubernetes cluster, we typically work with higher abstractions than Pods. Consequently, we often choose Deployments to automate crucial aspects such as pod recreation in case of failure, scaling, and updates, ensuring robust and efficient handling of the application workloads.

In addition, we are indirectly still leveraging Pods when using Deployments since our application runs as containers inside Pods. Nonetheless, we’re not managing individual PodsDeployments delegate this responsibility to the Kubernetes control plane, which operates based on our defined specifications.

6. Conclusion

In this article, we’ve learned about two fundamental Kubernetes concepts: Pods and Deployments. We’ve highlighted the differences and saw that Pods represent the basic building blocks of execution. Deployments offer a higher-level approach to managing multiple Pod instances, along with other lifecycle aspects.

In general, we should prefer wrapping our Pod declarations in Deployments.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.