This guide shows you how to use the Gloo Edge ingress controller and Flagger to automate canary releases and A/B testing.
Prerequisites
Flagger requires a Kubernetes cluster v1.16 or newer and Gloo Edge ingress 1.6.0 or newer.
This guide was written for Flagger version 1.6.0 or higher. Prior versions of Flagger used Gloo UpstreamGroups to handle canaries, but newer versions of Flagger use Gloo RouteTables to handle canaries as well as A/B testing.
Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployments, ClusterIP services, Gloo route tables and upstreams). These objects expose the application outside the cluster and drive the canary analysis and promotion.
Create a test namespace:
kubectlcreatenstest
Create a deployment and a horizontal pod autoscaler:
Save the above resource as podinfo-virtualservice.yaml and then apply it:
kubectlapply-f./podinfo-virtualservice.yaml
Create a canary custom resource (replace app.example.com with your own domain):
apiVersion:flagger.app/v1beta1kind:Canarymetadata:name:podinfonamespace:testspec:# upstreamRef (optional)# defines an upstream to copy the spec from when flagger generates new upstreams.# necessary to copy over TLS config, circuit breakers, etc. (anything nonstandard)# upstreamRef:# apiVersion: gloo.solo.io/v1# kind: Upstream# name: podinfo-upstream# namespace: gloo-systemprovider:gloo# deployment referencetargetRef:apiVersion:apps/v1kind:Deploymentname:podinfo# HPA reference (optional)autoscalerRef:apiVersion:autoscaling/v2kind:HorizontalPodAutoscalername:podinfoservice:# ClusterIP port numberport:9898# container port number or name (optional)targetPort:9898analysis:# schedule interval (default 60s)interval:10s# max number of failed metric checks before rollbackthreshold:5# max traffic percentage routed to canary# percentage (0-100)maxWeight:50# canary increment step# percentage (0-100)stepWeight:5# Gloo Prometheus checksmetrics: - name:request-success-rate# minimum req success rate (non 5xx responses)# percentage (0-100)thresholdRange:min:99interval:1m - name:request-duration# maximum req duration P99# millisecondsthresholdRange:max:500interval:30s# testing (optional)webhooks: - name:acceptance-testtype:pre-rollouturl:http://flagger-loadtester.test/timeout:10smetadata:type:bashcmd:"curl -sd 'test' http://podinfo-canary:9898/token | grep token" - name:load-testurl:http://flagger-loadtester.test/timeout:5smetadata:type:cmdcmd:"hey -z 2m -q 5 -c 2 -host app.example.com http://gateway-proxy.gloo-system"
Note: when using upstreamRef the following fields are copied over from the original upstream: Labels, SslConfig, CircuitBreakers, ConnectionConfig, UseHttp2, InitialStreamWindowSize
Save the above resource as podinfo-canary.yaml and then apply it:
kubectlapply-f./podinfo-canary.yaml
After a couple of seconds Flagger will create the canary objects:
Flagger implements a control loop that gradually shifts traffic to the canary while measuring key performance indicators like HTTP requests success rate, requests average duration and pod health. Based on analysis of the KPIs a canary is promoted or aborted, and the analysis result is published to Slack.
Trigger a canary deployment by updating the container image:
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.
kubectl -n test describe canary/podinfo
Status:
Canary Weight: 0
Failed Checks: 10
Phase: Failed
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Synced 3m flagger Starting canary deployment for podinfo.test
Normal Synced 3m flagger Advance podinfo.test canary weight 5
Normal Synced 3m flagger Advance podinfo.test canary weight 10
Normal Synced 3m flagger Advance podinfo.test canary weight 15
Normal Synced 3m flagger Halt podinfo.test advancement success rate 69.17% < 99%
Normal Synced 2m flagger Halt podinfo.test advancement success rate 61.39% < 99%
Normal Synced 2m flagger Halt podinfo.test advancement success rate 55.06% < 99%
Normal Synced 2m flagger Halt podinfo.test advancement success rate 47.00% < 99%
Normal Synced 2m flagger (combined from similar events): Halt podinfo.test advancement success rate 38.08% < 99%
Warning Synced 1m flagger Rolling back podinfo.test failed checks threshold reached 10
Warning Synced 1m flagger Canary failed! Scaling down podinfo.test
Custom metrics
The canary analysis can be extended with Prometheus queries.
The demo app is instrumented with Prometheus so you can create a custom check that will use the HTTP request duration histogram to validate the canary.
Create a metric template and apply it on the cluster:
The above configuration validates the canary by checking if the HTTP 404 req/sec percentage is below 5 percent of the total traffic. If the 404s rate reaches the 5% threshold, then the canary fails.
Trigger a canary deployment by updating the container image:
kubectl -n gloo-system logs deployment/flagger -f | jq .msg
Starting canary deployment for podinfo.test
Advance podinfo.test canary weight 5
Advance podinfo.test canary weight 10
Advance podinfo.test canary weight 15
Halt podinfo.test advancement 404s percentage 6.20 > 5
Halt podinfo.test advancement 404s percentage 6.45 > 5
Halt podinfo.test advancement 404s percentage 7.60 > 5
Halt podinfo.test advancement 404s percentage 8.69 > 5
Halt podinfo.test advancement 404s percentage 9.70 > 5
Rolling back podinfo.test failed checks threshold reached 5
Canary failed! Scaling down podinfo.test
If you have alerting configured, Flagger will send a notification with the reason why the canary failed.
A/B Testing
Besides weighted routing, Flagger can be configured to route traffic to the canary based on HTTP match conditions. In an A/B testing scenario, you'll be using HTTP headers or cookies to target a certain segment of your users. This is particularly useful for frontend applications that require session affinity.
Edit the canary analysis, remove the max/step weight and add the match conditions and iterations: