11-Overview-Field Selectors

cucytoman發表於2019-09-24

concepts/overview/working-with-objects/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
本作品採用《CC 協議》,轉載必須註明作者和本文連結