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/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") 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`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} kubernetes/create -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} 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`"}} kubernetes/initialization -.-> lab-9675{{"`Scaling and Managing Pods with Deployments`"}} end

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

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