Kubernetes Labels and Selectors
Kubernetes Labels and Selectors
Section titled βKubernetes Labels and SelectorsβIn the world of Kubernetes, where thousands of containers can run across hundreds of nodes, organization isnβt just a βnice-to-haveβ it is critical for survival. Labels and Selectors are the core mechanism Kubernetes uses to group, filter, and operate on these resources.
Unlike traditional systems where grouping is often hierarchical (folders, groups), Kubernetes uses a flat, loosely coupled tagging system. This allows for multidimensional organization without rigid structures.
> Labels are for identifying attributes of objects that are meaningful to users, while Selectors are for grouping those objects.
Key Characteristics to Remember
Section titled βKey Characteristics to RememberβCharacteristicDescriptionKey-Value PairLabels always come in two parts: a key and a value (e.g., environment: production).Metadata OnlyLabels do not affect the Podβs actual application logic; they are just descriptive data attached to it.Many-to-ManyOne Pod can have multiple labels (e.g., app: nginx, env: prod), and many Pods can share the same label.QueryableYou use βSelectorsβ (filters) to search for resources based on their labels.MutableYou can add, change, or remove labels from a running Pod at any time without restarting it.
Kubernetes is a dynamic environment. Pods come and go, get new IP addresses, and move between nodes. You cannot rely on static IDs or IPs to track them. This is where the decoupling power of Labels and Selectors comes in.
A Label is attached to a Kubernetes object at creation time or later. It stores identifying metadata. A Selector is a core grouping primitive used in Kubernetes. The API currently supports two types of selectors: equality-based and set-based. A Label Selector allows a client (user or another resource) to identify a set of objects. This is the glue that holds loosely coupled components together for example, a Service knows which Pods to send traffic to only because the Serviceβs selector matches the Podsβ labels.
Equality-Based
Section titled βEquality-BasedβIf you are just starting, focus on the βEquality-Basedβ selectors. This is the simplest form.
-
Syntax:
key = value -
Example: You have a web server Pod. You label it
app: nginx. -
Action: When you create a Service to expose this web server, you tell the Service: βLook for any Pod where
appequalsnginxapp: nginx.β -
Result: The Service finds your Pod and starts routing traffic to it. If you remove the label, the Service stops sending traffic instantly.
Set-Based Selectors
Section titled βSet-Based SelectorsβAs you advance, you will need Set-Based Selectors. These allow for complex filtering.
-
Operators:
in,notin,exists(key exists),doesnotexist. -
Scenario: You want to update all apps that are in
productionORstagingenvironments, but NOTtesting. -
Selector Logic:
environment in (production, staging).
You can also combine selectors. For example, in a kubectl command, you can use a comma to say βANDβ: kubectl get pods -l environment=production,tier=frontend This fetches Pods that have both labels.
Syntax and Constraints
Section titled βSyntax and ConstraintsβA valid label key has two segments: an optional prefix and a name, separated by a slash (/).
-
Name (Required):
-
Max 63 characters.
-
Must start and end with an alphanumeric character (
[a-z0-9A-Z]). -
Can contain dashes (
-), underscores (_), dots (.), and alphanumerics. -
Prefix (Optional):
-
DNS subdomain (e.g.,
devsecopsguru.in/). -
Max 253 characters.
-
Note: The
kubernetes.io/andk8s.io/prefixes are reserved for Kubernetes core components.
Example: Pod Definition with Labels
Section titled βExample: Pod Definition with LabelsβapiVersion: v1kind: Podmetadata: name: nginx-pod labels: app: nginx environment: production tier: frontend owner: devops-teamspec: containers: - name: nginx image: nginx:latestKey Components
Section titled βKey Componentsβ-
Prefix (Optional): Used to avoid collisions (e.g.,
k8s.io/). -
Name (Required): The main identifier (e.g.,
app). -
Value (Optional): The data (e.g.,
nginx).
Benefits
Section titled βBenefitsβ-
Organization: Turns a chaotic list of 100 Pods into neat, filtered groups.
-
Automation: CI/CD tools rely on labels to know which Deployments to update.
-
Loose Coupling: Services donβt need to know the exact names of Pods; they just need to know the label.
Use Cases
Section titled βUse Casesβ-
Canary Deployments: Label 5% of your Pods with
track: canaryand 95% withtrack: stable. -
Environment Separation: Use
env: dev,env: stage,env: prodto keep resources distinct even if they live in the same cluster. -
Debugging: Label a crashing Pod with
status: investigating. This signals to your team that you are looking into it.
Common Issues
Section titled βCommon Issuesβ-
Typos:
env: prodvsenv: production. If you are not consistent, your Selectors will miss resources. -
Selector Mismatch: Creating a Service that looks for
app: frontendwhen your Pod is labeledname: frontend. The Service will find nothing. -
Over-labeling: Adding 50 labels to every Pod makes the YAML file hard to read and manage.
Solutions
Section titled βSolutionsβ-
Validation: Use tools like
kube-linteror admission controllers to enforce valid labels before the YAML is applied. -
Automation: Use Helm charts or Kustomize to apply labels automatically so humans donβt make typing mistakes.
-
Documentation: Maintain a central list of βStandard Labelsβ that everyone must use.
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels
https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels
Selectors
Section titled βSelectorsβIf Labels are the βtags,β Label Selectors are the βsearch queries.β Selectors allow you to identify a set of objects based on their labels. This is the primary grouping mechanism in Kubernetes.
> Labels are the Identity (Who am I?), and Selectors are the Group Definition (Who do I want?).
Key Characteristics to Remember
Section titled βKey Characteristics to RememberβCharacteristicDescriptionThe BridgeSelectors are the only link between a Service/Deployment and its Pods. There is no hard-coded linking.Equality-BasedThe simplest type. Matches exact values. Uses =, ==, or !=.Set-BasedThe powerful type. Matches a list of values. Uses in, notin, or exists.**Logic is βANDβ**If you define multiple selectors (e.g., app: ui, env: prod), a Pod must have BOTH to be selected.Live & DynamicSelectors never stop working. If you add a label to a running Pod right now, the Selector picks it up immediately.
The core logic of a Selector is Dynamic Grouping. In traditional IT, we used static IP addresses to connect servers. In Kubernetes, we use Selectors to create βLogical Sets.β
When a Deployment needs to know which Pods it manages, it uses a Selector. When a Service needs to know where to route a userβs request, it uses a Selector. This means you can scale from 1 Pod to 1,000 Pods, and you never have to update the Service configuration. The Serviceβs selector app=frontend automatically βcatchesβ all 1,000 Pods because they all share that label.
There are two types of selectors supported by the Kubernetes API:
Equality-Based Selectors
Section titled βEquality-Based SelectorsβThese allow filtering by label keys and values. Three operators are allowed: =, ==, and !=.
-
environment = production: Selects resources where the key isenvironmentand value isproduction. -
tier != frontend: Selects resources where the keytieris present but the value is NOTfrontend, or the keytieris missing entirely. -
Keyword:
matchLabels -
**How it looks in YAML:**YAML
selector: matchLabels: app: my-web-app tier: frontend -
What it means: βFind me all Pods that have the label
appequal tomy-web-appANDtierequal tofrontend.β -
Note: If a Pod has extra labels (like
version: v1), it is still selected. It just needs to match the ones you asked for.
Tools for Beginners:
-
Kubectl: Try
kubectl get pods --selector app=nginx. This is the manual way to run a selector. -
Lens / OpenLens: Click on a Service in Lens, and it will auto-generate the selector query to show you exactly which Pods are being targeted.
Set-Based Selectors
Section titled βSet-Based SelectorsβThese allow filtering keys according to a set of values. Operators are: in, notin, and exists (often implicitly checking just the key).
-
environment in (production, qa): Selects resources withenvironmentset to eitherproductionORqa. -
tier notin (frontend, backend): Selects resources withtierNOT set tofrontendorbackend. -
partition: Selects all resources that have a label with the keypartition(value doesnβt matter). -
Keyword:
matchExpressions -
Where used: Job, Deployment, DaemonSet, ReplicaSet (and node affinity).
-
Structure:
Bash
selector: matchLabels: app: backend # Older style (Equality) matchExpressions: # Newer style (Set-based) - {key: tier, operator: In, values: [cache, database]} - {key: environment, operator: NotIn, values: [dev]}``
selector: matchLabels: app: backend # Older style (Equality) matchExpressions: # Newer style (Set-based) - {key: tier, operator: In, values: [cache, database]} - {key: environment, operator: NotIn, values: [dev]}-
The Operators:
-
In: The value must be one of these (Logic: A OR B). -
NotIn: The value must NOT be one of these. -
Exists: The label key exists (value doesnβt matter). -
DoesNotExist: The label key must not exist at all.
Tools for Advanced Users:
-
Kube-score: A static analysis tool that checks your YAMLs. It can warn you if your selectors are missing or targeting the wrong things.
-
Goldilocks: While mostly for resources, it helps visualize if your deployments (grouped by selectors) are set up correctly.
Key Components
Section titled βKey Componentsβ-
matchLabels: Simple map of key-value pairs (Exact match).
-
matchExpressions: List of requirements (Set-based logic).
-
key: The label key to check. -
operator: The logic (In,NotIn,Exists,DoesNotExist). -
values: The array of values to check against.
Use Cases
Section titled βUse Casesβ-
Release Management: Use a selector
version: v2to route traffic only to the new version of your app. -
Debugging: Create a Service with a selector
status: debug. Then, label any problematic Pod withstatus: debugto instantly pull it into a special βdebugging network.β -
Cleanups:
kubectl delete pods -l tier=frontenddeletes all frontend pods at once.
Benefits
Section titled βBenefitsβ-
Self-Healing: If a Pod dies, the ReplicaSet sees the number of βselectedβ Pods drop. It creates a new one to match the selector.
-
No Hardcoding: You never have to write an IP address in your code.
-
Load Balancing: The Service selector automatically distributes traffic to all matching Pods.
Common Issues
Section titled βCommon Issuesβ-
The βOrphanedβ Service: You create a Service with selector
app: nginx, but your Pods are labeledapp: my-nginx. The Service points to nothing. -
Selector Overlap: Two different Deployments have the same labels. The Service will send traffic to both sets of Pods randomly. This causes weird errors where 50% of requests fail.
-
Immutable Fields: Trying to edit a Deploymentβs selector and getting an API error.
Solutions
Section titled βSolutionsβ-
Use
kubectl get endpoints: This is the 1stdebugging command. If your Service has no endpoints, your Selector is wrong. -
Label Verification: Before applying a Service, run
kubectl get pods --show-labelsto ensure your spelling matches exactly. -
Unique Labels: Always include a unique label (like
deployment-id) to prevent overlaps.
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
How Kubernetes Uses Them: The βGlueβ
Section titled βHow Kubernetes Uses Them: The βGlueββHow Labels and Selectors Bind Everything
We have learned about Labels (the sticky notes) and Selectors (the filters). Now, letβs talk about the Glue the magic that connects them.
In Kubernetes, there are no cables. Everything is floating in the cluster. The βGlueβ is simply a Matching Rule.
-
The Rule: A βServiceβ (or Deployment) constantly shouts: βI am looking for anyone with the tag
app: frontend!β -
The Match: Any Pod that has that tag instantly βsticksβ to the Service.
-
The Magic: If you peel the label off a Pod, it instantly βunsticksβ and falls away. The connection is broken, even though the Pod is still running.
> The βGlueβ is not a permanent weld; it is a magnetic attraction that only exists as long as the Label matches the Selector.
Key Characteristics to Remember
Section titled βKey Characteristics to RememberβCharacteristicDescriptionLoose CouplingThe Service and Pod are independent. They donβt know each otherβs IDs, only the labels.Dynamic BindingThe connection is live. It is re-evaluated constantly (milliseconds).Self-HealingIf a βgluedβ Pod dies, the Selector notices the gap and the Deployment creates a replacement to fill the glue spot.Endpoint ObjectThe actual list of IPs that stick to a Service is stored in a hidden object called Endpoints.Many-to-OneA single Service (Selector) can glue itself to hundreds of Pods (Labels) at once.
In Kubernetes, the βGlueβ isnβt a static configuration. It is an active process managed by the Controller Manager.
-
Watch: The Controller watches the state of the cluster.
-
Filter: It applies the Selector (e.g.,
app=nginx) to the list of all running Pods. -
Update: It updates the
Endpointsobject with the IP addresses of the matching Pods. -
Repeat: If a Pod is added or removed, the loop runs again and updates the list instantly. This is why Kubernetes feels so responsive.
Service Discovery (Service -> Pods)
Section titled βService Discovery (Service -> Pods)βFocus on the Service-to-Pod connection. This is the most common use of the βGlue.β
-
Scenario: You have 3 Web Pods. You want one IP address to access all of them.
-
The Glue: The Service.
-
Visual:
-
Pod A (
app: web) <β GLUED β> Service (selector: app: web) -
Pod B (
app: web) <β GLUED β> Service -
Pod C (
app: db) <β X (No Glue) X β> Service
Tools:
kubectl get endpoints: This command reveals the glue. It shows you the IP addresses of the Pods that were successfully βcaughtβ by the Service.
A Service explicitly uses a selector to determine which Pods to send traffic to.
apiVersion: v1kind: Servicemetadata: name: my-servicespec: selector: app: web # This Service targets any Pod with label app=nginx tier: frontend ports: - protocol: TCP port: 80 targetPort: 9376Deployments & ReplicaSets (Controller -> Pods)
Section titled βDeployments & ReplicaSets (Controller -> Pods)βNow, look at the Deployment-to-ReplicaSet connection.
-
Ownership: A Deployment manages a ReplicaSet, and a ReplicaSet manages Pods. How does it know which Pods it owns? The Glue.
-
The βOrphanβ Risk: If you manually remove a label from a running Pod that belongs to a Deployment:
-
The Deploymentβs selector no longer matches the Pod.
-
The βGlueβ breaks.
-
The Deployment thinks: βOh no! I lost a Pod!β
-
It immediately creates a new Pod to replace it.
-
The old Pod is now an βOrphanβ it is still running, but the Deployment ignores it.
Tools for Advanced Users:
-
kubectl get pods --show-labels: Essential for debugging broken glue. -
kubectl label --overwrite: The tool to break or fix glue on the fly.
A Deployment manages a set of Pods. It must know which Pods it owns. It uses matchLabels or matchExpressions.
apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-deploymentspec: replicas: 3 selector: matchLabels: app: nginx template: # The Pod template metadata: labels: app: nginx # MUST match the selector above spec: containers: - name: nginx image: nginx:1.14.2Critical Rule: The spec.template.metadata.labels must match the spec.selector. If they donβt, the Deployment will fail to create the Pods (or will create them and immediately lose track of them).
Advanced: matchExpressions
Section titled βAdvanced: matchExpressionsβFor more complex logic in deployments (like βschedule this pod on nodes that are NOT in zone-aβ), we use matchExpressions.
YAML
selector: matchLabels: component: redis matchExpressions: - {key: tier, operator: In, values: [cache]} - {key: environment, operator: NotIn, values: [dev]}Best Practices & Recommended Labels
Section titled βBest Practices & Recommended LabelsβDonβt just use arbitrary names like foo=bar. Kubernetes recommends a standard set of labels to visualize and manage applications effectively across tools.
KeyDescriptionExampleapp.kubernetes.io/nameThe name of the applicationmysql``app.kubernetes.io/instanceA unique name identifying the instancemysql-abcxzy``app.kubernetes.io/versionThe current version of the application5.7.21``app.kubernetes.io/componentThe component within the architecturedatabase``app.kubernetes.io/part-ofThe name of a higher-level applicationwordpress``app.kubernetes.io/managed-byThe tool being used to manage the operationhelm
Practice Lab
Section titled βPractice LabβExecutable YAML file that demonstrates Labels, Selectors (Equality & Set-Based), and the βGlueβ logic.
1: Create k8s-labels.yaml and copy below code.
Bash
# ==============================================================================# KUBERNETES LABELS & SELECTORS## This file demonstrates:# 1. Best Practice Labeling (Standard Labels)# 2. Equality-Based Selectors (Service -> Pod)# 3. Set-Based Selectors (Deployment -> Pod)# 4. The "Glue" Logic (How Services find Deployment Pods)# ==============================================================================
# ------------------------------------------------------------------------------# 1. THE "MANUAL" POD# Use Case: A standalone pod to demonstrate rich metadata labeling.# ------------------------------------------------------------------------------apiVersion: v1kind: Podmetadata: name: manual-web-pod # -------------------------------------------------------------------------- # LABELS: The "Sticky Notes" # These do not change how the pod runs, but they allow us to find it later. # -------------------------------------------------------------------------- labels: # --- Standard Kubernetes Labels (Best Practice) --- app.kubernetes.io/name: my-web-app # What application is this? app.kubernetes.io/instance: manual-01 # Which specific instance? app.kubernetes.io/version: "1.27" # Version (Must be a string) app.kubernetes.io/component: frontend # Architecture layer app.kubernetes.io/part-of: corporate-site # Higher-level system app.kubernetes.io/managed-by: kubectl # What tool deployed this?
# --- Custom Organization Labels --- environment: production tier: frontend owner: devops-team-a cost-center: "9901"
# --- Operational Labels (for debugging/logic) --- status: stable security-scan: passedspec: containers: - name: nginx image: nginx:alpine
---
# ------------------------------------------------------------------------------# 2. THE SERVICE (THE "GLUE")# Use Case: Demonstrates "Equality-Based" Selectors.# This Service acts like a magnet. It attracts ANY Pod that matches its selector.# ------------------------------------------------------------------------------apiVersion: v1kind: Servicemetadata: name: web-service-glue labels: description: "public-entry-point"spec: type: ClusterIP # -------------------------------------------------------------------------- # SELECTOR: The "Filter" / "Search Query" # Logic: "Select ALL Pods where (app.kubernetes.io/name == my-web-app) AND (tier == frontend)" # -------------------------------------------------------------------------- selector: app.kubernetes.io/name: my-web-app # Equality Match tier: frontend # Equality Match
# CRITICAL NOTE: This selector will match: # 1. The 'manual-web-pod' defined above. # 2. The Pods created by the Deployment defined below. # PROOF: Run 'kubectl get endpoints web-service-glue' to see all IPs grouped together. ports: - port: 80 targetPort: 80
---
# ------------------------------------------------------------------------------# 3. THE DEPLOYMENT (THE "MANAGER")# Use Case: Demonstrates "Set-Based" Selectors (Advanced).# The Deployment manages Pods. It must find them to ensure 3 replicas exist.# ------------------------------------------------------------------------------apiVersion: apps/v1kind: Deploymentmetadata: name: advanced-web-deployment labels: app.kubernetes.io/name: my-web-appspec: replicas: 3
# -------------------------------------------------------------------------- # ADVANCED SELECTOR: matchExpressions # This is more powerful than the simple key:value pair. # -------------------------------------------------------------------------- selector: matchLabels: app.kubernetes.io/name: my-web-app # Simple match matchExpressions: # Logic: Tier must be IN the list [frontend, ui, web] - key: tier operator: In values: - frontend - ui - web # Logic: Environment must NOT be 'dev' (Safety check) - key: environment operator: NotIn values: - dev # Logic: The label 'owner' must exist (Value doesn't matter, just the key) - key: owner operator: Exists
# -------------------------------------------------------------------------- # POD TEMPLATE: The Blueprint # The labels here MUST match the selector above, or the Deployment fails. # -------------------------------------------------------------------------- template: metadata: labels: # These match the 'matchLabels' part app.kubernetes.io/name: my-web-app
# These match the 'matchExpressions' part tier: frontend # Is inside [frontend, ui, web] -> MATCH environment: production # Is NOT [dev] -> MATCH owner: devops-automation # Key 'owner' exists -> MATCH
# Extra labels (ignored by selector, but useful for other tools) version: "2.0" spec: containers: - name: nginx image: nginx:alpine
---
# ------------------------------------------------------------------------------# 4. THE "CANARY" SERVICE (TRAFFIC SPLITTING)# Use Case: Demonstrates how to target a SUBSET of the same pods.# ------------------------------------------------------------------------------apiVersion: v1kind: Servicemetadata: name: canary-service-checkspec: type: ClusterIP selector: # This acts as an AND operator. # It will ONLY select pods that have BOTH labels. app.kubernetes.io/name: my-web-app version: "2.0"
# RESULT: # - Will it pick up 'manual-web-pod'? NO (It has version "1.27") # - Will it pick up Deployment pods? YES (They have version "2.0") ports: - port: 80 targetPort: 80``
# ==============================================================================# KUBERNETES LABELS & SELECTORS## This file demonstrates:# 1. Best Practice Labeling (Standard Labels)# 2. Equality-Based Selectors (Service -> Pod)# 3. Set-Based Selectors (Deployment -> Pod)# 4. The "Glue" Logic (How Services find Deployment Pods)# ==============================================================================
# ------------------------------------------------------------------------------# 1. THE "MANUAL" POD# Use Case: A standalone pod to demonstrate rich metadata labeling.# ------------------------------------------------------------------------------apiVersion: v1kind: Podmetadata: name: manual-web-pod # -------------------------------------------------------------------------- # LABELS: The "Sticky Notes" # These do not change how the pod runs, but they allow us to find it later. # -------------------------------------------------------------------------- labels: # --- Standard Kubernetes Labels (Best Practice) --- app.kubernetes.io/name: my-web-app # What application is this? app.kubernetes.io/instance: manual-01 # Which specific instance? app.kubernetes.io/version: "1.27" # Version (Must be a string) app.kubernetes.io/component: frontend # Architecture layer app.kubernetes.io/part-of: corporate-site # Higher-level system app.kubernetes.io/managed-by: kubectl # What tool deployed this?
# --- Custom Organization Labels --- environment: production tier: frontend owner: devops-team-a cost-center: "9901"
# --- Operational Labels (for debugging/logic) --- status: stable security-scan: passedspec: containers: - name: nginx image: nginx:alpine
---
# ------------------------------------------------------------------------------# 2. THE SERVICE (THE "GLUE")# Use Case: Demonstrates "Equality-Based" Selectors.# This Service acts like a magnet. It attracts ANY Pod that matches its selector.# ------------------------------------------------------------------------------apiVersion: v1kind: Servicemetadata: name: web-service-glue labels: description: "public-entry-point"spec: type: ClusterIP # -------------------------------------------------------------------------- # SELECTOR: The "Filter" / "Search Query" # Logic: "Select ALL Pods where (app.kubernetes.io/name == my-web-app) AND (tier == frontend)" # -------------------------------------------------------------------------- selector: app.kubernetes.io/name: my-web-app # Equality Match tier: frontend # Equality Match
# CRITICAL NOTE: This selector will match: # 1. The 'manual-web-pod' defined above. # 2. The Pods created by the Deployment defined below. # PROOF: Run 'kubectl get endpoints web-service-glue' to see all IPs grouped together. ports: - port: 80 targetPort: 80
---
# ------------------------------------------------------------------------------# 3. THE DEPLOYMENT (THE "MANAGER")# Use Case: Demonstrates "Set-Based" Selectors (Advanced).# The Deployment manages Pods. It must find them to ensure 3 replicas exist.# ------------------------------------------------------------------------------apiVersion: apps/v1kind: Deploymentmetadata: name: advanced-web-deployment labels: app.kubernetes.io/name: my-web-appspec: replicas: 3
# -------------------------------------------------------------------------- # ADVANCED SELECTOR: matchExpressions # This is more powerful than the simple key:value pair. # -------------------------------------------------------------------------- selector: matchLabels: app.kubernetes.io/name: my-web-app # Simple match matchExpressions: # Logic: Tier must be IN the list [frontend, ui, web] - key: tier operator: In values: - frontend - ui - web # Logic: Environment must NOT be 'dev' (Safety check) - key: environment operator: NotIn values: - dev # Logic: The label 'owner' must exist (Value doesn't matter, just the key) - key: owner operator: Exists
# -------------------------------------------------------------------------- # POD TEMPLATE: The Blueprint # The labels here MUST match the selector above, or the Deployment fails. # -------------------------------------------------------------------------- template: metadata: labels: # These match the 'matchLabels' part app.kubernetes.io/name: my-web-app
# These match the 'matchExpressions' part tier: frontend # Is inside [frontend, ui, web] -> MATCH environment: production # Is NOT [dev] -> MATCH owner: devops-automation # Key 'owner' exists -> MATCH
# Extra labels (ignored by selector, but useful for other tools) version: "2.0" spec: containers: - name: nginx image: nginx:alpine
---
# ------------------------------------------------------------------------------# 4. THE "CANARY" SERVICE (TRAFFIC SPLITTING)# Use Case: Demonstrates how to target a SUBSET of the same pods.# ------------------------------------------------------------------------------apiVersion: v1kind: Servicemetadata: name: canary-service-checkspec: type: ClusterIP selector: # This acts as an AND operator. # It will ONLY select pods that have BOTH labels. app.kubernetes.io/name: my-web-app version: "2.0"
# RESULT: # - Will it pick up 'manual-web-pod'? NO (It has version "1.27") # - Will it pick up Deployment pods? YES (They have version "2.0") ports: - port: 80 targetPort: 802: Apply it.
Bash
kubectl apply -f k8s-labels.yaml.yaml``
kubectl apply -f k8s-labels.yaml.yaml3: Verify the βGlueβ (Endpoints):
Run this command to see the Service connecting to the Pods:
Bash
kubectl get endpoints web-service-glue``
kubectl get endpoints web-service-glueYou should see 4 IPs listed (1 manual pod + 3 deployment pods) because they all share the common labels app.kubernetes.io/name: my-web-app and tier: frontend.
4: Test the Canary Filter:
Bash
kubectl get endpoints canary-service-check``
kubectl get endpoints canary-service-checkYou should see only 3 IPs (Deployment pods only) because the manual pod has the wrong version.