Scaling and Managing Pods with Deployments

KubernetesKubernetesBeginner
Practice Now

Introduction

In this lab, you will learn how to scale and manage Pods with Deployments in Kubernetes. Deployments are a higher-level abstraction that allow you to declaratively manage and scale replica sets of Pods. By using Deployments, you can easily update your application to a new version, roll back to a previous version, and scale your application up or down to meet changing demand.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/delete("`Delete`") kubernetes/BasicCommandsGroup -.-> kubernetes/edit("`Edit`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/rollout("`Rollout`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/scale("`Scale`") subgraph Lab Skills kubernetes/get -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} kubernetes/delete -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} kubernetes/edit -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} kubernetes/apply -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} kubernetes/rollout -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} kubernetes/scale -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} end

Create a Deployment

  1. Create a file called my-deployment.yaml in /home/labex/project/ with the following content::
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: nginx:latest
          ports:
            - containerPort: 80

This YAML file defines a Deployment with 3 replicas, running an Nginx container. The selector field selects the Pods controlled by the Deployment based on the app label.

  1. Deploy the my-deployment Deployment:
kubectl apply -f my-deployment.yaml

This will create the my-deployment Deployment and its associated ReplicaSets and Pods.

  1. Verify that the Deployment has been created:
kubectl get deployments

This will show you the Deployments in your cluster, including the my-deployment Deployment.

Scale the Deployment

  1. Scale up the my-deployment Deployment to 5 replicas:
kubectl scale deployment my-deployment --replicas=5

This will increase the number of replicas in the my-deployment Deployment to 5.

  1. Verify that the Deployment has been scaled:
kubectl get deployments

This will show you the Deployments in your cluster, including the my-deployment Deployment with 5 replicas.

Update the Deployment

  1. Edit the my-deployment Deployment to use the nginx:1.19 image:
kubectl edit deployment my-deployment

This will open the Deployment in your default text editor. Change the image field to nginx:1.19 and save the file.

  1. Verify that the Deployment has been updated:
kubectl rollout status deployment/my-deployment

This will show you the status of the latest rollout of the my-deployment Deployment.

Roll Back the Deployment

  1. Roll back the my-deployment Deployment to the previous version:
kubectl rollout undo deployment/my-deployment

This will roll back the my-deployment Deployment to the previous version.

  1. Verify that the Deployment has been rolled back:
kubectl rollout status deployment/my-deployment

This will show you the status of the latest rollout of the my-deployment Deployment.

Clean Up

  1. Delete the my-deployment Deployment:
kubectl delete deployment my-deployment

This will delete the my-deployment Deployment and its associated ReplicaSets and Pods.

Summary

In this lab, you learned how to create and manage Deployments in Kubernetes. You created a Deployment with 3 replicas, scaled the Deployment to 5 replicas, updated the Deployment to use a different image, rolled back the Deployment to the previous version,

Other Kubernetes Tutorials you may like