Kubernetes kubectl annotate 命令详解

kubectl annotate

更新一个或多个资源的Annotations信息。

  • Annotations由key/value组成。
  • Annotations的目的是存储辅助数据,特别是通过工具和系统扩展操作的数据,更多介绍在这里
  • 如果--overwrite为true,现有的annotations可以被覆盖,否则试图覆盖annotations将会报错。
  • 如果设置了--resource-version,则更新将使用此resource version,否则将使用原有的resource version。

有效资源类型包括:

  • all
  • certificatesigningrequests (aka 'csr')
  • clusterrolebindings
  • clusterroles
  • clusters (valid only for federation apiservers)
  • componentstatuses (aka 'cs')
  • configmaps (aka 'cm')
  • controllerrevisions
  • cronjobs
  • daemonsets (aka 'ds')
  • deployments (aka 'deploy')
  • endpoints (aka 'ep')
  • events (aka 'ev')
  • horizontalpodautoscalers (aka 'hpa')
  • ingresses (aka 'ing')
  • jobs
  • limitranges (aka 'limits')
  • namespaces (aka 'ns')
  • networkpolicies (aka 'netpol')
  • nodes (aka 'no')
  • persistentvolumeclaims (aka 'pvc')
  • persistentvolumes (aka 'pv')
  • poddisruptionbudgets (aka 'pdb')
  • podpreset
  • pods (aka 'po')
  • podsecuritypolicies (aka 'psp')
  • podtemplates
  • replicasets (aka 'rs')
  • replicationcontrollers (aka 'rc')
  • resourcequotas (aka 'quota')
  • rolebindings
  • roles
  • secrets
  • serviceaccounts (aka 'sa')
  • services (aka 'svc')
  • statefulsets
  • storageclasses
  • thirdpartyresources

语法

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

示例

更新Pod“foo”,设置annotation “description”的value “my frontend”,如果同一个annotation多次设置,则只使用最后设置的value值。

kubectl annotate pods foo description='my frontend'

根据“pod.json”中的type和name更新pod的annotation

kubectl annotate -f pod.json description='my frontend'

更新Pod"foo",设置annotation“description”的value“my frontend running nginx”,覆盖现有的值。

kubectl annotate --overwrite pods foo description='my frontend running nginx'

更新 namespace中的所有pod

kubectl annotate pods --all description='my frontend running nginx'

只有当resource-version为1时,才更新pod ' foo '。

kubectl annotate pods foo description='my frontend running nginx' --resource-version=1

通过删除名为“description”的annotations来更新pod ' foo '。#不需要- overwrite flag。

kubectl annotate pods foo description-

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 annotation
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
local false If true, annotation 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 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.
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 annotation 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].
K8S中文社区微信公众号

译者

Vincent

业精于勤 荒于嬉;行成于思 毁于随;

发表评论

电子邮件地址不会被公开。

Time limit exceeded. Please complete the captcha once again.