Linux command
kubectl-label 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Add label to resource
kubectl label [pod/name] [key]=[value]
Remove label
kubectl label [pod/name] [key]-
Overwrite existing label
kubectl label --overwrite [pod/name] [key]=[newvalue]
Label all pods with selector
kubectl label pods -l [app=myapp] [env]=[production]
说明
kubectl label adds, updates, or removes labels on Kubernetes resources such as pods, nodes, services, and deployments. Labels are key-value metadata pairs that serve as the primary mechanism for organizing, grouping, and selecting resources throughout the Kubernetes ecosystem. They are used extensively by selectors in services, deployments, and network policies to determine which resources to target. To add a label, specify a key=value pair; to remove one, append a minus sign to the key name (e.g., `key-`). By default, overwriting an existing label requires the --overwrite flag to prevent accidental changes. The command can operate on individual resources, on all resources of a given type with --all, or on a filtered set matched by a label selector.
参数
- --overwrite
- Allow overwriting existing labels.
- --all
- Select all resources of type.
- -l, --selector _selector_
- Label selector for filtering.
- -n, --namespace _name_
- Kubernetes namespace.
FAQ
What is the kubectl-label command used for?
kubectl label adds, updates, or removes labels on Kubernetes resources such as pods, nodes, services, and deployments. Labels are key-value metadata pairs that serve as the primary mechanism for organizing, grouping, and selecting resources throughout the Kubernetes ecosystem. They are used extensively by selectors in services, deployments, and network policies to determine which resources to target. To add a label, specify a key=value pair; to remove one, append a minus sign to the key name (e.g., `key-`). By default, overwriting an existing label requires the --overwrite flag to prevent accidental changes. The command can operate on individual resources, on all resources of a given type with --all, or on a filtered set matched by a label selector.
How do I run a basic kubectl-label example?
Run `kubectl label [pod/name] [key]=[value]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --overwrite do in kubectl-label?
Allow overwriting existing labels.