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

Prerequisites

Flagger requires a Kubernetes cluster v1.19 or newer and NGINX ingress v0.46 or newer.
Install the NGINX ingress controller with Helm v3:
1
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
2
kubectl create ns ingress-nginx
3
helm upgrade -i ingress-nginx ingress-nginx/ingress-nginx \
4
--namespace ingress-nginx \
5
--set controller.metrics.enabled=true \
6
--set controller.podAnnotations."prometheus\.io/scrape"=true \
7
--set controller.podAnnotations."prometheus\.io/port"=10254
Copied!
Install Flagger and the Prometheus add-on in the same namespace as the ingress controller:
1
helm repo add flagger https://flagger.app
2
3
helm upgrade -i flagger flagger/flagger \
4
--namespace ingress-nginx \
5
--set prometheus.install=true \
6
--set meshProvider=nginx
Copied!

Bootstrap

Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployments, ClusterIP services and canary ingress). 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 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
helm upgrade -i flagger-loadtester flagger/loadtester \
2
--namespace=test
Copied!
Create an ingress definition (replace app.example.com with your own domain):
1
apiVersion: networking.k8s.io/v1
2
kind: Ingress
3
metadata:
4
name: podinfo
5
namespace: test
6
labels:
7
app: podinfo
8
annotations:
9
kubernetes.io/ingress.class: "nginx"
10
spec:
11
rules:
12
- host: "app.example.com"
13
http:
14
paths:
15
- pathType: Prefix
16
path: "/"
17
backend:
18
service:
19
name: podinfo
20
port:
21
number: 80
Copied!
Save the above resource as podinfo-ingress.yaml and then apply it:
1
kubectl apply -f ./podinfo-ingress.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
provider: nginx
8
# deployment reference
9
targetRef:
10
apiVersion: apps/v1
11
kind: Deployment
12
name: podinfo
13
# ingress reference
14
ingressRef:
15
apiVersion: networking.k8s.io/v1
16
kind: Ingress
17
name: podinfo
18
# HPA reference (optional)
19
autoscalerRef:
20
apiVersion: autoscaling/v2beta2
21
kind: HorizontalPodAutoscaler
22
name: podinfo
23
# the maximum time in seconds for the canary deployment
24
# to make progress before it is rollback (default 600s)
25
progressDeadlineSeconds: 60
26
service:
27
# ClusterIP port number
28
port: 80
29
# container port number or name
30
targetPort: 9898
31
analysis:
32
# schedule interval (default 60s)
33
interval: 10s
34
# max number of failed metric checks before rollback
35
threshold: 10
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
# NGINX 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
# testing (optional)
51
webhooks:
52
- name: acceptance-test
53
type: pre-rollout
54
url: http://flagger-loadtester.test/
55
timeout: 30s
56
metadata:
57
type: bash
58
cmd: "curl -sd 'test' http://podinfo-canary/token | grep token"
59
- name: load-test
60
url: http://flagger-loadtester.test/
61
timeout: 5s
62
metadata:
63
cmd: "hey -z 1m -q 10 -c 2 http://app.example.com/"
Copied!
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
ingresses.extensions/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
ingresses.extensions/podinfo-canary
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 or MS Teams.
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-06T14:05:07Z
5
prod frontend Succeeded 0 2019-05-05T16:15:07Z
6
prod backend Failed 0 2019-05-04T17:05:07Z
Copied!

Automated rollback

During the canary analysis you can generate HTTP 500 errors 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 http://app.example.com/status/500
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: latency
5
namespace: test
6
spec:
7
provider:
8
type: prometheus
9
address: http://flagger-prometheus.ingress-nginx:9090
10
query: |
11
histogram_quantile(0.99,
12
sum(
13
rate(
14
http_request_duration_seconds_bucket{
15
kubernetes_namespace="{{ namespace }}",
16
kubernetes_pod_name=~"{{ target }}-[0-9a-zA-Z]+(-[0-9a-zA-Z]+)"
17
}[1m]
18
)
19
) by (le)
20
)
Copied!
Edit the canary analysis and add the latency check:
1
analysis:
2
metrics:
3
- name: "latency"
4
templateRef:
5
name: latency
6
thresholdRange:
7
max: 0.5
8
interval: 1m
Copied!
The threshold is set to 500ms so if the average request duration in the last minute goes over half a second then the analysis will fail and the canary will not be promoted.
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 high response latency:
1
watch curl http://app.example.com/delay/2
Copied!
Watch Flagger logs:
1
kubectl -n nginx-ingress 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 latency 1.20 > 0.5
8
Halt podinfo.test advancement latency 1.45 > 0.5
9
Halt podinfo.test advancement latency 1.60 > 0.5
10
Halt podinfo.test advancement latency 1.69 > 0.5
11
Halt podinfo.test advancement latency 1.70 > 0.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: 10
4
iterations: 10
5
match:
6
# curl -H 'X-Canary: insider' http://app.example.com
7
- headers:
8
x-canary:
9
exact: "insider"
10
# curl -b 'canary=always' http://app.example.com
11
- headers:
12
cookie:
13
exact: "canary"
14
metrics:
15
- name: request-success-rate
16
thresholdRange:
17
min: 99
18
interval: 1m
19
webhooks:
20
- name: load-test
21
url: http://flagger-loadtester.test/
22
timeout: 5s
23
metadata:
24
cmd: "hey -z 1m -q 10 -c 2 -H 'Cookie: canary=always' http://app.example.com/"
Copied!
The above configuration will run an analysis for ten minutes targeting users that have a canary cookie set to always or those that call the service using the 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 testing:
1
kubectl -n test describe canary/podinfo
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!
The above procedure can be extended with custom metrics checks, webhooks, manual promotion approval and Slack or MS Teams notifications.
Last modified 4mo ago