How to Implement Canary Releases in Kubernetes: A Step-by-Step Guide

Canary releases let you roll out new application versions to a small subset of users before a full deployment. Kubernetes simplifies this with native features like Deployments, Services, and scaling. This tutorial walks you through a practical replicas-based canary strategy—no extra tools required.

Start by creating two Deployments: one for your stable version (e.g., my-app-stable) and one for the canary (my-app-canary). Both must share the same app: my-app label in their spec.selector.matchLabels and spec.template.metadata.labels. The canary Deployment uses a different version tag (e.g., version: canary), while the stable uses version: stable.

Article illustration

Route Traffic with a Single Service

Create a Kubernetes Service that selects pods using only the shared app: my-app label. The Service will distribute requests to both Deployments based on the number of running replicas. For example, if stable runs 10 replicas and canary runs 1, about 9% of traffic hits the canary.

Gradually Increase Canary Replicas

  • Start with 1 canary replica (e.g., kubectl scale deployment my-app-canary --replicas=1).
  • Monitor metrics like error rates, latency, and user feedback.
  • Increase replicas stepwise (e.g., 2, 5, 10) to shift more traffic to the new version.
  • Update the stable Deployment only after the canary is fully validated.

Rollback and Cleanup

If issues arise, simply scale the canary back to 0 replicas (kubectl scale deployment my-app-canary --replicas=0). Once the stable version is updated, delete the canary Deployment to avoid confusion.

This replicas-based method is simple, cheap, and works for most microservices. For more advanced traffic splitting (e.g., by headers), consider using an Ingress controller like NGINX or a service mesh like Istio. Start small, monitor closely, and your canary releases will be smooth and safe.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *