Istio Canary Deployments
This guide shows you how to use Istio and Flagger to automate canary deployments.

Prerequisites
Flagger requires a Kubernetes cluster v1.16 or newer and Istio v1.5 or newer.
Install Istio with telemetry support and Prometheus:
Install Flagger in the istio-system namespace:
Create an ingress gateway to expose the demo app outside of the mesh:
Bootstrap
Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployments, ClusterIP services, Istio destination rules and virtual services). These objects expose the application inside the mesh and drive the canary analysis and promotion.
Create a test namespace with Istio sidecar injection enabled:
Create a deployment and a horizontal pod autoscaler:
Deploy the load testing service to generate traffic during the canary analysis:
Create a canary custom resource (replace example.com with your own domain):
Note that when using Istio 1.4 you have to replace the request-duration with a metric template.
Save the above resource as podinfo-canary.yaml and then apply it:
When the canary analysis starts, Flagger will call the pre-rollout webhooks before routing traffic to the canary. The canary analysis will run for five minutes while validating the HTTP metrics and rollout hooks every minute.

After a couple of seconds Flagger will create the canary objects:
Automated canary promotion
Trigger a canary deployment by updating the container image:
Flagger detects that the deployment revision changed and starts a new rollout:
Note that if you apply new changes to the deployment during the canary analysis, Flagger will restart the analysis.
A canary deployment is triggered by changes in any of the following objects:
Deployment PodSpec (container image, command, ports, env, resources, etc)
ConfigMaps mounted as volumes or mapped to environment variables
Secrets mounted as volumes or mapped to environment variables
You can monitor all canaries with:
Automated rollback
During the canary analysis you can generate HTTP 500 errors and high latency to test if Flagger pauses the rollout.
Trigger another canary deployment:
Exec into the load tester pod with:
Generate HTTP 500 errors:
Generate latency:
When the number of failed checks reaches the canary analysis threshold, the traffic is routed back to the primary, the canary is scaled to zero and the rollout is marked as failed.
Session Affinity
While Flagger can perform weighted routing and A/B testing individually, with Istio it can combine the two leading to a Canary release with session affinity. For more information you can read the deployment strategies docs.
Create a canary custom resource (replace app.example.com with your own domain):
Save the above resource as podinfo-canary-session-affinity.yaml and then apply it:
Trigger a canary deployment by updating the container image:
You can load app.example.com in your browser and refresh it until you see the requests being served by podinfo:6.0.1. All subsequent requests after that will be served by podinfo:6.0.1 and not podinfo:6.0.0 because of the session affinity configured by Flagger with Istio.
Traffic mirroring

For applications that perform read operations, Flagger can be configured to drive canary releases with traffic mirroring. Istio traffic mirroring will copy each incoming request, sending one request to the primary and one to the canary service. The response from the primary is sent back to the user and the response from the canary is discarded. Metrics are collected on both requests so that the deployment will only proceed if the canary metrics are within the threshold values.
Note that mirroring should be used for requests that are idempotent or capable of being processed twice (once by the primary and once by the canary).
You can enable mirroring by replacing stepWeight/maxWeight with iterations and by setting analysis.mirror to true:
With the above configuration, Flagger will run a canary release with the following steps:
detect new revision (deployment spec, secrets or configmaps changes)
scale from zero the canary deployment
wait for the HPA to set the canary minimum replicas
check canary pods health
run the acceptance tests
abort the canary release if tests fail
start the load tests
mirror 100% of the traffic from primary to canary
check request success rate and request duration every minute
abort the canary release if the metrics check failure threshold is reached
stop traffic mirroring after the number of iterations is reached
route live traffic to the canary pods
promote the canary (update the primary secrets, configmaps and deployment spec)
wait for the primary deployment rollout to finish
wait for the HPA to set the primary minimum replicas
check primary pods health
switch live traffic back to primary
scale to zero the canary
send notification with the canary analysis result
The above procedure can be extended with custom metrics checks, webhooks, manual promotion approval and Slack or MS Teams notifications.
Canary Deployments for TCP Services
Performing a Canary deployment on a TCP (non HTTP) service is nearly identical to an HTTP Canary. Besides updating your Gateway document to support the TCP routing, the only difference is you have to set the appProtocol field to TCP inside of the service section of your Canary document.
Example:
If the appProtocol equals TCP then Flagger will treat this as a Canary deployment for a TCP service. When it creates the VirtualService document it will add a TCP section to route requests between the primary and canary services. See Istio documentation for more information on this spec.
The resulting VirtualService will include a tcp section similar to what is shown below:
Once the Canary analysis begins, Flagger will be able to adjust the weights inside of this tcp section to advance the Canary deployment until it either runs into an error (and is halted) or it successfully reaches the end of the analysis and is Promoted.
It is also important to note that if you set appProtocol to anything other than TCP, for example if you set it to HTTP, it will perform the Canary and treat it as an HTTP service. The same remains true if you do not set appProtocol at all. It will ONLY treat a Canary as a TCP service if appProtocal equals TCP.
Last updated
Was this helpful?