This guide shows you how to automate A/B testing with Istio and Flagger.
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.
Prerequisites
Flagger requires a Kubernetes cluster v1.16 or newer and Istio v1.0 or newer.
Install Istio with telemetry support and Prometheus:
Create a canary custom resource (replace example.com with your own domain):
apiVersion:flagger.app/v1beta1kind:Canarymetadata:name:podinfonamespace:testspec:# deployment referencetargetRef:apiVersion:apps/v1kind:Deploymentname:podinfo# the maximum time in seconds for the canary deployment# to make progress before it is rollback (default 600s)progressDeadlineSeconds:60# HPA reference (optional)autoscalerRef:apiVersion:autoscaling/v2kind:HorizontalPodAutoscalername:podinfoservice:# container portport:9898# Istio gateways (optional)gateways: - istio-system/public-gateway# Istio virtual service host names (optional)hosts: - app.example.com# Istio traffic policy (optional)trafficPolicy:tls:# use ISTIO_MUTUAL when mTLS is enabledmode:DISABLEanalysis:# schedule interval (default 60s)interval:1m# total number of iterationsiterations:10# max number of failed iterations before rollbackthreshold:2# canary match conditionmatch: - headers:user-agent:regex:".*Firefox.*" - headers:cookie:regex:"^(.*?;)?(type=insider)(;.*)?$"metrics: - 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# generate traffic during analysiswebhooks: - name:load-testurl:http://flagger-loadtester.test/timeout:15smetadata:cmd:"hey -z 1m -q 10 -c 2 -H 'Cookie: type=insider' http://podinfo.test:9898/"
Note that when using Istio 1.5 you have to replace the request-duration with a metric template.
The above configuration will run an analysis for ten minutes targeting Firefox users and those that have an insider cookie.
Save the above resource as podinfo-abtest.yaml and then apply it:
kubectlapply-f./podinfo-abtest.yaml
After a couple of seconds Flagger will create the canary objects:
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:
Failed Checks: 2
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 iteration 1/10
Normal Synced 3m flagger Advance podinfo.test canary iteration 2/10
Normal Synced 3m flagger Advance podinfo.test canary iteration 3/10
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%
Warning Synced 2m flagger Rolling back podinfo.test failed checks threshold reached 2
Warning Synced 1m flagger Canary failed! Scaling down podinfo.test