Friday 7 August 2020

Kubernetes: Working with labels

 Step 1: Define .yml file with some labels.

labels-demo.yml

apiVersion: v1
kind: Pod
metadata:
  name: user-service
  labels:
    env: dev
    author: Krishna
    application_type: service
    release-version: "1.0"
spec:
  containers:
  - name: user-service
    image: jboss/wildfly

Following labels are defined in the above yml file.

env: dev

         author: Krishna

application_type: service

         release-version: "1.0"

 

Step 2: Create a resource from a file ‘labels-demo.yml’.

$kubectl create -f labels-demo.yml 
pod/user-service created

Step 3: View labels associated with pod using the below command.

kubectl get pods --show-labels

$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE    LABELS
user-service   1/1     Running   0          2m1s   application_type=service,author=Krishna,env=dev,release-version=1.0

How to add labels to the already deployed pod?

Below command add new label ‘support=true’

 

kubectl label pod/user-service support=true

$kubectl label pod/user-service support=true
pod/user-service labeled
$
$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE    LABELS
user-service   1/1     Running   0          6m7s   application_type=service,author=Krishna,env=dev,release-version=1.0,support=true


How to override existing labels?

Use --overwrite option to override existing labels.

 

For example, below statement override the author's name to chamu.

kubectl label pod/user-service author=chamu --overwrite

$kubectl label pod/user-service author=chamu --overwrite
pod/user-service labeled
$
$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE     LABELS
user-service   1/1     Running   0          7m43s   application_type=service,author=chamu,env=dev,release-version=1.0,support=true

How to delete a label?

Below statement delete the label ‘support’.

 

kubectl label pod/user-service support-

$kubectl label pod/user-service support-
pod/user-service labeled
$
$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE   LABELS
user-service   1/1     Running   0          10m   application_type=service,author=chamu,env=dev,release-version=1.0




Previous                                                    Next                                                    Home

No comments:

Post a Comment