Skip to content

Labels

Labels

Using Labels

With describe deployment command you can see the name of the label:

$ kubectl describe deployment
Name:                   kubernetes-bootcamp
Namespace:              default
CreationTimestamp:      Thu, 01 Feb 2018 18:58:47 -0800
Labels:                 run=kubernetes-bootcamp
Annotations:            deployment.kubernetes.io/revision=1
Selector:               run=kubernetes-bootcamp
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  run=kubernetes-bootcamp
  Containers:
   kubernetes-bootcamp:
    Image:        docker.io/jocatalin/kubernetes-bootcamp:v1
    Port:         8080/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   kubernetes-bootcamp-5d7f968ccb (1/1 replicas created)
Events:          <none>

Let’s use this label to query our list of Pods. We’ll use the kubectl get pods command with -l as a parameter, followed by the label values

$ kubectl get pods -l run=kubernetes-bootcamp
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-5d7f968ccb-njgzx   1/1       Running   0          4h

You can do the same to list the existing services:

$ kubectl get services -l run=kubernetes-bootcamp
NAME                  TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes-bootcamp   NodePort   10.106.141.147   <none>        8080:30626/TCP   3h

To apply a new label we use the label command followed by the object type, object name and the new label:

$ kubectl label pod $POD_NAME app=v1
pod "kubernetes-bootcamp-5d7f968ccb-njgzx" labeled

To see that if the label is attached

 $ kubectl describe pods $POD_NAME
Name:           kubernetes-bootcamp-5d7f968ccb-njgzx
Namespace:      default
Node:           k8s-node1/192.168.11.11
Start Time:     Thu, 01 Feb 2018 18:58:47 -0800
Labels:         app=v1
                pod-template-hash=1839524776
                run=kubernetes-bootcamp
Annotations:    <none>
Status:         Running
IP:             10.244.2.3
Controlled By:  ReplicaSet/kubernetes-bootcamp-5d7f968ccb
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://1cfd60b5838438a62759183381aa374344304b4dac30a7d0132a2a87b9d41fcd
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           8080/TCP
    State:          Running
      Started:      Thu, 01 Feb 2018 18:59:13 -0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-fzcmd (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  default-token-fzcmd:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-fzcmd
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none

To query now the list of pods using the new label:

$ kubectl get pods -l app=v1
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-5d7f968ccb-njgzx   1/1       Running   0          4h

To list all pods with thier lables

$ kubectl get pods --show-labels
NAME                                READY     STATUS    RESTARTS   AGE       LABELS
nginx-deployment-6c54bd5869-glcvh   1/1       Running   0          10m       app=nginx,pod-template-hash=2710681425
nginx-deployment-6c54bd5869-rgbvv   1/1       Running   0          10m       app=nginx,pod-template-hash=2710681425