1️⃣ Labels & Selectors in Kubernetes (Detailed Explanation) ⇒
🔹 Labels in Kubernetes
Labels in Kubernetes are key-value pairs assigned to objects like Pods, Nodes, and Services. They provide an efficient way to organize, manage, and filter resources within a cluster.
✅ Key Features of Labels:
- Labels are the mechanism you use to organize kubernetes objects.
- A label is a key-value pair without any predefined meaning that can be attached to the objects. [Name: Utsav, Class: Pods]
- Labels are similar to tags in AWS or git where you use a name to quick reference.
- so you are free to choose labels as you need it to refer an environment which is used for dev or testing or production , refer a product group like departmentA, departmentB
- Labels do not provide uniqueness (unlike names or UIDs).
- A single object can have multiple labels.
- Labels help categorize resources based on the environment, team, application, etc.
- Common use cases:
- Grouping Pods by environment (e.g., env=development)
- Tagging resources with a team name (e.g., team=backend)
- Identifying workloads (e.g., app=nginx)
 
📌 Example of Labels in a Pod Configuration (Declarative Method):
Create a file pod5.yml and define a Pod with labels:
# Example of Labels in a Pod
kind: Pod
apiVersion: v1
metadata:
  name: delhipod
  labels:
    env: development
    class: pods
    love: devops
spec:
  containers:
    - name: c00
      image: ubuntu
      command: ["/bin/bash", "-c", "while true; do echo Hello-Bhupinder; sleep 5 ; done"]
🛠 Applying the Pod Configuration:
kubectl apply -f pod5.yml   # Deploy the pod with labels
kubectl get pods --show-labels  # Display the labels associated with the pod
🔹 Adding Labels to an Existing Pod (Imperative Method)