Monday 27 July 2020

Kubernetes: Labels, Selectors, Annotations and Namespaces

These are used to organize our application.

 

Lables

Labels are key:value pairs. These can be attached to objects like pods, services, deployments. Labels are mainly used to organize Kubernetes objects.

 

Why labels?

a.   Users can easily identify the objects.

b.   Used to organize clusters

 

Can I change the labels at runtime?

Labels can be added at deployment time and can be changed at any time.

 

Example

environment:prod

release:beta

service:front-end

 

For example, you have hundreds of microservices running in Kubernetes Environment. By providing labels like appType, environment you can easily organize them. Below image depicts the same.

From the above image, you can easily query for all the pods that of type ‘weservice’ and running in prod environment.


employeeServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice

........
........

In the above example, I added labels app, author, serviceType to the Deployment.

 

Selectors

Label selectors are used to identify a group of objects.

 

Types of selectors

a.   Equality Based Selector: =, !=

b.   Set Based Selector: IN, NONIN, EXISTS

 

Label Selectors are used with kubectl command.

 

Namespaces

Allow multiple virtual clusters backed by the same physical cluster. When you launch Kubernetes, a ‘default’ namespace is created. Objects are placed in ‘default’ namespace by default.

 

Annotations

Annotations provide detailed metadata in an object (For example, who created this pod etc.,). Annotations are used by tools.

 

Syntax to create annotation

kubectl annotate pod {podName} annotation1=value1

 

Can I use annotations in queries?

No, annotation is used to provide additional information.

 

Let’s create a deployment and add labels and annotations to it.

 

Step 1: Create a deployment.

 

employeeServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

As you see the definition file, I added three labels to the deployment.

  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice

Create deployment by executing the below command.

$kubectl create -f employeeServiceDeployment.yml
deployment.apps/employee-service-deployment created

You can get all the deployments by executing the below command.

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           29s

Step 2: Let's add a label to the deployment ‘employee-service-deployment’.

 

kubectl label deployment employee-service-deployment  env=prod version=1.24

 

Above statement adds two labels env, version to the deployment ‘employee-service-deployment’.

$kubectl label deployment employee-service-deployment  env=prod version=1.24
deployment.apps/employee-service-deployment labelled

Step 3: Let’s see the labels by executing the below command.

kubectl get deployments --show-labels

$kubectl get deployments --show-labels
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
employee-service-deployment   5/5     5            5           3m15s   app=employee-service-deployment,author=krishna,env=prod,serviceType=webservice,version=1.24

Step 4: Let's create other deployments to understand the usage of selectors.

 

notificationServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notification-service-deployment
  labels:
    app: notification-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: notification-service
      labels:
        app: notification-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: notification-service-container
        image: jboss/wildfly

  replicas: 10
  selector:
    matchLabels:
      app: notification-service

Create the deployment by executing the below command.

$kubectl create -f notificationServiceDeployment.yml 
deployment.apps/notification-service-deployment created

Let’s see the labels associated with all the deployments.

$kubectl get deployments --show-labels
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
employee-service-deployment       5/5     5            5           6m42s   app=employee-service-deployment,author=krishna,env=prod,serviceType=webservice,version=1.24
notification-service-deployment   5/10    10           5           27s     app=notification-service-deployment,author=krishna,serviceType=webservice

Step 5: Query the deployments using selectors.

 

kubectl get deployments --selector app=employee-service-deployment

kubectl get deployments --selector version=1.24

$kubectl get deployments --selector app=employee-service-deployment
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           8m51s
$
$
$kubectl get deployments --selector version=1.24 
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           9m13s

Step 6: You can even get the selector used by Kubernetes for the deployments by executing ‘kubectl describe’ command.

 

kubectl describe deployment employee-service-deployment

$kubectl describe deployment employee-service-deployment
Name:                   employee-service-deployment
Namespace:              default
CreationTimestamp:      Sun, 07 Jun 2020 15:12:19 +0530
Labels:                 app=employee-service-deployment
                        author=krishna
                        env=prod
                        serviceType=webservice
                        version=1.24
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=employee-service
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=employee-service
           author=krishna
           serviceType=webservice
  Containers:
   employee-service-container:
    Image:        jboss/wildfly
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   employee-service-deployment-dff6c9cdf (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  14m   deployment-controller  Scaled up replica set employee-service-deployment-dff6c9cdf to 5

As you see the output, you will find the below statement.

Selector:               app=employee-service

 

How to delete a label?

$kubectl get deployments --show-labels
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
notification-service-deployment   10/10    10           10           26s   app=notification-service-deployment,author=krishna,serviceType=webservice

Using – operator, we can delete the label.

 

For example, below statement delete the label app from the deployment.

kubectl label deployments notification-service-deployment app-

 

Let's delete the label for pods.

$kubectl get pods --show-labels
NAME                                              READY   STATUS    RESTARTS   AGE     LABELS
notification-service-deployment-7dc84f6d4-5wtsg   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-7cc8p   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-92ftm   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-9prw6   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-crknr   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-ct7bq   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-g8hc6   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-mnlb2   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-sgrm4   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-tlsb4   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice

Below statement delete the label author from the pods.

 

kubectl label pods notification-service-deployment-7dc84f6d4-5wtsg  author-

$kubectl label pods notification-service-deployment-7dc84f6d4-5wtsg  author-
pod/notification-service-deployment-7dc84f6d4-5wtsg labeled
$
$kubectl get pods notification-service-deployment-7dc84f6d4-5wtsg --show-labels
NAME                                              READY   STATUS    RESTARTS   AGE     LABELS
notification-service-deployment-7dc84f6d4-5wtsg   1/1     Running   0          4m36s   app=notification-service,pod-template-hash=7dc84f6d4,serviceType=webservice


Previous                                                    Next                                                    Home

No comments:

Post a Comment