Learnitweb

Kubernetes Deployment YAML Manifest

1. What is a Kubernetes Deployment?

In Kubernetes, a Deployment is a higher-level abstraction used to manage the lifecycle of Pods. While you can create Pods directly, it’s not practical in real-world scenarios where:

  • You need to ensure availability and fault-tolerance
  • You need rolling updates to newer versions
  • You want Kubernetes to automatically heal or recreate failed Pods

A Deployment automates the process of creating, scaling, updating, and rolling back Pods via ReplicaSets.


2. Why Use a Deployment Instead of a Pod?

Problems with managing Pods manually:

  • If a Pod crashes, it’s not recreated automatically.
  • Scaling the application to multiple Pods is manual.
  • Updates or rollbacks are not versioned or controlled.
  • No guarantee of high availability across nodes.

Benefits of using Deployments:

  • Ensures desired number of identical Pods are always running.
  • Supports automated rolling updates and rollbacks.
  • Automatically replaces Pods in case of failure or node crash.
  • Deployments manage ReplicaSets, which in turn manage Pods.

3. Anatomy of a Deployment YAML Manifest

Here is an example:

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

Explanation of Sections:

FieldDescription
apiVersionAPI version to use (apps/v1 is required for Deployment)
kindResource type: Deployment
metadataMetadata like name, labels, and optionally namespace
spec.replicasDesired number of Pod replicas
strategyStrategy for updating Pods (default is RollingUpdate)
selectorLabels used by Deployment to find and manage Pods
templatePod template used to create new Pods
template.specSame structure as a regular Pod manifest (containers, volumes, etc.)

4. Deployment Strategies

A. RollingUpdate (Default)

This strategy updates Pods incrementally — new Pods are created while old ones are removed.

Fields:

  • maxSurge: Max number of extra Pods allowed during update.
  • maxUnavailable: Max number of Pods allowed to be unavailable.

Both can be specified as:

  • Absolute number (e.g., 1)
  • Percentage (e.g., 25%)

Use Case: When your app can tolerate old and new versions running together.

strategy:
  rollingUpdate:
    maxSurge: 1
    maxUnavailable: 1

B. Recreate

All old Pods are deleted before new ones are created.

Use Case: When your app doesn’t support parallel versions, or must run in a clean state.

strategy:
  type: Recreate

5. How Deployments Work Internally

  • Deployment → creates a ReplicaSet
  • ReplicaSet → creates and manages Pods

ReplicaSet ensures:

  • The desired number of Pods are always running
  • If a Pod crashes, a new one is created automatically

If a node fails, the Deployment/ReplicaSet reschedules Pods to another healthy node.


6. Managing Deployments via kubectl

Create a Deployment

kubectl apply -f nginx-deployment.yaml

View All Deployments

kubectl get deployments

Columns Explained:

  • NAME: Deployment name
  • READY: Ready Pods vs desired Pods
  • UP-TO-DATE: Number of Pods updated to the latest version
  • AVAILABLE: Number of Pods available to serve traffic
  • AGE: Time since deployment was created

7. Inspecting ReplicaSets and Pods

Get ReplicaSets Created by Deployment

kubectl get rs

Example Output:

NAME                           DESIRED   CURRENT   READY   AGE
nginx-deployment-6b474476c4    3         3         3       52m

Get Pods Managed by Deployment

kubectl get pods

Example Output:

NAME                                     READY   STATUS    RESTARTS   AGE
nginx-deployment-6b474476c4-b6j4j        1/1     Running   0          3h31m

Filter Pods by Label

kubectl get pods -l app=nginx

8. Describe and Debug Deployment

View Detailed Info About a Deployment

kubectl describe deployment nginx-deployment

View Deployment Rollout History

kubectl rollout history deployment nginx-deployment

Undo Last Deployment

kubectl rollout undo deployment nginx-deployment

Undo to a Specific Revision

kubectl rollout undo deployment nginx-deployment --to-revision=2

9. Delete a Deployment

Command:

kubectl delete deployment nginx-deployment

This will also:

  • Remove ReplicaSets created by it
  • Delete all Pods under those ReplicaSets

10. Summary Table

FeatureDescription
DeploymentManages desired number of identical Pods
ReplicaSetEnsures correct number of Pods
Pod TemplateDefines how each Pod looks (image, ports, labels)
RollingUpdateDefault update strategy — no downtime
RecreateStrategy that replaces all Pods at once
RollbackRollback to previous Deployment revision
Self-HealingAutomatically recreates failed Pods

11. Conclusion

A Kubernetes Deployment is the standard way to manage stateless applications in production. It ensures that:

  • Your application is always available
  • Scaling, updating, and recovery are seamless
  • Downtime is minimized using Rolling Updates
  • Full history and rollback support is available