11-Overview-Field Selectors

concepts/overview/working-with-objects/field-selectors/

Field Selectors

Supported fieldsSupported operatorsChained selectorsMultiple resource types

Field selectors let you select Kubernetes resources based on the value of one or more resource fields. Here are some example field selector queries:字段选择器允许您根据一个或多个资源字段的值选择kubernetes资源。下面是一些字段选择器查询示例:

  • metadata.name=my-service
  • metadata.namespace!=default
  • status.phase=Pending

This kubectl command selects all Pods for which the value of the status.phase field is Running:

此kubectl命令选择status.phase字段值正在运行的所有pod:

kubectl get pods --field-selector status.phase=Running

Note:

Field selectors are essentially resource filters. By default, no selectors/filters are applied, meaning that all resources of the specified type are selected. This makes the following kubectl queries equivalent:字段选择器本质上是资源过滤器。默认情况下,不应用选择器/筛选器,这意味着选定了指定类型的所有资源。这使得以下kubectl查询等效:

kubectl get pods
kubectl get pods --field-selector ""

Supported fields

Supported field selectors vary by Kubernetes resource type. All resource types support the metadata.name and metadata.namespace fields. Using unsupported field selectors produces an error. For example支持的字段选择器因kubernetes资源类型而异。所有资源类型都支持metadata.name和metadata.namespace字段。使用不支持的字段选择器会产生错误。例如:

kubectl get ingress --field-selector foo.bar=baz
Error from server (BadRequest): Unable to find "ingresses" that match label selector "", field selector "foo.bar=baz": "foo.bar" is not a known field selector: only "metadata.name", "metadata.namespace"

Supported operators

You can use the =, ==, and != operators with field selectors (= and == mean the same thing). This kubectl command, for example, selects all Kubernetes Services that aren’t in the default namespace:您可以使用==,和!=带有字段选择器的运算符(===意思相同)。例如,此“kubectl”命令选择不在“default”命名空间中的所有kubernetes服务:

kubectl get services  --all-namespaces --field-selector metadata.namespace!=default

Chained selectors

As with label and other selectors, field selectors can be chained together as a comma-separated list. This kubectl command selects all Pods for which the status.phase does not equal Running and the spec.restartPolicy field equals Always:与[标签](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels)和其他选择器一样,字段选择器可以作为逗号分隔列表链接在一起。此“kubectl”命令选择“status.phase”不等于“running”且“spec.restartpolicy”字段等于“always”的所有播客

kubectl get pods --field-selector=status.phase!=Running,spec.restartPolicy=Always

Multiple resource types

You use field selectors across multiple resource types. This kubectl command selects all Statefulsets and Services that are not in the default namespace:在多个资源类型中使用字段选择器。此“kubectl”命令选择不在“default”命名空间中的所有statefulset和服务:

kubectl get statefulsets,services --all-namespaces --field-selector metadata.namespace!=default
k8s
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!