Kubernetes Set Command

KubernetesKubernetesBeginner
Practice Now

Introduction

The set command in Kubernetes is a versatile tool that allows you to modify the configuration of existing Kubernetes resources. This command enables adjustments such as updating container images and configuring resource requests and limits, all without directly editing YAML files. It simplifies resource management and enhances operational efficiency.

By the end of this lab, you will understand how to:

  • Start a Kubernetes cluster.
  • Update the container image of a deployment.
  • Configure resource requests and limits for a deployment.
  • Modify labels and annotations on a deployment using relevant commands.

This lab is designed for beginners and assumes you are working on an Ubuntu Linux system with Kubernetes tools installed. No prior experience with Kubernetes is required.

Setup

minikube start

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ClusterInformationGroup(["`Cluster Information`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/set("`Set`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/BasicCommandsGroup -.-> kubernetes/annotate("`Annotate`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") kubernetes/ClusterInformationGroup -.-> kubernetes/cluster_info("`Cluster Info`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/create -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/get -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/set -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/run -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/annotate -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/label -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/cluster_info -.-> lab-8424{{"`Kubernetes Set Command`"}} kubernetes/initialization -.-> lab-8424{{"`Kubernetes Set Command`"}} end

Start a Kubernetes Cluster

Before interacting with Kubernetes resources, ensure that a Kubernetes cluster is running. For this lab, we will use Minikube to set up a single-node Kubernetes cluster.

  1. Open a terminal and start Minikube:

    minikube start

    This initializes a local Kubernetes cluster. Minikube automatically assigns appropriate resources, but you can customize them using flags like --cpus and --memory if necessary.

  2. Verify that Minikube is running:

    kubectl cluster-info

    Ensure the output confirms the cluster is operational.

Create a Deployment

Once the cluster is running, create a simple Kubernetes deployment named hello-world using the nginx image.

  1. Run the following command to create the deployment:

    kubectl create deployment hello-world --image=nginx

    This command creates a deployment named hello-world with one replica of the nginx container.

  2. Verify that the deployment was successfully created:

    kubectl get deployments

    Check the output to ensure hello-world appears in the list of deployments.

Explore the kubectl set Command

The kubectl set command provides multiple subcommands to configure and modify application resources. It helps manage specific aspects like environment variables, container images, and resource settings.

  1. Run the following command to view the available kubectl set subcommands:

    kubectl set -h

    You will see the following output:

    Configure application resources.
    
    These commands help you make changes to existing application resources.
    
    Available Commands:
      env              Update environment variables on a pod template
      image            Update the image of a pod template
      resources        Update resource requests/limits on objects with pod templates
      selector         Set the selector on a resource
      serviceaccount   Update the service account of a resource
      subject          Update the user, group, or service account in a role binding or cluster role binding
    
    Usage:
      kubectl set SUBCOMMAND [options]
    
    Use "kubectl --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).

    Review the available subcommands and their descriptions to understand how kubectl set can be used.

  2. Use kubectl set --help to explore additional details about each subcommand as needed.

Update the Container Image

Next, update the container image in the hello-world deployment to a specific version.

  1. Use the kubectl set command to update the container image to nginx:1.19.10:

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

    This command updates the nginx container in the hello-world deployment.

  2. Verify the image update by querying the container image:

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

    Ensure the output shows nginx:1.19.10.

Configure Resource Requests and Limits

Resource management is essential for Kubernetes deployments. Set resource requests and limits for the hello-world deployment.

  1. Configure CPU and memory requests and limits:

    kubectl set resources deployment/hello-world --limits=cpu=1,memory=512Mi --requests=cpu=500m,memory=256Mi

    This command sets resource requests to 500m CPU and 256Mi memory and limits to 1 CPU and 512Mi memory.

  2. Verify the resource settings by describing the deployment:

    kubectl describe deployment hello-world

    Check the Limits and Requests sections in the output to confirm the configuration.

Modify Labels on the Deployment

Labels help categorize and organize Kubernetes resources. Use the kubectl label command to add or modify labels on the deployment.

  1. Add a label environment=development to the hello-world deployment:

    kubectl label deployment hello-world environment=development

    This command adds a new label to the deployment.

  2. Verify that the label has been applied:

    kubectl get deployment hello-world --show-labels

    Check the LABELS column for the environment=development label.

Update Annotations on the Deployment

Annotations provide metadata to Kubernetes resources. Use the kubectl annotate command to add or update annotations on the deployment.

  1. Add an annotation owner=team-alpha to the hello-world deployment:

    kubectl annotate deployment hello-world owner=team-alpha

    This command adds an annotation to the deployment.

  2. Verify that the annotation has been applied:

    kubectl describe deployment hello-world

    Check the Annotations section for owner=team-alpha.

Summary

In this lab, you learned how to use the Kubernetes set command to manage deployments effectively. You:

  • Started a Kubernetes cluster.
  • Created a deployment.
  • Updated the container image.
  • Configured resource requests and limits.
  • Modified labels and annotations using the appropriate commands.

These skills are essential for managing Kubernetes applications efficiently.

Other Kubernetes Tutorials you may like