# kubectl label 命令详解

更新(增加、修改或删除)资源上的 label(标签)。

  • label 必须以字母或数字开头,可以使用字母、数字、连字符、点和下划线,最长 63 个字符。
  • 如果--overwrite 为 true,则可以覆盖已有的 label,否则尝试覆盖 label 将会报错。
  • 如果指定了--resource-version,则更新将使用此资源版本,否则将使用现有的资源版本。

# 语法

$ label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]

# 示例

给名为 foo 的 Pod 添加 label unhealthy=true。

kubectl label pods foo unhealthy=true

给名为 foo 的 Pod 修改 label 为 'status' / value 'unhealthy',且覆盖现有的 value。

kubectl label --overwrite pods foo status=unhealthy

给 namespace 中的所有 pod 添加 label

kubectl label pods --all status=unhealthy

仅当 resource-version=1 时才更新 名为 foo 的 Pod 上的 label。

kubectl label pods foo status=unhealthy --resource-version=1

删除名为“bar”的 label。(使用“ - ”减号相连)

kubectl label pods foo bar-

# Flags

Name Shorthand Default Usage
all false select all resources in the namespace of the specified resource types
allow-missing-template-keys true If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-run false If true, only print the object that would be sent, without sending it.
filename f [] Filename, directory, or URL to files identifying the resource to update the labels
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
local false If true, label will NOT contact api-server but run locally.
no-headers false When using the default or custom-column output format, don't print headers (default print headers).
output o Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
output-version DEPRECATED: To use a specific API version, fully-qualify the resource, version, and group (for example: 'jobs.v1.batch/myjob').
overwrite false If true, allow labels to be overwritten, otherwise reject label updates that overwrite existing labels.
record false Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursive R false Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.
resource-version If non-empty, the labels update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.
selector l Selector (label query) to filter on, supports '=', '==', and '!='.
show-all a false When printing, show all resources (default hide terminated pods.)
show-labels false When printing, show all labels as the last column (default hide labels column)
sort-by If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
Last Updated: 4/15/2023, 8:33:17 PM