๐Ÿถ
Kubernetes

Delete Kubernetes Node Labels: Command & API Guide

By Jan on 02/05/2025

Learn how to efficiently delete Kubernetes node labels using both imperative commands and the Kubernetes API in this comprehensive guide.

Delete Kubernetes Node Labels: Command & API Guide

Table of Contents

Introduction

This document provides a quick reference for common Kubernetes kubectl commands related to taints and patching objects. It covers listing taints on nodes, adding a taint to a node, and updating API objects using strategic and JSON merge patches.

Step-by-Step Guide

  1. List taints on Kubernetes nodes:

    kubectl get nodes -o json | jq '.items[].spec.taints' 
  2. Add a taint to a node:

    kubectl taint nodes <node-name> key=value:NoSchedule
  3. Update an API object using a strategic merge patch:

    kubectl patch <resource> <name> -p '{ "spec": { "key": "new-value" } }'
  4. Update an API object using a JSON merge patch:

    kubectl patch <resource> <name> --type merge -p '{ "spec": { "key": "new-value" } }'

Code Example

This document provides code examples for listing taints on Kubernetes nodes, adding a taint to a node, updating an API object using a strategic merge patch, and updating an API object using a JSON merge patch. It includes examples using kubectl, jq, and JSON path for retrieving and displaying taints, adding taints with specific keys, values, and effects, and patching deployments and pods using strategic and JSON merge patches. The examples demonstrate how to update replicas in deployments and add labels to pods.

This document provides code examples for the following Kubernetes operations:

  1. Listing Taints on Kubernetes Nodes

    This example uses kubectl, jq, and a JSON path to retrieve and display taints on all nodes:

    kubectl get nodes -o json | jq '.items[].spec.taints'

    Example Output:

    [
      {
        "key": "node.kubernetes.io/unreachable",
        "effect": "NoSchedule",
        "timeAdded": "2023-10-26T12:00:00Z"
      }
    ]
    null
    ...
  2. Adding a Taint to a Node

    This example adds a taint to a node named "my-node" that prevents scheduling of pods without a matching toleration:

    kubectl taint nodes my-node app=blue:NoSchedule

    This command adds a taint with:

    • Key: app
    • Value: blue
    • Effect: NoSchedule
  3. Updating an API Object Using a Strategic Merge Patch

    This example updates the replicas field of a Deployment named "my-deployment" to 3:

    kubectl patch deployment my-deployment -p '{"spec": {"replicas": 3}}'

    Explanation:

    • kubectl patch: Uses the patch command to update the resource.
    • deployment my-deployment: Specifies the resource type and name.
    • -p '{"spec": {"replicas": 3}}': Provides the patch data in JSON format, targeting the spec.replicas field.
  4. Updating an API Object Using a JSON Merge Patch

    This example updates the labels of a Pod named "my-pod" using a JSON merge patch:

    kubectl patch pod my-pod --type merge -p '{"metadata": {"labels": {"environment": "production"}}}'

    Explanation:

    • --type merge: Specifies the patch type as JSON merge patch.
    • -p '{"metadata": {"labels": {"environment": "production"}}}': Provides the patch data in JSON format, adding the environment label with the value production.

These examples demonstrate basic operations related to Kubernetes taints and patching API objects. Remember to adapt the commands and data to your specific needs and environment.

Additional Notes

General:

  • Taints and Tolerations: Taints and tolerations work together to control pod scheduling. Taints on a node repel pods, while tolerations on a pod allow them to be scheduled onto tainted nodes.
  • Understanding Patch Types:
    • Strategic Merge Patch: This patch type is Kubernetes-specific and understands Kubernetes object schemas. It's generally safer for updates but can be more complex.
    • JSON Merge Patch: This is a standard patch type that simply merges JSON objects. It's simpler to use but can lead to unexpected results if not used carefully.
  • Resource Types: The provided examples use "deployment" and "pod" as resource types. These can be replaced with any valid Kubernetes resource type, such as "service", "ingress", etc.

Listing Taints:

  • Filtering Output: The jq command can be further customized to filter and display specific taint information. For example, to only show taints with the effect "NoSchedule": kubectl get nodes -o json | jq '.items[].spec.taints[] | select(.effect == "NoSchedule")'
  • Troubleshooting: If no taints are displayed, ensure that the nodes actually have taints applied.

Adding Taints:

  • Taint Effects:
    • NoSchedule: Prevents new pods from being scheduled onto the node.
    • PreferNoSchedule: Discourages new pods from being scheduled onto the node, but doesn't prevent it entirely.
    • NoExecute: Evicts existing pods that don't tolerate the taint.
  • Removing Taints: To remove a taint, use the kubectl taint nodes <node-name> key-= command.

Patching Objects:

  • Patch Data: The patch data provided in the -p flag must be valid JSON and should target the specific fields you want to update.
  • Dry Run: Use the --dry-run=client flag with kubectl patch to simulate the patch operation without actually applying the changes. This is useful for testing your patch data.
  • Alternative to kubectl patch: You can also update Kubernetes objects by editing them directly using kubectl edit <resource> <name>. However, patching is generally preferred for automated updates.

These notes provide additional context and considerations for working with taints and patching objects in Kubernetes. Always refer to the official Kubernetes documentation for the most up-to-date information and best practices.

Summary

Command Description
`kubectl get nodes -o json jq '.items[].spec.taints'`
kubectl taint nodes <node-name> key=value:NoSchedule Adds a taint to a specific node.
kubectl patch <resource> <name> -p '{ "spec": { "key": "new-value" } }' Updates a Kubernetes resource using a strategic merge patch.
kubectl patch <resource> <name> --type merge -p '{ "spec": { "key": "new-value" } }' Updates a Kubernetes resource using a JSON merge patch.

Conclusion

This document provided a concise guide to essential Kubernetes commands for managing taints and patching objects. By understanding these commands and concepts, users can effectively control pod scheduling, update deployments, modify pod configurations, and perform other crucial Kubernetes operations. Remember to consult the official Kubernetes documentation for comprehensive information and best practices.

References

Were You Able to Follow the Instructions?

๐Ÿ˜Love it!
๐Ÿ˜ŠYes
๐Ÿ˜Meh-gical
๐Ÿ˜žNo
๐ŸคฎClickbait