liveBook · Manning

Label selector

As explained in Road to Kubernetes

A label selector is a Kubernetes matching expression that identifies or filters resources by their key-value labels.

A label selector finds Kubernetes resources by their metadata labels: key-value pairs such as `app: my-nginx`. A label name has no special meaning by itself. What matters is that the selector and the resource use the same key and value. section 8.3.2

Use the same match for different deployment tasks:

  • Find Pods: `kubectl get pods -l app=my-nginx` lists matching Pods; `--selector app=my-nginx` is the longer form. If none match, the query returns no Pods. section 8.3.2
  • Route traffic: put `selector: {app: my-nginx}` in a Service manifest so the Service sends requests to matching Pods. section 8.3.2
  • Manage Pods: set a Deployment’s `spec.selector.matchLabels` to match the labels in its Pod template. A StatefulSet uses the same pattern to identify the Pods it manages. section 8.3.3 section 8.4.2

Check a scaled NGINX deployment

With `app: my-nginx` on the Pod template and 15 Deployment replicas, run `kubectl get pods --selector app=my-nginx`. Expect at least 15 matches; an earlier standalone Pod with the same label may also appear. The query matches labels, not just membership in that Deployment. section 8.3.3

Listing 8.6 K8s YAML manifest for an NGINX Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 15
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest

Compare `spec.selector.matchLabels` with `spec.template.metadata.labels`: both specify `app: my-nginx`. section 8.3.3

Listing 8.5 K8s YAML manifest for an NGINX Service
apiVersion: v1
kind: Service
metadata:
  name: first-service-nginx
spec:
  type: NodePort
  ports:
  - port: 5050
    targetPort: 80
    nodePort: 30007
  selector:
    app: my-nginx

The NGINX Service uses the same label pair under `spec.selector` to select its Pods. section 8.3.2

How Road to Kubernetes covers it

How deep it goes
Shows key-value matching in kubectl and Services and `matchLabels` in Deployments; leaves other selector-expression forms unexplored.
Examples
Uses an NGINX Service and 15-replica Deployment sharing `app: my-nginx`.
What it assumes
Introduces metadata labels as key-value pairs before using selectors; assumes basic ability to apply manifests and run kubectl.
What you can do after
Operate/configure selectors to filter resources, connect a Service to labeled Pods, and match a Deployment to its Pod template.
How it connects
Links metadata labels, Pods, Services, Deployments, StatefulSets, and kubectl filtering.

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 Road to Kubernetes. The section links point to the parts of the book it draws on.