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






