Sign In

Curriculum 11: Labels, Selectors & Annotations

Field Selectors

10 min · 15 XP

Field Selectors

Field selectors filter Kubernetes resources by the values of specific resource fields, rather than by labels. They let you query based on built-in properties like status, name, and node assignment.

Basic Usage

Use the --field-selector flag with kubectl get:

# Find pods that are running
kubectl get pods --field-selector status.phase=Running

# Find pods that are NOT running
kubectl get pods --field-selector status.phase!=Running

# Find a specific pod by name
kubectl get pods --field-selector metadata.name=my-app

# Find pods on a specific node
kubectl get pods --field-selector spec.nodeName=worker-1

Combining Field Selectors

Chain multiple field selectors with commas for AND logic:

# Running pods on a specific node
kubectl get pods \
  --field-selector status.phase=Running,spec.nodeName=worker-2

# Combine with namespace
kubectl get pods \
  --field-selector status.phase=Failed -n production

Mixing Field Selectors with Label Selectors

Field selectors and label selectors work independently and can be combined:

# Running pods with a specific label
kubectl get pods \
  --field-selector status.phase=Running \
  -l app=web

# Failed pods belonging to a team
kubectl get pods \
  --field-selector status.phase=Failed \
  -l team=backend -A

Supported Fields

Field selectors have a key limitation: only a small set of fields is supported, and the supported fields vary by resource type.

Fields supported across all resource types:

  • metadata.name
  • metadata.namespace

Additional fields for Pods:

  • status.phase
  • spec.nodeName
  • spec.restartPolicy
  • spec.schedulerName
  • spec.serviceAccountName

Additional fields for Events:

  • reason
  • involvedObject.kind
  • involvedObject.name
  • type
# Find warning events
kubectl get events --field-selector type=Warning

# Find events related to a specific pod
kubectl get events --field-selector involvedObject.name=my-app

Limitations

Field selectors are deliberately limited compared to label selectors:

  • Only a few fields per resource type are indexed and queryable
  • Set-based operators (in, notin) are not supported -- only = and !=
  • Attempting to filter on an unsupported field returns an error
  • For flexible querying, prefer labels over field selectors

Key Takeaways

  • Field selectors filter by resource fields like status.phase and metadata.name
  • Use --field-selector with = and != operators only
  • The metadata.name and metadata.namespace fields work on all resources
  • Combine field selectors with label selectors for precise queries
  • Only a small set of fields is supported per resource type