Wednesday, September 2, 2026

Creating a Deployment in Kubernetes using YAML

 

You'll learn how real applications are deployed, configured, scaled, updated, and healed automatically.


What is a Deployment?

A Deployment is a Kubernetes resource used to manage and deploy applications. It ensures that the desired number of Pods are running and provides features like scaling, rolling updates, and self-healing.




Deployment YAML Example


Create a file named deployment.yaml:


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


Understanding the YAML


apiVersion - Specifies the API version used by the Deployment.

apiVersion: apps/v1


kind - Defines the resource type. example (Pod, Deployment, Services)

kind: Deployment


metadata - Gives the deployment a name.

metadata:

  name: nginx-deployment


replicas - Creates desired no of identical Pods.

replicas: 3


selector - Used to identify the Pods managed by this Deployment. (Selector identifies which Pods are managed by the Deployment.)

selector:

  matchLabels:

    app: nginx


template - Represents the Pod template. (Create Pods with label app: nginx and run the nginx container.)

template:


container image - Specifies the container image to use.

image: nginx:latest



Create Deployment

kubectl apply -f deployment.yaml



Verify Deployment

kubectl get deployments

kubectl get rs

kubectl get pods



Scale Deployment (Increase replicas from 3 to 5)

kubectl scale deployment nginx-deployment --replicas=5

kubectl get pods




Update Deployment

Update image version - kubectl set image deployment/nginx-deployment nginx=nginx:1.26

Check rollout status - kubectl rollout status deployment/nginx-deployment




Rollback Deployment

View rollout history - kubectl rollout history deployment nginx-deployment

Rollback - kubectl rollout undo deployment nginx-deployment




Self-Healing Example (Delete a Pod)

kubectl delete pod <pod-name>


Kubernetes automatically creates a new Pod to maintain the desired replica count.



No comments:

Post a Comment

testing