Flagger
Search…
Istio A/B Testing
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.
Flagger A/B Testing Stages

Prerequisites

Flagger requires a Kubernetes cluster v1.16 or newer and Istio v1.0 or newer.
Install Istio with telemetry support and Prometheus:
1
istioctl manifest install --set profile=default
2
3
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.8/samples/addons/prometheus.yaml
Copied!
Install Flagger in the istio-system namespace:
1
kubectl apply -k github.com/fluxcd/flagger//kustomize/istio
Copied!
Create an ingress gateway to expose the demo app outside of the mesh:
1
apiVersion: networking.istio.io/v1alpha3
2
kind: Gateway
3
metadata:
4
name: public-gateway
5
namespace: istio-system
6
spec:
7
selector:
8
istio: ingressgateway
9
servers:
10
- port:
11
number: 80
12
name: http
13
protocol: HTTP
14
hosts:
15
- "*"
Copied!

Bootstrap

Create a test namespace with Istio sidecar injection enabled:
1
kubectl create ns test
2
kubectl label namespace test istio-injection=enabled
Copied!
Create a deployment and a horizontal pod autoscaler:
1
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/podinfo?ref=main
Copied!
Deploy the load testing service to generate traffic during the canary analysis:
1
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/tester?ref=main
Copied!
Create a canary custom resource (replace example.com with your own domain):
1
apiVersion: flagger.app/v1beta1
2
kind: Canary
3
metadata:
4
name: podinfo
5
namespace: test
6
spec:
7
# deployment reference
8
targetRef:
9
apiVersion: apps/v1
10
kind: Deployment
11
name: podinfo
12
# the maximum time in seconds for the canary deployment
13
# to make progress before it is rollback (default 600s)
14
progressDeadlineSeconds: 60
15
# HPA reference (optional)
16
autoscalerRef:
17
apiVersion: autoscaling/v2beta2
18
kind: HorizontalPodAutoscaler
19
name: podinfo
20
service:
21
# container port
22
port: 9898
23
# Istio gateways (optional)
24
gateways:
25
- public-gateway.istio-system.svc.cluster.local
26
# Istio virtual service host names (optional)
27
hosts:
28
- app.example.com
29
# Istio traffic policy (optional)
30
trafficPolicy:
31
tls:
32
# use ISTIO_MUTUAL when mTLS is enabled
33
mode: DISABLE
34
analysis:
35
# schedule interval (default 60s)
36
interval: 1m
37
# total number of iterations
38
iterations: 10
39
# max number of failed iterations before rollback
40
threshold: 2
41
# canary match condition
42
match:
43
- headers:
44
user-agent:
45
regex: ".*Firefox.*"
46
- headers:
47
cookie:
48
regex: "^(.*?;)?(type=insider)(;.*)?quot;
49
metrics:
50
- name: request-success-rate
51
# minimum req success rate (non 5xx responses)
52
# percentage (0-100)
53
thresholdRange:
54
min: 99
55
interval: 1m
56
- name: request-duration
57
# maximum req duration P99
58
# milliseconds
59
thresholdRange:
60
max: 500
61
interval: 30s
62
# generate traffic during analysis
63
webhooks:
64
- name: load-test
65
url: http://flagger-loadtester.test/
66
timeout: 15s
67
metadata:
68
cmd: "hey -z 1m -q 10 -c 2 -H 'Cookie: type=insider' http://podinfo.test:9898/"
Copied!
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:
1
kubectl apply -f ./podinfo-abtest.yaml
Copied!
After a couple of seconds Flagger will create the canary objects:
1
# applied
2
deployment.apps/podinfo
3
horizontalpodautoscaler.autoscaling/podinfo
4
canary.flagger.app/podinfo
5
6
# generated
7
deployment.apps/podinfo-primary
8
horizontalpodautoscaler.autoscaling/podinfo-primary
9
service/podinfo
10
service/podinfo-canary
11
service/podinfo-primary
12
destinationrule.networking.istio.io/podinfo-canary
13
destinationrule.networking.istio.io/podinfo-primary
14
virtualservice.networking.istio.io/podinfo
Copied!

Automated canary promotion

Trigger a canary deployment by updating the container image:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.1
Copied!
Flagger detects that the deployment revision changed and starts a new rollout:
1
kubectl -n test describe canary/abtest
2
3
Status:
4
Failed Checks: 0
5
Phase: Succeeded
6
Events:
7
Type Reason Age From Message
8
---- ------ ---- ---- -------
9
Normal Synced 3m flagger New revision detected podinfo.test
10
Normal Synced 3m flagger Scaling up podinfo.test
11
Warning Synced 3m flagger Waiting for podinfo.test rollout to finish: 0 of 1 updated replicas are available
12
Normal Synced 3m flagger Advance podinfo.test canary iteration 1/10
13
Normal Synced 3m flagger Advance podinfo.test canary iteration 2/10
14
Normal Synced 3m flagger Advance podinfo.test canary iteration 3/10
15
Normal Synced 2m flagger Advance podinfo.test canary iteration 4/10
16
Normal Synced 2m flagger Advance podinfo.test canary iteration 5/10
17
Normal Synced 1m flagger Advance podinfo.test canary iteration 6/10
18
Normal Synced 1m flagger Advance podinfo.test canary iteration 7/10
19
Normal Synced 55s flagger Advance podinfo.test canary iteration 8/10
20
Normal Synced 45s flagger Advance podinfo.test canary iteration 9/10
21
Normal Synced 35s flagger Advance podinfo.test canary iteration 10/10
22
Normal Synced 25s flagger Copying podinfo.test template spec to podinfo-primary.test
23
Warning Synced 15s flagger Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
24
Normal Synced 5s flagger Promotion completed! Scaling down podinfo.test
Copied!
Note that if you apply new changes to the deployment during the canary analysis, Flagger will restart the analysis.
You can monitor all canaries with:
1
watch kubectl get canaries --all-namespaces
2
3
NAMESPACE NAME STATUS WEIGHT LASTTRANSITIONTIME
4
test podinfo Progressing 100 2019-03-16T14:05:07Z
5
prod frontend Succeeded 0 2019-03-15T16:15:07Z
6
prod backend Failed 0 2019-03-14T17:05:07Z
Copied!

Automated rollback

During the canary analysis you can generate HTTP 500 errors and high latency to test Flagger's rollback.
Generate HTTP 500 errors:
1
watch curl -b 'type=insider' http://app.example.com/status/500
Copied!
Generate latency:
1
watch curl -b 'type=insider' http://app.example.com/delay/1
Copied!
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.
1
kubectl -n test describe canary/podinfo
2
3
Status:
4
Failed Checks: 2
5
Phase: Failed
6
Events:
7
Type Reason Age From Message
8
---- ------ ---- ---- -------
9
Normal Synced 3m flagger Starting canary deployment for podinfo.test
10
Normal Synced 3m flagger Advance podinfo.test canary iteration 1/10
11
Normal Synced 3m flagger Advance podinfo.test canary iteration 2/10
12
Normal Synced 3m flagger Advance podinfo.test canary iteration 3/10
13
Normal Synced 3m flagger Halt podinfo.test advancement success rate 69.17% < 99%
14
Normal Synced 2m flagger Halt podinfo.test advancement success rate 61.39% < 99%
15
Warning Synced 2m flagger Rolling back podinfo.test failed checks threshold reached 2
16
Warning Synced 1m flagger Canary failed! Scaling down podinfo.test
Copied!
The above procedure can be extended with custom metrics checks, webhooks, manual promotion approval and Slack or MS Teams notifications.
Last modified 8mo ago