Back to Blog
Lesson 38 of the Kubernetes: Kubernetes Concepts & Your First Pod course
KubernetesAugust 26, 20263 min read

Advanced Labeling Strategies: Canary Deployments in Kubernetes

Learn how to move beyond basic organization with advanced labeling strategies in Kubernetes. Master multi-label filtering and implement canary deployments.

KubernetesLabelsCanaryDevOpsInfrastructureNetworking
Close-up of stacked blue plastic crates with a quality control label.

Previously in this course, we explored introduction-to-labels-and-selectors-in-kubernetes to identify and group simple resources. In this lesson, we will leverage that foundation to solve real-world operational problems, specifically how to use complex labels for multi-criteria filtering and orchestrating canary deployments.

Why Advanced Labeling?

In production environments, a single label like app: web-server is rarely enough. You need to track the lifecycle of your application, distinguish between environment tiers, and manage traffic flow during updates. Advanced labeling is the backbone of deployments-declarative-updates-for-kubernetes-applications and the-role-of-services-kubernetes-networking-explained because it allows the control plane to decouple the definition of a workload from the routing of traffic.

Multi-Label Filtering

Kubernetes labels function as key-value pairs that support set-based requirements. When you use kubectl or write service selectors, you can combine multiple labels to target a specific subset of pods.

Consider a cluster running multiple versions and environments. If you want to target only the production version of your API, you use a selector that requires both app: api and env: prod.

Worked Example: Multi-Criteria Selection

Imagine we have several pods. We want to select only the production backend pods.

YAML
# To list these pods, use the -l flag with comma separation
kubectl get pods -l app=backend,env=prod

If you need to exclude a specific version, you can use the != operator:

Bash
# Select all backend pods that are NOT in the 'canary' release
kubectl get pods -l app=backend,version!=canary

Canary Deployment Strategy

A canary deployment is a pattern where you roll out a new version of your application to a small subset of users before upgrading the entire fleet. Labels make this possible by allowing your Service to "split" traffic.

By labeling your stable pods with version: stable and your new pods with version: canary, you can create two separate Services (or manipulate one) to control traffic flow.

Implementation Steps:

  1. Stable State: Your main Service points to app: web, version: stable.
  2. Deploy Canary: Deploy new pods with app: web, version: canary.
  3. Route Traffic: Update a second "Canary" Service to select app: web, version: canary.
YAML
# canary-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: web-canary
spec:
  selector:
    app: web
    version: canary  # This ensures only the new code receives this traffic
  ports:
    - port: 80

Common Pitfalls

  • Selector Mismatch: The most common error is when a Service's label selector doesn't match any Pod labels. The Service will remain "silent," showing no endpoints. Always use kubectl describe svc <service-name> to verify that it found matching pods.
  • Over-Labeling: Don't add labels for every piece of metadata. Labels are meant for selection and grouping. Use Annotations for descriptive metadata (like contact emails or build hashes) that you don't intend to query frequently.
  • Case Sensitivity: Label keys and values are case-sensitive. Version: stable is different from version: stable.

Hands-on Exercise: The "Canary" Test

  1. Create two Pods with label app: demo, but give one version: v1 and the other version: v2.
  2. Use kubectl get pods -l app=demo,version=v1 to verify you can isolate the first version.
  3. Apply a Service that targets only version: v2.
  4. Confirm the Service endpoints via kubectl get endpoints.

Frequently Asked Questions

Q: Can I change a label on a running pod? Yes, use kubectl label pod <pod-name> key=value --overwrite. Note that this will immediately change which Services include or exclude that pod.

Q: Should I use labels for security? No. Labels are metadata and can be changed by anyone with edit access. Use NetworkPolicies for security-based traffic control.

Q: How many labels should a pod have? There is no hard limit, but keep it readable. Stick to standard labels like app, env, version, and tier.

Recap

We’ve moved beyond simple identification. By using multi-label filtering, you can precisely control which resources are managed by your services. By applying these labels to a canary deployment strategy, you now have the tools to ship code to production with significantly lower risk.

Up next: Introduction to Ingress

Similar Posts