标签
标签(Label)是将资源进行分类的标识符,资源标签其实就是一个键值型(key/values)数据。标签旨在指定对象(如Pod等)辨识性的属性,这些属性仅对用户存在特定的意义,对Kubernetes集群来说并不直接表达核心系统语义。标签可以在对象创建时附加其上,并能够在创建后的任意时间进行添加和修改。一个对象可以拥有多个标签,一个标签也可以附加于多个对象(通常是同一类对象)之上,如图所示。
k get node,ns --show-labels
kd get svc,ep,pod --show-labels
kd describe svc|grep Labels
要在Kubernetes(k8s)集群中添加或删除工作节点的标签,你可以使用kubectl
命令行工具来完成。下面是相应的步骤:
添加集群工作节点标签
-
首先,使用
kubectl get nodes
命令获取当前集群中的工作节点列表。找到要添加标签的工作节点名称。 -
使用
kubectl label nodes <节点名称> <标签键>=<标签值>
命令为目标节点添加标签。将<节点名称>
替换为工作节点的名称,并指定要添加的标签键和标签值。例如,要为名为
node-1
的节点添加标签env=prod
,可以运行以下命令:kubectl label nodes node-1 env=prod
-
确认标签已成功添加,可以运行
kubectl describe node <节点名称>
命令查看节点的详细信息,其中将包含新添加的标签。
删除集群工作节点标签
-
首先,使用
kubectl get nodes
命令获取当前集群中的工作节点列表。找到要删除标签的工作节点名称。 -
使用
kubectl label nodes <节点名称> <标签键>-
命令删除目标节点上的标签。将<节点名称>
替换为工作节点的名称,并指定要删除的标签键。例如,要从名为
node-1
的节点上删除标签env
,可以运行以下命令:kubectl label nodes node-1 env-
-
确认标签已成功删除,可以运行
kubectl describe node <节点名称>
命令查看节点的详细信息,确保标签已被移除。
这样,你就可以在Kubernetes集群中添加或删除工作节点的标签了。请注意,这些操作需要执行者具备适当的权限。
标签选择器
标签选择器(Selector)全称为“Label Selector”,它是一种根据Label来过滤符合条件的资源对象的机制。例如,将附有标签“role: backend”的所有Pod对象挑选出来归为一组就是标签选择器的一种应用,如图所示。用户通常使用标签对资源对象进行分类,而后使用标签选择器挑选出它们,例如将其创建为某Service的端点。
kd describe deployment|grep Selector
注解
Annotation(注解)是另一种附加在对象之上的键值类型的数据,但它拥有更大的数据容量。Annotation常用于将各种非标识型元数据(metadata)附加到对象上,但它不能用于标识和选择对象,通常也不会被Kubernetes直接使用,其主要目的是方便工具或用户的阅读及查找等。
kd describe svc|grep Annotations
kd describe deployment|grep Annotations
更新一个或多个资源上的注释。
所有Kubernetes对象都支持将附加数据作为注释存储在对象中。注释包括键/值对可以大于标签,并包含任意字符串值,如结构化JSON。工具和系统扩展可以使用注释来存储自己的数据。除非设置了–overwrite,否则尝试设置已存在的注释将失败。If–资源版本为如果指定的资源版本与服务器上的当前资源版本不匹配,则该命令将失败。
使用“kubectl api resources”获取支持资源的完整列表。
ka annotate --help
Update the annotations on one or more resources.
All Kubernetes objects support the ability to store additional data with the object as annotations. Annotations are
key/value pairs that can be larger than labels and include arbitrary string values such as structured JSON. Tools and
system extensions may use annotations to store their own data.
Attempting to set an annotation that already exists will fail unless --overwrite is set. If --resource-version is
specified and does not match the current resource version on the server the command will fail.
Use "kubectl api-resources" for a complete list of supported resources.
Examples:
# Update pod 'foo' with the annotation 'description' and the value 'my frontend'
# If the same annotation is set multiple times, only the last value will be applied
kubectl annotate pods foo description='my frontend'
# Update a pod identified by type and name in "pod.json"
kubectl annotate -f pod.json description='my frontend'
# Update pod 'foo' with the annotation 'description' and the value 'my frontend running nginx', overwriting any
existing value
kubectl annotate --overwrite pods foo description='my frontend running nginx'
# Update all pods in the namespace
kubectl annotate pods --all description='my frontend running nginx'
# Update pod 'foo' only if the resource is unchanged from version 1
kubectl annotate pods foo description='my frontend running nginx' --resource-version=1
# Update pod 'foo' by removing an annotation named 'description' if it exists
# Does not require the --overwrite flag
kubectl annotate pods foo description-
Options:
--all=false:
Select all resources, in the namespace of the specified resource types.
-A, --all-namespaces=false:
If true, check the specified action in all namespaces.
--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='none':
Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without
sending it. If server strategy, submit server-side request without persisting the resource.
--field-manager='kubectl-annotate':
Name of the manager used to track field ownership.
--field-selector='':
Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector
key1=value1,key2=value2). The server only supports a limited number of field queries per type.
-f, --filename=[]:
Filename, directory, or URL to files identifying the resource to update the annotation
-k, --kustomize='':
Process the kustomization directory. This flag can't be used together with -f or -R.
--list=false:
If true, display the annotations for a given resource.
--local=false:
If true, annotation will NOT contact api-server but run locally.
-o, --output='':
Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath,
jsonpath-as-json, jsonpath-file).
--overwrite=false:
If true, allow annotations to be overwritten, otherwise reject annotation updates that overwrite existing
annotations.
-R, --recursive=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 annotation update will only succeed if this is the current resource-version for the object.
Only valid when specifying a single resource.
-l, --selector='':
Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching
objects must satisfy all of the specified label constraints.
--show-managed-fields=false:
If true, keep the managedFields when printing objects in JSON or YAML format.
--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].
Usage:
kubectl annotate [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]
[options]
Use "kubectl options" for a list of global command-line options (applies to all commands).