TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Kubernetes/Kubernetes Deployments
Kubernetes Tutorial

Kubernetes Deployments

Learn how Kubernetes Deployments manage application Pods, replicas, ReplicaSets, rolling updates, scaling, rollbacks, and application releases.

Level: Beginner to Intermediate
Topic: Kubernetes Workloads

Kubernetes Workload Management

Manage Application Deployments

Deployment

ReplicaSet

Pod Replicas

Rolling Updates

Rollbacks

Introduction

What is a Kubernetes Deployment?

A Kubernetes Deployment is a workload resource used to manage stateless applications running inside a Kubernetes cluster.

Instead of manually creating and managing individual Pods, you define the desired state of your application in a Deployment. Kubernetes then works to maintain that desired state.

Deployments provide features such as replica management, rolling updates, application scaling, rollout history, and rollbacks.

Deployment Fundamentals

How Kubernetes Deployments work

Deployments provide a declarative way to manage applications and maintain the required number of running Pod replicas.

01

Desired State Management

A Deployment defines the desired state of an application, including which container image to run and how many replicas should be available.

Key Topics

  • Desired state
  • Declarative configuration
  • Application management
  • Self-healing
02

Replica Management

Deployments manage ReplicaSets, which maintain the required number of running Pod replicas.

Key Topics

  • Replicas
  • ReplicaSets
  • Pod availability
  • Automatic replacement
03

Rolling Updates

Deployments can gradually replace old application versions with new versions while maintaining application availability.

Key Topics

  • Rolling updates
  • Version releases
  • Availability
  • Controlled updates
04

Rollbacks

If a new deployment version causes problems, Kubernetes can roll back to a previously working revision.

Key Topics

  • Revision history
  • Rollback
  • Deployment history
  • Application recovery

Deployment Architecture

Components of a Deployment

A Deployment uses several Kubernetes resources and configuration components to manage application workloads.

Deployment

Defines the desired application state and manages ReplicaSets.

ReplicaSet

Ensures the configured number of Pod replicas are running.

Pods

Run the application containers created by the ReplicaSet.

Selector

Matches Pods that belong to the Deployment.

Template

Defines the Pod specification used to create new replicas.

Strategy

Defines how Kubernetes updates existing application replicas.

YAML Configuration

Creating your first Deployment

A Deployment is commonly defined using a YAML manifest. The configuration describes the desired number of replicas and the Pod template used to create the application instances.

Kubernetes uses the Pod template to create Pods and a ReplicaSet to maintain the required number of replicas.

Basic Deployment Example

apiVersion: apps/v1
kind: Deployment

metadata:
  name: nginx-deployment

spec:
  replicas: 3

  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx

    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80

Deploying Applications

Apply the Deployment

After creating the YAML configuration, use kubectl to submit the Deployment to the Kubernetes API.

Kubernetes creates the Deployment, ReplicaSet, and the required number of Pods based on the configuration.

Deploy the Application

kubectl apply -f deployment.yaml

kubectl get deployments

kubectl get replicasets

kubectl get pods

Scaling

Scaling application replicas

Kubernetes Deployments make it easy to increase or decrease the number of application instances.

When the replica count is increased, Kubernetes creates additional Pods. When it is reduced, unnecessary Pods are terminated.

Scale a Deployment

kubectl scale deployment nginx-deployment --replicas=5

kubectl get pods

kubectl get deployment nginx-deployment

Rolling Updates

Updating applications gradually

Kubernetes Deployments can update applications gradually using a rolling update strategy.

New Pods are created using the updated configuration while older Pods are gradually removed. This helps maintain application availability during the update process.

You can monitor rollout progress to confirm that the new version has been successfully deployed.

Update a Container Image

kubectl set image deployment/nginx-deployment \
nginx=nginx:latest

kubectl rollout status deployment/nginx-deployment

kubectl get pods

Rollbacks

Recover from a failed deployment

Kubernetes stores Deployment revision history, allowing you to inspect previous versions of an application rollout.

If a new version causes problems, you can roll back the Deployment to a previously working revision.

Check History and Roll Back

kubectl rollout history deployment/nginx-deployment

kubectl rollout undo deployment/nginx-deployment

kubectl rollout status deployment/nginx-deployment

Deployment Flow

How a Deployment manages Pods

01

Deployment

Defines the desired application configuration and replica count.

02

ReplicaSet

Maintains the desired number of application Pod replicas.

03

Pods

Run the application containers across Kubernetes nodes.

kubectl Commands

Useful Deployment commands

Use these commands to create, inspect, scale, update, and roll back Kubernetes Deployments.

kubectl get deployments

List Deployments in the current namespace.

kubectl get replicasets

List ReplicaSets managed by Deployments.

kubectl get pods

List Pods created for the application.

kubectl describe deployment <name>

Display detailed information about a Deployment.

kubectl apply -f deployment.yaml

Create or update a Deployment using YAML.

kubectl scale deployment <name> --replicas=5

Change the number of desired application replicas.

kubectl rollout status deployment <name>

Check the progress of a Deployment rollout.

kubectl rollout undo deployment <name>

Roll back to a previous Deployment revision.

Learning Roadmap

How to learn Kubernetes Deployments

Follow these steps to understand how Deployments manage Kubernetes applications.

Step 1

Understand Deployments

Learn why Deployments are used to manage stateless Kubernetes applications instead of manually creating Pods.

Step 2

Create a Deployment

Define an application using a Deployment YAML manifest and apply it to your Kubernetes cluster.

Step 3

Understand ReplicaSets

Learn how Deployments use ReplicaSets to maintain the required number of running Pods.

Step 4

Scale Applications

Increase or decrease the number of replicas based on application requirements.

Step 5

Perform Rolling Updates

Update container images and application versions using Kubernetes rollout strategies.

Step 6

Practice Rollbacks

Learn how to inspect rollout history and return to a previous working application version.

Best Practices

Kubernetes Deployment best practices

Define Resource Requests

Specify appropriate CPU and memory requests to help Kubernetes schedule Pods effectively.

Use Resource Limits

Configure reasonable limits where appropriate to prevent workloads from consuming excessive resources.

Use Readiness Probes

Ensure Pods receive traffic only after the application is ready to handle requests.

Use Liveness Probes

Configure health checks that help Kubernetes identify unhealthy application containers.

Monitor Rollouts

Check rollout status when deploying application updates and investigate failures before proceeding.

Keep Revision History

Maintain useful rollout history so you can investigate and roll back problematic application releases.

Next Step

Start deploying applications with Kubernetes

Practice creating Deployments, scaling replicas, performing rolling updates, monitoring rollouts, and rolling back application versions.