Flagger
Search…
Kuma Canary Deployments
This guide shows you how to use Kuma and Flagger to automate canary deployments.
Flagger Kuma Canary

Prerequisites

Flagger requires a Kubernetes cluster v1.16 or newer and Kuma 1.3 or newer.
Install Kuma and Prometheus (part of Kuma Metrics):
1
kumactl install control-plane | kubectl apply -f -
2
kumactl install metrics | kubectl apply -f -
Copied!
Install Flagger in the kuma-system namespace:
1
kubectl apply -k github.com/fluxcd/flagger//kustomize/kuma
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 Kuma TrafficRoute). These objects expose the application inside the mesh and drive the canary analysis and promotion.
Create a test namespace and enable Kuma sidecar injection:
1
kubectl create ns test
2
kubectl annotate namespace test kuma.io/sidecar-injection=enabled
Copied!
Install 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 deployment and a horizontal pod autoscaler:
1
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/podinfo?ref=main
Copied!
Create a canary custom resource for the podinfo deployment:
1
apiVersion: flagger.app/v1beta1
2
kind: Canary
3
metadata:
4
name: podinfo
5
namespace: test
6
annotations:
7
kuma.io/mesh: default
8
spec:
9
targetRef:
10
apiVersion: apps/v1
11
kind: Deployment
12
name: podinfo
13
progressDeadlineSeconds: 60
14
service:
15
port: 9898
16
targetPort: 9898
17
apex:
18
annotations:
19
9898.service.kuma.io/protocol: "http"
20
canary:
21
annotations:
22
9898.service.kuma.io/protocol: "http"
23
primary:
24
annotations:
25
9898.service.kuma.io/protocol: "http"
26
analysis:
27
# schedule interval (default 60s)
28
interval: 30s
29
# max number of failed metric checks before rollback
30
threshold: 5
31
# max traffic percentage routed to canary
32
# percentage (0-100)
33
maxWeight: 50
34
# canary increment step
35
# percentage (0-100)
36
stepWeight: 5
37
metrics:
38
- name: request-success-rate
39
threshold: 99
40
interval: 1m
41
- name: request-duration
42
threshold: 500
43
interval: 30s
44
webhooks:
45
- name: acceptance-test
46
type: pre-rollout
47
url: http://flagger-loadtester.test/
48
timeout: 30s
49
metadata:
50
type: bash
51
cmd: "curl -sd 'test' http://podinfo-canary.test:9898/token | grep token"
52
- name: load-test
53
type: rollout
54
url: http://flagger-loadtester.test/
55
metadata:
56
cmd: "hey -z 2m -q 10 -c 2 http://podinfo-canary.test:9898/"
Copied!
Save the above resource as podinfo-canary.yaml and then apply it:
1
kubectl apply -f ./podinfo-canary.yaml
Copied!
When the canary analysis starts, Flagger will call the pre-rollout webhooks before routing traffic to the canary. The canary analysis will run for five minutes while validating the HTTP metrics and rollout hooks every half a minute.
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
trafficroutes.kuma.io/podinfo
Copied!
After the boostrap, the podinfo deployment will be scaled to zero and the traffic to podinfo.test will be routed to the primary pods. During the canary analysis, the podinfo-canary.test address can be used to target directly the canary pods.

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=ghcr.io/stefanprodan/podinfo:6.0.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
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
Waiting for podinfo.test rollout to finish: 1 of 2 updated replicas are available
17
Advance podinfo.test canary weight 30
18
Advance podinfo.test canary weight 35
19
Advance podinfo.test canary weight 40
20
Advance podinfo.test canary weight 45
21
Advance podinfo.test canary weight 50
22
Copying podinfo.test template spec to podinfo-primary.test
23
Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
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.
A canary deployment is triggered by changes in any of the following objects:
  • Deployment PodSpec (container image, command, ports, env, resources, etc)
  • ConfigMaps mounted as volumes or mapped to environment variables
  • Secrets mounted as volumes or mapped to environment variables
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-06-30T14:05:07Z
5
prod frontend Succeeded 0 2019-06-30T16:15:07Z
6
prod backend Failed 0 2019-06-30T17: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=ghcr.io/stefanprodan/podinfo:6.0.2
Copied!
Exec into the load tester pod with:
1
kubectl -n test exec -it flagger-loadtester-xx-xx sh
Copied!
Generate HTTP 500 errors:
1
watch -n 1 curl http://podinfo-canary.test:9898/status/500
Copied!
Generate latency:
1
watch -n 1 curl http://podinfo-canary.test:9898/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
Canary Weight: 0
5
Failed Checks: 10
6
Phase: Failed
7
Events:
8
Starting canary analysis for podinfo.test
9
Pre-rollout check acceptance-test passed
10
Advance podinfo.test canary weight 5
11
Advance podinfo.test canary weight 10
12
Advance podinfo.test canary weight 15
13
Halt podinfo.test advancement success rate 69.17% < 99%
14
Halt podinfo.test advancement success rate 61.39% < 99%
15
Halt podinfo.test advancement success rate 55.06% < 99%
16
Halt podinfo.test advancement request duration 1.20s > 0.5s
17
Halt podinfo.test advancement request duration 1.45s > 0.5s
18
Rolling back podinfo.test failed checks threshold reached 5
19
Canary failed! Scaling down podinfo.test
Copied!
The above procedures can be extended with custom metrics checks, webhooks, manual promotion approval and Slack or MS Teams notifications.