Flagger
Search…
Skipper Canary Deployments
This guide shows you how to use the Skipper ingress controller and Flagger to automate canary deployments.
Flagger Skipper Ingress Controller

Prerequisites

Flagger requires a Kubernetes cluster v1.19 or newer and Skipper ingress v0.13 or newer.
Install Skipper ingress-controller using upstream definition.
Certain arguments are relevant:
1
- -enable-connection-metrics
2
- -histogram-metric-buckets=.01,1,10,100
3
- -kubernetes
4
- -kubernetes-in-cluster
5
- -kubernetes-path-mode=path-prefix
6
- -metrics-exp-decay-sample
7
- -metrics-flavour=prometheus
8
- -route-backend-metrics
9
- -route-backend-error-counters
10
- -route-response-metrics
11
- -serve-host-metrics
12
- -serve-route-metrics
13
- -whitelisted-healthcheck-cidr=0.0.0.0/0 # permit Kind source health checks
Copied!
Install Flagger using kustomize:
1
kustomize build https://github.com/fluxcd/flagger/kustomize/kubernetes | kubectl apply -f -
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: "skipper"
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: skipper
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
# Skipper Prometheus checks
43
metrics:
44
- name: request-success-rate
45
interval: 1m
46
# minimum req success rate (non 5xx responses)
47
# percentage (0-100)
48
thresholdRange:
49
min: 99
50
- name: request-duration
51
interval: 1m
52
# maximum req duration P99
53
# milliseconds
54
thresholdRange:
55
max: 500
56
webhooks:
57
- name: gate
58
type: confirm-rollout
59
url: http://flagger-loadtester.test/gate/approve
60
- name: acceptance-test
61
type: pre-rollout
62
url: http://flagger-loadtester.test/
63
timeout: 10s
64
metadata:
65
type: bash
66
cmd: "curl -sd 'test' http://podinfo-canary/token | grep token"
67
- name: load-test
68
type: rollout
69
url: http://flagger-loadtester.test/
70
timeout: 5s
71
metadata:
72
type: cmd
73
cmd: "hey -z 10m -q 10 -c 2 -host app.example.com http://skipper-ingress.kube-system"
74
logCmdOutput: "true"
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
ingress.networking.k8s.io/podinfo-ingress
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
ingress.networking.k8s.io/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:4.0.6
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
New revision detected! Scaling up podinfo.test
9
Waiting for podinfo.test rollout to finish: 0 of 1 updated replicas are available
10
Pre-rollout check acceptance-test passed
11
Advance podinfo.test canary weight 5
12
Advance podinfo.test canary weight 10
13
Advance podinfo.test canary weight 15
14
Advance podinfo.test canary weight 20
15
Advance podinfo.test canary weight 25
16
Advance podinfo.test canary weight 30
17
Advance podinfo.test canary weight 35
18
Advance podinfo.test canary weight 40
19
Advance podinfo.test canary weight 45
20
Advance podinfo.test canary weight 50
21
Copying podinfo.test template spec to podinfo-primary.test
22
Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
23
Routing all traffic to primary
24
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-2 Progressing 30 2020-08-14T12:32:12Z
5
test podinfo Succeeded 0 2020-08-14T11:23:88Z
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:4.0.6
Copied!
Exec into the load tester pod with:
1
kubectl -n test exec -it deploy/flagger-loadtester bash
Copied!
Generate HTTP 500 errors:
1
hey -z 1m -c 5 -q 5 http://app.example.com/status/500
Copied!
Generate latency:
1
watch -n 1 curl 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 flagger-system logs deploy/flagger -f | jq .msg
2
3
New revision detected! Scaling up podinfo.test
4
Canary deployment podinfo.test not ready: waiting for rollout to finish: 0 of 1 updated replicas are available
5
Starting canary analysis for podinfo.test
6
Pre-rollout check acceptance-test passed
7
Advance podinfo.test canary weight 5
8
Advance podinfo.test canary weight 10
9
Advance podinfo.test canary weight 15
10
Advance podinfo.test canary weight 20
11
Halt podinfo.test advancement success rate 53.42% < 99%
12
Halt podinfo.test advancement success rate 53.19% < 99%
13
Halt podinfo.test advancement success rate 48.05% < 99%
14
Rolling back podinfo.test failed checks threshold reached 3
15
Canary failed! Scaling down podinfo.test
Copied!

Custom metrics

The canary analysis can be extended with Prometheus queries.
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.flagger-system:9090
10
query: |
11
histogram_quantile(0.99,
12
sum(
13
rate(
14
skipper_serve_route_duration_seconds_bucket{
15
route=~"{{ printf "kube(ew)?_%s__%s_canary__.*__%s_canary(_[0-9]+)?" namespace ingress service }}",
16
le="+Inf"
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:4.0.6
Copied!
Generate high response latency:
1
watch curl http://app.example.com/delay/2
Copied!
Watch Flagger logs:
1
kubectl -n flagger-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 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.
Last modified 4mo ago