Kubernetes labels and selectors explained (with examples)
Learn how Kubernetes labels and selectors work with practical YAML examples. Covers equality-based and set-based selectors, Deployments, Services, and NetworkPolicy.
4 lessons · 11 min · Intermediate
Written by
Marketing Team at Civo
Written by
Marketing Team at Civo
Labels are key-value pairs attached to Kubernetes objects. Selectors use those labels to identify and group resources. Together they are the mechanism that connects Deployments to pods, Services to pods, and NetworkPolicies to pods. Understanding labels and selectors is fundamental to understanding how Kubernetes objects relate to each other.
Label syntax quick reference
Adding labels to a pod
Labels are defined in the metadata section of any Kubernetes object. You can also add them to existing objects imperatively.
Add a label to an existing pod:
kubectl label pod nginx demo=true
View all pods with their labels:
kubectl get pods --show-labels
Expected output:
NAME READY STATUS RESTARTS AGE LABELSnginx 1/1 Running 0 5m demo=true
Filter pods by a specific label:
kubectl get pods -l app=nginx
Equality-based vs set-based selectors
There are two types of selectors in Kubernetes.
Equality-based selectors match exact values using = or !=:
app=nginxenvironment!=production
Set-based selectors support richer expressions using in, notin, and exists:
app in (nginx, apache)environment notin (production)tier exists
Deployments and Services use equality-based selectors. Newer resources like Jobs and DaemonSets support both. If both matchLabels (equality-based) and matchExpressions (set-based) are specified, both must be true.
Common use cases
Deployments selecting pods
A Deployment uses selector.matchLabels to identify which pods it manages. Every pod created by the Deployment automatically gets the labels defined in the template.
apiVersion: apps/v1kind: Deploymentmetadata:name: nginx-deploymentspec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.25ports:- containerPort: 80
Services routing traffic to pods
A Service uses selector to find the pods it should route traffic to. Any pod with matching labels becomes an endpoint for the service.
apiVersion: v1kind: Servicemetadata:name: nginx-servicespec:selector:app: nginxports:- protocol: TCPport: 80targetPort: 80
NetworkPolicies targeting pods
A NetworkPolicy uses podSelector to define which pods the policy applies to.
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata:name: allow-nginxspec:podSelector:matchLabels:app: nginxpolicyTypes:- Ingress
Other common uses
- Node affinity: labels on nodes allow pods to express scheduling preferences, for example requiring a pod to run only on nodes labelled
disktype=ssd - Canary deployments: label pods with
version=canaryand route a percentage of traffic to them via a separate Service selector - Monitoring: Prometheus uses label selectors to decide which pods to scrape for metrics

Marketing Team at Civo
Civo is the Sovereign Cloud and AI platform designed to help developers and enterprises build without limits. We bridge the gap between the openness of the public cloud and the rigorous security of private environments, delivering full cloud parity across every deployment. As a team, we are dedicated to providing scalable compute, lightning-fast Kubernetes, and managed services that are ready in minutes. Through CivoStack Enterprise and our FlexCore appliance, we empower organizations to maintain total data sovereignty on their own hardware.
Our mission is to make the cloud faster, simpler, and fairer. By providing enterprise-grade NVIDIA GPUs and streamlined model management, we ensure that high-performance AI and machine learning are accessible to everyone. Built for transparency and performance, the Civo Team is here to give you total control over your infrastructure, your data, and your spend.
Share this lesson
Nice work, you finished Kubernetes Concepts: the core building blocks.
Your next step is up to you - keep building on what you've learned, or put it into practice on Civo.
Next Course
Kubernetes Objects: Deployments, pods, StatefulSets and more
9 lessons · 27 min
Learn how to work with Kubernetes objects in practice. Covers pods, deployments, StatefulSets, DaemonSets, init containers, probes, resource limits, and multi-container patterns.
Put it into practice
Spin up your first cluster on Civo
Get $250 free credit and launch a production-ready Kubernetes cluster in under 90 seconds.