6
votes

After reading the official documentation on kubernetes.io, I am still wondering what exactly is the difference between label and selector in Kubernetes?

Editing: For example consider the following Kubernetes object, what is the difference between Labels and Selectors of the following yaml file.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: label-demo
  labels:
    environment: production
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: App1
  template:
    metadata:
      labels:
        environment: production
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
1

1 Answers

8
votes

Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system. Labels can be used to organize and to select subsets of objects.

Via a label selector, the client/user can identify a set of objects. The label selector is the core grouping primitive in Kubernetes.

In a nutshell label selectors depend on labels to select a group of resources such as pods. For example a deployment selects a group of pods by a label selector in the deployment spec.