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

2 minutes reading time

Written by

Civo Team
Civo Team

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

ActionCommand or YAML

Define a label in YAML

metadata.labels: app: nginx

Add a label to an existing pod

kubectl label pod nginx demo=true

View labels on all pods

kubectl get pods --show-labels

Filter pods by label

kubectl get pods -l app=nginx

Equality-based selector

app=nginx or app!=nginx

Set-based selector

app in (nginx, apache)

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 LABELS
nginx 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=nginx
environment!=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/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- 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: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80

NetworkPolicies targeting pods

A NetworkPolicy uses podSelector to define which pods the policy applies to.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-nginx
spec:
podSelector:
matchLabels:
app: nginx
policyTypes:
- 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=canary and 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
Civo Team
Civo Team

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.

View author profile
Course complete

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.