Kubernetes Pod With Labels¶
Labels are key/value pairs that are attached to Kubernetes objects, such as pods.
Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system.
Here is the Docker Image used in this tutorial: reyanshkharga/nginx
Step 1: Create Pods With Labels¶
Apply the manifest files to create pods:
# Create frontend pod
kubectl apply -f frontend.yml
# Create backend pod
kubectl apply -f backend.yml
Step 2: List Pods¶
Step 3: Filter Pods Using Labels¶
-
Equality based filtering:
kubectl get pods -l environment=prod kubectl get pods -l environment=dev kubectl get pods -l environment=qa kubectl get pods -l environment=prod,tier=backend kubectl get pods -l environment=prod,tier=frontend kubectl get pods -l environment=dev,tier=frontend kubectl get pods -l environment=qa,tier=backend
-
Set based filtering:
kubectl get pods -l 'environment in (prod, qa)' kubectl get pods -l 'environment in (dev, qa)' kubectl get pods -l 'environment in (prod, dev)' kubectl get pods -l 'environment in (prod, dev, qa)' kubectl get pods -l 'environment in (prod, qa),tier in (backend, frontend)' kubectl get pods -l 'environment in (dev, qa),tier in (backend, frontend)' kubectl get pods -l 'environment in (prod, dev),tier in (backend, frontend)' kubectl get pods -l 'environment in (prod, dev),tier in (database)'