Kubernetes field selectors vs label selectors

3/19/2020

In Kubernetes field selectors are limited to certain fields for each resource Kind. But almost every resource has field selector for name and namespace on metadata If so why there's a need to have a separate label selector.

labels:
{
  app: foo
}

Instead of querying kubectl get pods -l app=foo, why couldn't it be part of generic field selector like:
kubectl get pods --field-selector metadata.labels.app=foo

-- manikawnth
kubernetes

2 Answers

3/19/2020

Labels enable users to map their own organizational structures onto system objects in a loosely coupled fashion, without requiring clients to store these mappings.It does not directly imply semantics to the core system.

For field selector clients need to store mappings.So using field selector for everything is not scalable.

-- Arghya Sadhu
Source: StackOverflow

3/19/2020

Short answer: because etcd is not optimized for general purpose querying and so Kubernetes has to pick and choose what to index and what not to. This is why both labels and annotations exist despite seeming very similar, labels are indexed for searching on and annotations are not.

-- coderanger
Source: StackOverflow