Flagger
Search…
Gloo Canary Deployments
This guide shows you how to use the Gloo Edge ingress controller and Flagger to automate canary releases and A/B testing.
Flagger Gloo Ingress Controller

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.
Install Gloo with Helm v3:
1
helm repo add gloo https://storage.googleapis.com/solo-public-helm
2
kubectl create ns gloo-system
3
helm upgrade -i gloo gloo/gloo \
4
--namespace gloo-system
Copied!
Install Flagger and the Prometheus add-on in the same namespace as Gloo:
1
helm repo add flagger https://flagger.app
2
3
helm upgrade -i flagger flagger/flagger \
4
--namespace gloo-system \
5
--set prometheus.install=true \
6
--set meshProvider=gloo
Copied!

Bootstrap

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:
1
kubectl create ns test
Copied!
Create a deployment and a horizontal pod autoscaler:
1
kubectl -n test 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 -n test apply -k https://github.com/fluxcd/flagger//kustomize/tester?ref=main
Copied!
Create a virtual service definition that references a route table that will be generated by Flagger (replace app.example.com with your own domain):
1
apiVersion: gateway.solo.io/v1
2
kind: VirtualService
3
metadata:
4
name: podinfo
5
namespace: test
6
spec:
7
virtualHost:
8
domains:
9
- 'app.example.com'
10
routes:
11
- matchers:
12
- prefix: /
13
delegateAction:
14
ref:
15
name: podinfo
16
namespace: test
Copied!
Save the above resource as podinfo-virtualservice.yaml and then apply it:
1
kubectl apply -f ./podinfo-virtualservice.yaml
Copied!
Create a canary custom resource (replace app.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
# upstreamRef (optional)
8
# defines an upstream to copy the spec from when flagger generates new upstreams.
9
# necessary to copy over TLS config, circuit breakers, etc. (anything nonstandard)
10
# upstreamRef:
11
# apiVersion: gloo.solo.io/v1
12
# kind: Upstream
13
# name: podinfo-upstream
14
# namespace: gloo-system
15
provider: gloo
16
# deployment reference
17
targetRef:
18
apiVersion: apps/v1
19
kind: Deployment
20
name: podinfo
21
# HPA reference (optional)
22
autoscalerRef:
23
apiVersion: autoscaling/v2beta2
24
kind: HorizontalPodAutoscaler
25
name: podinfo
26
service:
27
# ClusterIP port number
28
port: 9898
29
# container port number or name (optional)
30
targetPort: 9898
31
analysis:
32
# schedule interval (default 60s)
33
interval: 10s
34
# max number of failed metric checks before rollback
35
threshold: 5
36
# max traffic percentage routed to canary
37
# percentage (0-100)
38
maxWeight: 50
39
# canary increment step
40
# percentage (0-100)
41
stepWeight: 5
42
# Gloo Prometheus checks
43
metrics:
44
- name: request-success-rate
45
# minimum req success rate (non 5xx responses)
46
# percentage (0-100)
47
thresholdRange:
48
min: 99
49
interval: 1m
50
- name: request-duration
51
# maximum req duration P99
52
# milliseconds
53
thresholdRange:
54
max: 500
55
interval: 30s
56
# testing (optional)
57
webhooks:
58
- name: acceptance-test
59
type: pre-rollout
60
url: http://flagger-loadtester.test/
61
timeout: 10s
62
metadata:
63
type: bash
64
cmd: "curl -sd 'test' http://podinfo-canary:9898/token | grep token"
65
- name: load-test
66
url: http://flagger-loadtester.test/
67
timeout: 5s
68
metadata:
69
type: cmd
70
cmd: "hey -z 2m -q 5 -c 2 -host app.example.com http://gateway-proxy.gloo-system"
Copied!
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:
1
kubectl apply -f ./podinfo-canary.yaml
Copied!
After a couple of seconds Flagger will create the canary objects:
1
# applied
2
deployment.apps/podinfo
3
horizontalpodautoscaler.autoscaling/podinfo
4
virtualservices.gateway.solo.io/podinfo
5
canary.flagger.app/podinfo
6
7
# generated
8
deployment.apps/podinfo-primary
9
horizontalpodautoscaler.autoscaling/podinfo-primary
10
service/podinfo
11
service/podinfo-canary
12
service/podinfo-primary
13
routetables.gateway.solo.io/podinfo
14
upstreams.gloo.solo.io/test-podinfo-canaryupstream-9898
15
upstreams.gloo.solo.io/test-podinfo-primaryupstream-9898
Copied!
When the bootstrap finishes Flagger will set the canary status to initialized:
1
kubectl -n test get canary podinfo
2
3
NAME STATUS WEIGHT LASTTRANSITIONTIME
4
podinfo Initialized 0 2019-05-17T08:09:51Z
Copied!

Automated canary promotion

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.
Flagger Canary Stages
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/podinfo
2
3
Status:
4
Canary Weight: 0
5
Failed Checks: 0
6
Phase: Succeeded
7
Events:
8
Type Reason Age From Message
9
---- ------ ---- ---- -------
10
Normal Synced 3m flagger New revision detected podinfo.test
11
Normal Synced 3m flagger Scaling up podinfo.test
12
Warning Synced 3m flagger Waiting for podinfo.test rollout to finish: 0 of 1 updated replicas are available
13
Normal Synced 3m flagger Advance podinfo.test canary weight 5
14
Normal Synced 3m flagger Advance podinfo.test canary weight 10
15
Normal Synced 3m flagger Advance podinfo.test canary weight 15
16
Normal Synced 2m flagger Advance podinfo.test canary weight 20
17
Normal Synced 2m flagger Advance podinfo.test canary weight 25
18
Normal Synced 1m flagger Advance podinfo.test canary weight 30
19
Normal Synced 1m flagger Advance podinfo.test canary weight 35
20
Normal Synced 55s flagger Advance podinfo.test canary weight 40
21
Normal Synced 45s flagger Advance podinfo.test canary weight 45
22
Normal Synced 35s flagger Advance podinfo.test canary weight 50
23
Normal Synced 25s flagger Copying podinfo.test template spec to podinfo-primary.test
24
Warning Synced 15s flagger Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
25
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 15 2019-05-17T14:05:07Z
5
prod frontend Succeeded 0 2019-05-17T16:15:07Z
6
prod backend Failed 0 2019-05-17T17:05:07Z
Copied!

Automated rollback

During the canary analysis you can generate HTTP 500 errors and high latency to test if Flagger pauses and rolls back the faulted version.
Trigger another canary deployment:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.2
Copied!
Generate HTTP 500 errors:
1
watch curl -H 'Host: app.example.com' http://gateway-proxy.gloo-system/status/500
Copied!
Generate high latency:
1
watch curl -H 'Host: app.example.com' http://gateway-proxy.gloo-system/delay/2
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
Canary Weight: 0
5
Failed Checks: 10
6
Phase: Failed
7
Events:
8
Type Reason Age From Message
9
---- ------ ---- ---- -------
10
Normal Synced 3m flagger Starting canary deployment for podinfo.test
11
Normal Synced 3m flagger Advance podinfo.test canary weight 5
12
Normal Synced 3m flagger Advance podinfo.test canary weight 10
13
Normal Synced 3m flagger Advance podinfo.test canary weight 15
14
Normal Synced 3m flagger Halt podinfo.test advancement success rate 69.17% < 99%
15
Normal Synced 2m flagger Halt podinfo.test advancement success rate 61.39% < 99%
16
Normal Synced 2m flagger Halt podinfo.test advancement success rate 55.06% < 99%
17
Normal Synced 2m flagger Halt podinfo.test advancement success rate 47.00% < 99%
18
Normal Synced 2m flagger (combined from similar events): Halt podinfo.test advancement success rate 38.08% < 99%
19
Warning Synced 1m flagger Rolling back podinfo.test failed checks threshold reached 10
20
Warning Synced 1m flagger Canary failed! Scaling down podinfo.test
Copied!

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:
1
apiVersion: flagger.app/v1beta1
2
kind: MetricTemplate
3
metadata:
4
name: not-found-percentage
5
namespace: test
6
spec:
7
provider:
8
type: prometheus
9
address: http://flagger-prometheus.gloo-system:9090
10
query: |
11
100 - sum(
12
rate(
13
http_request_duration_seconds_count{
14
kubernetes_namespace="{{ namespace }}",
15
kubernetes_pod_name=~"{{ target }}-[0-9a-zA-Z]+(-[0-9a-zA-Z]+)"
16
status!="{{ interval }}"
17
}[1m]
18
)
19
)
20
/
21
sum(
22
rate(
23
http_request_duration_seconds_count{
24
kubernetes_namespace="{{ namespace }}",
25
kubernetes_pod_name=~"{{ target }}-[0-9a-zA-Z]+(-[0-9a-zA-Z]+)"
26
}[{{ interval }}]
27
)
28
) * 100
Copied!
Edit the canary analysis and add the following metric:
1
analysis:
2
metrics:
3
- name: "404s percentage"
4
templateRef:
5
name: not-found-percentage
6
thresholdRange:
7
max: 5
8
interval: 1m
Copied!
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:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.3
Copied!
Generate 404s:
1
watch curl -H 'Host: app.example.com' http://gateway-proxy.gloo-system/status/404
Copied!
Watch Flagger logs:
1
kubectl -n gloo-system logs deployment/flagger -f | jq .msg
2
3
Starting canary deployment for podinfo.test
4
Advance podinfo.test canary weight 5
5
Advance podinfo.test canary weight 10
6
Advance podinfo.test canary weight 15
7
Halt podinfo.test advancement 404s percentage 6.20 > 5
8
Halt podinfo.test advancement 404s percentage 6.45 > 5
9
Halt podinfo.test advancement 404s percentage 7.60 > 5
10
Halt podinfo.test advancement 404s percentage 8.69 > 5
11
Halt podinfo.test advancement 404s percentage 9.70 > 5
12
Rolling back podinfo.test failed checks threshold reached 5
13
Canary failed! Scaling down podinfo.test
Copied!
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.
Flagger A/B Testing Stages
Edit the canary analysis, remove the max/step weight and add the match conditions and iterations:
1
analysis:
2
interval: 1m
3
threshold: 5
4
iterations: 10
5
match:
6
- headers:
7
x-canary:
8
exact: "insider"
9
webhooks:
10
- name: load-test
11
url: http://flagger-loadtester.test/
12
metadata:
13
cmd: "hey -z 1m -q 5 -c 5 -H 'X-Canary: insider' -host app.example.com http://gateway-proxy.gloo-system"
Copied!
The above configuration will run an analysis for ten minutes targeting users that have a X-Canary: insider header.
Trigger a canary deployment by updating the container image:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.4
Copied!
Flagger detects that the deployment revision changed and starts the A/B test:
1
kubectl -n gloo-system logs deploy/flagger -f | jq .msg
2
3
New revision detected! Progressing canary analysis for podinfo.test
4
Advance podinfo.test canary iteration 1/10
5
Advance podinfo.test canary iteration 2/10
6
Advance podinfo.test canary iteration 3/10
7
Advance podinfo.test canary iteration 4/10
8
Advance podinfo.test canary iteration 5/10
9
Advance podinfo.test canary iteration 6/10
10
Advance podinfo.test canary iteration 7/10
11
Advance podinfo.test canary iteration 8/10
12
Advance podinfo.test canary iteration 9/10
13
Advance podinfo.test canary iteration 10/10
14
Copying podinfo.test template spec to podinfo-primary.test
15
Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
16
Routing all traffic to primary
17
Promotion completed! Scaling down podinfo.test
Copied!
The web browser user agent header allows user segmentation based on device or OS.
For example, if you want to route all mobile users to the canary instance:
1
match:
2
- headers:
3
user-agent:
4
regex: ".*Mobile.*"
Copied!
Or if you want to target only Android users:
1
match:
2
- headers:
3
user-agent:
4
regex: ".*Android.*"
Copied!
Or a specific browser version:
1
match:
2
- headers:
3
user-agent:
4
regex: ".*Firefox.*"
Copied!
For an in-depth look at the analysis process read the usage docs.
Last modified 5mo ago