Skip to main content

Working with Labels and Selectors

1. Adding Labels to Resources

Resource Type Label Syntax Notes
Pod metadata.labels Labels defined in the pod's metadata section
Service metadata.labels Labels defined in the service's metadata section
Deployment metadata.labels Labels defined in the deployment's metadata section
StatefulSet metadata.labels Labels defined in the statefulset's metadata section
ConfigMap metadata.labels Labels defined in the configmap's metadata section
Secret metadata.labels Labels defined in the secret's metadata section

Example: Adding Labels to a Pod

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
    version: v1.20
    environment: production
spec:
  containers:
  - name: nginx
    image: nginx:1.20

Example: Adding Labels to a Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
  labels:
    app: web
    version: v2.0
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
        version: v2.0
    spec:
      containers:
      - name: web-container
        image: nginx:1.20

2. Viewing Resource Labels

Resource Type Label Query Syntax Example
Pod label=VALUE kubectl get pods -l environment=production
Service label=VALUE kubectl get services -l app=nginx
Deployment label=VALUE kubectl get deployments -l version=v2
Node label=VALUE kubectl get nodes -l role=worker

Example: Get Pods with Specific Label

kubectl get pods -l environment=production

Example: Get Pods with Multiple Labels

kubectl get pods -l environment=production,app=webserver

Example: Get Pods with Label Not Equal to Value

kubectl get pods -l 'environment!=staging'

Example: Get Pods with Label Key Exists

kubectl get pods -l environment

Example: Get Pods with Label Key Does Not Exist

kubectl get pods -l '!environment'
Note: The -l flag supports complex label selectors using operators like =, !=, in, notin, and exists.

Label Query Operators

  • =value: Matches labels with the exact key-value pair.
  • !=value: Excludes labels with the specified key-value pair.
  • key in (value1, value2): Matches labels where key's value is one of the listed values.
  • key notin (value1, value2): Excludes labels where key's value is one of the listed values.
  • key: Matches labels that have the specified key (regardless of value).
  • !key: Matches labels that do not have the specified key.

3. Filtering by Label Selectors

Selector Type Syntax Description Example
Equality-based key=value Matches pods with the specified label key and value environment=production
Set-based key in (value1, value2) Matches pods where key's value is one of the provided values version in (v1, v2)
Set-based key notin (value1, value2) Matches pods where key's value is not one of the provided values version notin (v1, v2)
Existence key Matches pods that have the specified label key, regardless of value environment
Existence !key Matches pods that do not have the specified label key !environment

Example: Using Label Selectors in a Pod Definition

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
    version: v1
    environment: production
spec:
  containers:
  - name: nginx
    image: nginx:latest

Example: Filtering Pods by Label Selector

kubectl get pods -l environment=production
kubectl get pods -l version in (v1, v2)
kubectl get pods -l !environment
Note: Label selectors are used in various Kubernetes resources like Services, Deployments, and ReplicaSets to target specific pods.

4. Using Equality-Based Selectors

Selector Type Syntax Examples Description
matchLabels key=value environment=production Matches pods with a label key equal to value
matchLabels key!=value environment!=staging Matches pods with a label key not equal to value
matchLabels key in (value1, value2) version in (v1.0, v2.0) Matches pods with a label key in the set of values
matchLabels key notin (value1, value2) version notin (v1.0, v2.0) Matches pods with a label key not in the set of values

Example: Using Equality-Based Selectors in a Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
      version: v1.0
  template:
    metadata:
      labels:
        app: nginx
        version: v1.0
    spec:
      containers:
      - name: nginx
        image: nginx:1.21

Example: Using Equality-Based Selectors in a Service

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    matchLabels:
      app: nginx
      version: v1.0
  ports:
    - port: 80

5. Using Set-Based Selectors

Set-based selectors in Kubernetes allow you to select resources based on key-value label pairs using set operations. These selectors support three types of operations:

Operation Description Example
key in (value1, value2) Matches resources where the label key has any of the specified values environment in (production, staging)
key notin (value1, value2) Matches resources where the label key does not have any of the specified values environment notin (development)
key= value or key!= value Matches resources where the label key equals or does not equal a specific value tier=frontend, tier!=backend

Example: Using Set-Based Selectors in a Pod

apiVersion: v1
kind: Pod
metadata:
  name: web-pod
  labels:
    environment: production
    tier: frontend
    version: v1.2.3
spec:
  containers:
  - name: web-container
    image: nginx:latest

Example: Using Set-Based Selectors in a Service

apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    environment in (production, staging)
    tier notin (backend)
  ports:
  - port: 80
    targetPort: 80
Note: Set-based selectors are more powerful than simple equality operators and allow for complex label matching logic.

6. Removing Labels

Resource Type Command Description
Pod kubectl label pod <pod-name> <label-key>- Removes the specified label from a pod
Service kubectl label service <service-name> <label-key>- Removes the specified label from a service
Deployment kubectl label deployment <deployment-name> <label-key>- Removes the specified label from a deployment
Node kubectl label node <node-name> <label-key>- Removes the specified label from a node

Example: Removing a Label from a Pod

kubectl label pod my-pod environment-
Note: The - suffix removes the label key entirely. If you do not include the dash, it will set the label value to an empty string.

7. Overwriting Labels

Resource Type Label Key Label Value Action
Pod app v1 Update to v2
Deployment environment prod Change to staging

Example: Updating a Pod's Label

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
  labels:
    app: v2
    version: latest
spec:
  containers:
  - name: example-container
    image: nginx
Note: Label updates do not affect running pods unless they are recreated.
Warning: Updating labels on running resources may cause selectors to stop matching.

8. Using Labels in YAML

Field Description Example
metadata.labels Labels applied to the resource
{
  "metadata": {
    "labels": {
      "app": "nginx",
      "version": "1.19"
    }
  }
}

Example: Deploying a Pod with Labels

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: nginx
    version: "1.19"
    environment: production
spec:
  containers:
  - name: nginx
    image: nginx:1.19
Note: Labels are key-value pairs used to organize and select resources in Kubernetes. They help in grouping and filtering resources based on specific criteria.

9. Selecting with matchLabels

Field Description Example
matchLabels Map of key-value pairs for label matching "matchLabels": {"environment": "production", "version": "v1"}

Example: Using matchLabels in a Pod Spec

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: app
    image: nginx
  nodeSelector:
    matchLabels:
      environment: production

Example: Using matchLabels in a Service

apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  selector:
    matchLabels:
      app: frontend
      version: v2
  ports:
  - port: 80

10. Selecting with matchExpressions

Field Description Values Notes
matchExpressions Defines a set of key-value pairs to match against labels [{key: string, operator: string, values: [string]} Used in label selectors to filter resources

Example: Using matchExpressions in a Pod selector

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: myapp
    version: v1
spec:
  containers:
  - name: app-container
    image: nginx
  selector:
    matchExpressions:
      - key: app
        operator: In
        values: [myapp]
      - key: version
        operator: NotIn
        values: [v2, v3]
Note: The matchExpressions field allows for more complex label matching than simple key-value pairs.

matchExpressions Operators

  • In: Matches if the label key's value is in the provided list.
  • NotIn: Matches if the label key's value is not in the provided list.
  • Exists: Matches if the label key exists (regardless of value).
  • DoesNotExist: Matches if the label key does not exist.

Was this article helpful?