liveBook · Manning

Label selector

As explained in Acing the Certified Kubernetes Administrator Exam, Second Edition

A label selector is a Kubernetes matching expression that identifies or associates objects by their labels.

A label selector is a Kubernetes matching expression that identifies or associates objects by labels. Selectors connect Services to matching pods and can guide pod scheduling onto nodes with particular labels. section 4.3 section 6.4.1

Use label selectors to perform these operational tasks:

  • Associate a Service with pods carrying the required label, such as app=internal-svc. section 6.4.1
  • Route Service traffic to the intended Deployment or pods; a selector mismatch can leave the Service without endpoints. section 8.3.3
  • Influence scheduling by labeling nodes and selecting nodes with matching labels. section 4.3
  • Inspect pod labels with kubectl get po --show-labels and compare them with the Service selector. section 8.3.2

Diagnose a Service selector mismatch

When a Service cannot reach its application, inspect its YAML with kubectl get svc -o yaml and inspect pod labels with kubectl get po --show-labels. If the Service selects app=nginx, the target pod must carry app=nginx. Correct the selector or labels, then retry the request; a successful curl returns the nginx welcome page. section 8.3.2 section 8.3.3

spec:
  ports:
  - name: 8080-80
    port: 8080
    protocol: TCP
    targetPort: 80
  - name: search
    port: 9200
    protocol: TCP
    targetPort: 9200
  selector:
    app: internal-svc
  type: ClusterIP
status:
  loadBalancer: {}

Inspect the Service YAML to see the selector field alongside ports and targetPort values. section 6.4.1

How Acing the Certified Kubernetes Administrator Exam, Second Edition covers it

How deep it goes
Explains selector behavior through YAML examples and troubleshooting, stopping short of selector expression syntax beyond label equality.
Examples
Uses Service YAML, app=nginx labels, kubectl output, missing endpoints, and curl verification.
What you can do after
Diagnoses Service reachability with kubectl by comparing selectors, pod labels, endpoints, and port configuration.
How it connects
Ties label selectors to labels, Services, pods, Deployments, node selectors, and scheduling.

Also explained in

Compare how 7 books explain label selector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Certified Kubernetes Administrator Exam, Second Edition. The section links point to the parts of the book it draws on.