Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 271 of 8024. Kubernetes Architecture and Core ObjectsFree lesson

Defining Applications with Kubernetes Deployments

Learn to create and apply declarative YAML manifests to manage the desired state of containerized applications within a cluster.

In this lesson, you will learn how to transition from managing raw ReplicaSets to using Deployments, the standard way to define and manage the lifecycle of your containerized applications in Kubernetes.

Moving Beyond ReplicaSets

Up until now, you have used ReplicaSet objects to ensure a specific number of Pods are running. However, manually updating a ReplicaSet (for example, to change an image version) is tedious and error-prone. A Deployment acts as a higher-level manager. Instead of managing Pods directly, you define a Deployment, which in turn creates and manages the ReplicaSet for you.

This abstraction enables powerful capabilities like rolling updates, where the Deployment gradually replaces old Pods with new ones, ensuring your application stays available even during updates.

Creating a Deployment Manifest

A Deployment manifest looks very similar to a Pod template, wrapped in a deployment configuration. Here is a simple example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.21

When you apply this file with kubectl apply -f deployment.yaml, the Kubernetes control loop takes over. It verifies the desired state—3 replicas of the nginx:1.21 image—and continuously reconciles the cluster to match that state. If you change the image version in your file to nginx:1.22 and re-apply, the Deployment will automatically orchestrate a rolling update to the new version without you needing to delete the existing Pods manually.

Why use Deployments?

By using Deployments, you shift the burden of orchestration to the controller. You define the "desired state" of your entire application stack, and the Deployment object ensures that the transition between versions—or the recovery from failures—is handled gracefully, consistently, and declaratively. It is the fundamental building block for deploying modern, resilient services in a Kubernetes cluster.

Key Takeaway: Deployments provide a declarative way to manage the entire lifecycle of your application, enabling seamless updates and self-healing through automated reconciliation of your desired state.