What is a Kubernetes Deployment?

A Deployment in Kubernetes is a controller that manages the lifecycle of your application. It helps you define and maintain the desired state of your Pods and ReplicaSets - ensuring your application is always running as expected.


How Kubernetes Deployment Works?

When you define a Deployment, Kubernetes automatically:


  • Creates a ReplicaSet based on your Deployment definition.
  • The ReplicaSet then creates the desired number of Pods.
  • If any Pod fails or becomes unresponsive, Kubernetes replaces it automatically to maintain the desired count.
  • When you update your Deployment (for example, change an image version), Kubernetes performs a rolling update - updating Pods one by one to ensure zero downtime.

This automation ensures high availability, self-healing, and scalability - the three pillars of Kubernetes orchestration.


YAML Syntax for a Deployment

Deployments in Kubernetes are defined using YAML (Yet Another Markup Language) files. Below is the basic syntax to create a NGINX Deployment with 3 replicas.

apiVersion: apps/v1     #Defines the API version used
kind: Deployment     #Specifies the Kubernetes object type
metadata:     #Contains metadata such as the Deployment's name and labels.
 name: nginx-deployment
 labels:
   app: nginx
spec:     #Describes the desired state of the Deployment.
 replicas: 3     #Number of Pod instances you want to run.
 selector:     #Defines how the Deployment finds Pods to manage
   matchLabels:
    app: nginx
  template:     #Blueprint for the Pods that the Deployment creates.
    metadata:
     labels:
      app: nginx
    spec:
      containers:     #Defines container specifications - name, image, and exposed ports.
      - name: nginx
      image: nginx:1.7.9
      ports:
       - containerPort: 80


Creating a Deployment in Kubernetes

Once your YAML file (for example, nginx.yaml) is ready, you can easily deploy it using the kubectl command-line tool.


Step 1: Create the Deployment

kubectl create -f nginx.yaml

This command tells Kubernetes to read the configuration file and create the Deployment accordingly.


Step 2: Verify the Pods

kubectl get po

This will list all running Pods in your cluster.

You should see 3 pods corresponding to the 3 replicas defined in your YAML.


Step 3: Check Deployment Status

kubectl get deployments

This shows the name, desired replicas, current replicas, and available replicas - confirming your deployment's status.


Conclusion

A Kubernetes Deployment is at the heart of maintaining reliable, scalable, and resilient applications in any containerized environment. With a simple YAML definition and powerful kubectl commands, you can deploy, monitor, and manage complex workloads effortlessly.