Kubernetes——Pod资源使用资源注解(annotation)

Pod资源使用资源注解(annotation)

一、注解概述

  注解(annotation)是 "键值" 类型的数据,不过它不能用于标签及挑选 Kubernetes 对象,仅可用于为资源提供“元数据”信息。另外,注解中的元数据不受字符数量的限制,它可大可小,可以为结构化或非结构化形式,也支持使用在标签中禁止使用的其他字符。

  资源注解(annotation)可以由用户手动添加,也可以由工具程序自动附加冰使用它们。在 Kubernetes 的新版中(alpha 或 beta 阶段)为某资源引入新字段时,常以注解的方式提供,以避免其增删等变动对用户带来困扰,一旦确定支持使用它们,这些新增字段就将再引入到资源中不能给淘汰相关的注解。另外,为资源添加注解也可以让其他用户快速了解资源的相关信息,例如其创建者的身份。以下为常用的场景案例:

  • 由声明式配置层(如 apply 命令)管理的字段:将这些字段定义为注解有助于识别由服务器或客户端设定的默认值、系统自动生成的字段以及由自动伸缩系统生成的字段。
  • 构建、发行或镜像等相关的信息,例如,时间戳、发行ID、Git分支、PR号码、镜像哈希及仓库地址等。
  • 指向日志、监控、分析或审计仓库的指针。
  • 由客户端或工具程序生成的用于调试目的的信息:如名称、版本、构建信息等。
  • 用户或工具程序的来源地信息,例如,来自其他生态系统组件的相关对象的 url。
  • 轻量化滚动升级工具的元数据,如 config 及checkpoints。
  • 相关人员的电话等联系信息,或者指向类似信息的可寻址的目录条目,如网站站点。

 二、查看资源注解

  "kubectl get -o yaml' 和 "kubectl describe" 命令均能显示资源的注解信息。

……
Priority:     0
Node:         mh-k8s-worker-prd-243.35/10.255.243.35
Start Time:   Tue, 17 May 2022 10:48:57 +0800
Labels:       app=washingservice-sorting
              app.kubernetes.io/name=washingservice-sorting
              app.kubernetes.io/version=v1
              bm-org=xxx
              pod-template-hash=5647b86866
              version=v1
Annotations:  cni.projectcalico.org/podIP: 10.233.83.59/32
              cni.projectcalico.org/podIPs: 10.233.83.59/32
Status:       Running
IP:           10.233.83.59
IPs:
  IP:           10.233.83.59
Controlled By:  ReplicaSet/washingservice-sorting-v1-5647b86866
Init Containers:
  sidecar-jmx:
……

三、管理资源注解

  annotatinons 可在资源创建时使用 "metadata.annotations" 字段指定它,也可随时按需在活动的资源上使用 "kubectl annotate" 命令来进行附加:

[root@mh-k8s-master-prd-243-24 ~]# kubectl annotate pods --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, including uninitialized ones, 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='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-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.
      --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-file.
      --overwrite=false: If true, allow annotations to be overwritten, otherwise reject annotation updates that
overwrite existing annotations.
      --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.
  -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, not including uninitialized ones, supports '=', '==', and
'!='.(e.g. -l key1=value1,key2=value2).
      --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).
[root@mh-k8s-master-prd-243-24 ~]# 

  为 pod-example 重新进行注解

[root@mh-k8s-master-prd-243-24 ~]# kubectl annotate pods pod-example ilinux.io/created-by="cluster admin"

  查看生成的注解信息:

[root@mh-k8s-master-prd-243-24 ~]# kubectl describe pods pod-example | grep "Annotations"
posted @ 2022-06-13 16:16  左扬  阅读(2023)  评论(0编辑  收藏  举报
levels of contents