Saturday, 8 August 2020

Kubernetes: Labels: Working with 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

 

selectors.yml
apiVersion: v1
kind: Pod
metadata:
  name: user-management-service
  labels:
    env: dev
    author: Krishna
    application_type: service
    team: Marketting
    release-version: "1.23"
spec:
  containers:
  - name: user-management-service
    image: jboss/wildfly
---
apiVersion: v1
kind: Pod
metadata:
  name: domain-managemet-service
  labels:
    env: dev
    author: chamu
    application_type: service
    team: HR
    release-version: "1.25"
spec:
  containers:
  - name: domain-management-service
    image: jboss/wildfly
---
apiVersion: v1
kind: Pod
metadata:
  name: content-management-service
  labels:
    env: prod
    author: krishna
    application_type: service
    team: Finance
    release-version: "7.5"
spec:
  containers:
  - name: content-management-service
    image: jboss/wildfly

Step 1: Create a resource from a file ‘selectors.yml’ by executing the below command.

kubectl create -f selectors.yml
$kubectl create -f selectors.yml 
pod/user-management-service created
pod/domain-managemet-service created
pod/content-management-service created

= and != operator

a. Get all the prods where the environment is prod

kubectl get pods --selector env=prod

$kubectl get pods --selector env=prod
NAME                         READY   STATUS    RESTARTS   AGE
content-management-service   1/1     Running   0          80s

b. Get all the prods where the environment is not prod

kubectl get pods --selector env!=prod

$kubectl get pods --selector env!=prod
NAME                       READY   STATUS    RESTARTS   AGE
domain-managemet-service   1/1     Running   0          4m20s
user-management-service    1/1     Running   0          4m20s

IN Operator

kubectl get pods -l 'release-version in (1.23, 7)' --show-labels

$kubectl get pods -l 'release-version in (1.23, 7)' --show-labels
NAME                      READY   STATUS    RESTARTS   AGE     LABELS
user-management-service   1/1     Running   0          4m20s   application_type=service,author=Krishna,env=dev,release-version=1.23,team=Marketting

NOT IN Operator

kubectl get pods -l 'release-version notin (1.23, 7)' --show-labels

$kubectl get pods -l 'release-version notin (1.23, 7)' --show-labels
NAME                         READY   STATUS    RESTARTS   AGE     LABELS
content-management-service   1/1     Running   0          5m13s   application_type=service,author=krishna,env=prod,release-version=7.5,team=Finance
domain-managemet-service     1/1     Running   0          5m13s   application_type=service,author=chamu,env=dev,release-version=1.25,team=HR


Previous                                                    Next                                                    Home

No comments:

Post a Comment