Kubernetes Set Command

KubernetesKubernetesBeginner
Practice Now

Introduction

In Kubernetes, the set command is used to modify the configuration of existing Kubernetes resources. The set command can be used to modify various aspects of a Kubernetes resource, such as labels, annotations, and the number of replicas for a deployment.

In this lab, we will go through the steps required to use the Kubernetes set command to modify the configuration of a Kubernetes resource.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/set("`Set`") subgraph Lab Skills kubernetes/create -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/get -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/set -.-> lab-8424{{"`Kubernetes Set Command`"}} end

Modify Image of a Deployment

In this step, we will use the set command to modify the image of an existing deployment.

  1. Create a new deployment named hello-world with the nginx image and one replica:

    kubectl create deployment hello-world --image=nginx --replicas=1
  2. Use the set command to update the nginx container with the image nginx:1.19.10, you would run the following command:

    kubectl set image deployment/hello-world nginx=nginx:1.19.10

    Note that this command will only update the specified container's image. If you have multiple containers in your deployment, you will need to run this command for each container.

  3. Verify that the image was updated to the deployment by running the following command:

    kubectl get deployment hello-world -o jsonpath='{.spec.template.spec.containers[0].image}'

    This command will show the image of the hello-world deployment.

Modify Resources of a Deployment

In this step, we will use the kubectl set command to modify resources (CPU and memory limits/requests) of a Kubernetes Deployment.

  1. Use the set command to set the CPU and memory limits to 1 CPU and 512Mi respectively, and the CPU and memory requests to 500m CPU and 256Mi respectively, run the following command:

    kubectl set resources deployment/hello-world --limits=cpu=1,memory=512Mi --requests=cpu=500m,memory=256Mi
  2. Verify that the limits were changed to 1 CPU and 512Mi and the requests were changed to 500m CPU and 256Mi respectively.

    kubectl describe deployment hello-world

Summary

In this lab, we have gone through the steps required to use the Kubernetes set command to modify the configuration of a Kubernetes resource. We modified the labels, replicas, and annotations of an existing deployment using the set command.

Other Kubernetes Tutorials you may like