Shashikant shah

Sunday 25 February 2024

Labels, Selector in Kubernetes :-

1. What are Labels and selectors?

In Kubernetes, labels and selectors are key concepts used for organizing and selecting sets of objects.

Labels:- Labels are key-value pairs attached to Kubernetes objects (such as pods, services, and deployments) as metadata. They are used to express identifying attributes of objects.



# kubectl apply -f first pods.yaml 

# kubectl get pods -o wide --show-labels

# kubectl get pods --show-labels




2. How to add label though command.

# kubectl label pod <pod_name>  <env=testing>

# kubectl get pods --show-labels




3. Multiple labels added for pods in yaml file.











4. How to edit/replace the name “env=testing” label to “env=prod” label.

# kubectl edit pods Nginx pod

# kubectl label --overwrite pod nginxpod env=prod

5. How to delete the “env=testing” label.

# kubectl label pod nginxpod env-

6. How to add labels for all pods.

# kubectl  label --all status=xyz

7. Check how many pods are running.

# kubectl  get  pods  --field-selector status.phase=Running

8. Check how many pods is not running.

# kubectl  get  pods  --field-selector status.phase!=Running

9. Check how many pods are running in the namespace.

# kubectl  get  pods  --field-selector status.phase=Running,metadata.namespace=default -L env


1. Pods Selector:-

Selectors are used to filter objects based on their labels. They allow you to define criteria for selecting a specific set of objects.

Supports newer resources such as:

1.ReplicaSets

2. Deployments

3. Jobs

4.DaemonSet

2. ReplicaSet uses Set-based operators and replicationController uses Equality-based.

Types of Selectors:

Equality-Based Selectors: Match objects based on the equality of label values (=, ==, !=).

Set-Based Selectors: Match objects based on the presence or absence of labels ( operator:- in, notin, exists, DoesNotExist).

1) matchLables:  This field is a map of key-value pairs. When using matchLabels, resources are selected where the labels match exactly the key-value pairs specified in the selector.


2) matchExpressions : This field allows for more complex label selectors using a list of requirements. Each requirement contains a key, an operator, and a value. ( operator :- in, notin, exists, DoesNotExist).

For operator: In


When the first expression is run, four pods will be selected, then when the second expression is run, only 2 pods will be selected.

For operator: NotIn  (ignore labels)




When the expression is run, 2 pods is not selected (app: Python) and only 3 pods will be selected.

For example:-

For operator: In,  Notin, equal.




Pods selector

# cat nginx_pods.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: web-app

spec:

  replicas: 3

  selector:

    matchLabels:

      app: my-web-app

  template:

    metadata:

      labels:

        app: my-web-app

    spec:

      containers:

      - name: web-app-container

        image: nginx:latest

        ports:

        - containerPort: 80


1.selector name app=my-web-app :-


2.Pods labels :-


3.service name web-app-service :-


4. For endpoint :-


5.
Curl command :-




No comments:

Post a Comment