What are Kubernetes labels and selectors?
Answer
Labels are key-value pairs attached to Kubernetes objects used to organize, select, and filter resources. Selectors match objects by their labels. Labels are fundamental to how Services find pods, Deployments manage ReplicaSets, and scheduling works. Label conventions: metadata: labels: app: my-api environment: production version: "1.0.3" tier: backend owner: platform-team. Common label keys (recommended): app.kubernetes.io/name, app.kubernetes.io/version, app.kubernetes.io/component, app.kubernetes.io/part-of, app.kubernetes.io/managed-by. Label selectors in Services: selector: app: my-api tier: backend — routes traffic to pods with BOTH labels. Equality-based selectors: app=my-api, environment!=staging. Set-based selectors: environment in (production, staging), tier notin (frontend), !experimental. kubectl with selectors: kubectl get pods -l app=my-api,tier=backend kubectl get pods --selector="environment in (production)" kubectl delete pods -l app=old-app. Annotations: different from labels — key-value pairs for non-identifying metadata (deployment notes, build info, external tool config). Not used in selectors. Can contain larger amounts of data. Examples: deployment.kubernetes.io/revision: "3", kubernetes.io/description: "Main API server". Field selectors: filter by resource fields: kubectl get pods --field-selector status.phase=Running,spec.nodeName=node1.