The meaning of the key field

11/22/2019

I have a deployment, that looks as follows: enter image description here

The question is, what is the difference between the red border label and the violet one?

-- zero_coding
kubernetes

3 Answers

11/22/2019

You can add labels to any k8s object. Pod are one of the objects that take most advantage of it as services target them through labels, but again, any object can have labels.

About deployments, a deployment creates a replicaSet, that in turn creates the pods. The red square is the deployment labels, while the violet is the labels that will have the pods that the replicaSet will create. Note that it is under template section (Pod template).

Now, and this is the important thing with deployments, these two labels must match, in order for a deployment to recognize its "children", otherwise the pods will be orphan. So, if you would change any of these labels, the deployment will create new pods with its labels, and the pods that do not match will run without any controller to back them up.

-- suren
Source: StackOverflow

11/22/2019

These key fields are refereed as Labels on Kubernetes. These labels are used in Kubernetes in order to organize our cluster.

Labels are key/value pairs that are attached to objects that can be used to identify or group resources in Kubernetes. They can be used to select resources from a list.

Labels can be attached to objects at creation time and subsequently added and modified at any time. Each object can have a set of key/value labels defined. Each Key must be unique for a given object.

Going deeper on it, lets suppose you have this Pod in your cluster:

apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
  namespace: default
  labels:  
    env: development
spec:
  containers:
  - name: busybox
    image: busybox
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

As you can see we are setting one label: env: development

If you deploy this pod you can run the following command to list all lables set to this pod:

kubectl get pod sample-pod --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
sample-pod   1/1     Running   0          28s   env=development

You can also list all pods with development label:

$ kubectl get pods -l env=development
NAME         READY   STATUS    RESTARTS   AGE
sample-pod   1/1     Running   0          106s

You can also delete a pod using the label selection:

pod "sample-pod" deleted

Matching objects must satisfy all of the specified label constraints, though they may have additional labels as well. Three kinds of operators are admitted =,==,!=. The first two represent equality (and are simply synonyms), while the latter represents inequality. For example:

environment = production
tier != frontend

You can read more about labels on Kubernetes Documentation.

-- mWatney
Source: StackOverflow

11/22/2019

blue border labels are applied to pod spec where as red border labels are part of deployment spec.

Notice that replicaSet selector uses same key:value pair to identify the related pods.

You can query deployment object using below command

kubectl get deploy -l app=nginx

same way, you can query pod using

kubectl get po -l app=nginx
-- P Ekambaram
Source: StackOverflow