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

Prerequisites

Flagger requires a Kubernetes cluster v1.16 or newer and Istio v1.5 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

Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployments, ClusterIP services, Istio destination rules and virtual services). These objects expose the application inside the mesh and drive the canary analysis and promotion.
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
# service port number
22
port: 9898
23
# container port number or name (optional)
24
targetPort: 9898
25
# Istio gateways (optional)
26
gateways:
27
- public-gateway.istio-system.svc.cluster.local
28
# Istio virtual service host names (optional)
29
hosts:
30
- app.example.com
31
# Istio traffic policy (optional)
32
trafficPolicy:
33
tls:
34
# use ISTIO_MUTUAL when mTLS is enabled
35
mode: DISABLE
36
# Istio retry policy (optional)
37
retries:
38
attempts: 3
39
perTryTimeout: 1s
40
retryOn: "gateway-error,connect-failure,refused-stream"
41
analysis:
42
# schedule interval (default 60s)
43
interval: 1m
44
# max number of failed metric checks before rollback
45
threshold: 5
46
# max traffic percentage routed to canary
47
# percentage (0-100)
48
maxWeight: 50
49
# canary increment step
50
# percentage (0-100)
51
stepWeight: 10
52
metrics:
53
- name: request-success-rate
54
# minimum req success rate (non 5xx responses)
55
# percentage (0-100)
56
thresholdRange:
57
min: 99
58
interval: 1m
59
- name: request-duration
60
# maximum req duration P99
61
# milliseconds
62
thresholdRange:
63
max: 500
64
interval: 30s
65
# testing (optional)
66
webhooks:
67
- name: acceptance-test
68
type: pre-rollout
69
url: http://flagger-loadtester.test/
70
timeout: 30s
71
metadata:
72
type: bash
73
cmd: "curl -sd 'test' http://podinfo-canary:9898/token | grep token"
74
- name: load-test
75
url: http://flagger-loadtester.test/
76
timeout: 5s
77
metadata:
78
cmd: "hey -z 1m -q 10 -c 2 http://podinfo-canary.test:9898/"
Copied!
Note that when using Istio 1.4 you have to replace the request-duration with a metric template.
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 minute.
Flagger Canary Process
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/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.
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-01-16T14:05:07Z
5
prod frontend Succeeded 0 2019-01-15T16:15:07Z
6
prod backend Failed 0 2019-01-14T17:05:07Z
Copied!

Automated rollback

During the canary analysis you can generate HTTP 500 errors and high latency to test if Flagger pauses the rollout.
Trigger another canary deployment:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.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 curl http://podinfo-canary:9898/status/500
Copied!
Generate latency:
1
watch curl http://podinfo-canary: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
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!

Traffic mirroring

Flagger Canary Traffic Shadowing
For applications that perform read operations, Flagger can be configured to drive canary releases with traffic mirroring. Istio traffic mirroring will copy each incoming request, sending one request to the primary and one to the canary service. The response from the primary is sent back to the user and the response from the canary is discarded. Metrics are collected on both requests so that the deployment will only proceed if the canary metrics are within the threshold values.
Note that mirroring should be used for requests that are idempotent or capable of being processed twice (once by the primary and once by the canary).
You can enable mirroring by replacing stepWeight/maxWeight with iterations and by setting analysis.mirror to true:
1
apiVersion: flagger.app/v1beta1
2
kind: Canary
3
metadata:
4
name: podinfo
5
namespace: test
6
spec:
7
analysis:
8
# schedule interval
9
interval: 1m
10
# max number of failed metric checks before rollback
11
threshold: 5
12
# total number of iterations
13
iterations: 10
14
# enable traffic shadowing
15
mirror: true
16
# weight of the traffic mirrored to your canary (defaults to 100%)
17
mirrorWeight: 100
18
metrics:
19
- name: request-success-rate
20
thresholdRange:
21
min: 99
22
interval: 1m
23
- name: request-duration
24
thresholdRange:
25
max: 500
26
interval: 1m
27
webhooks:
28
- name: acceptance-test
29
type: pre-rollout
30
url: http://flagger-loadtester.test/
31
timeout: 30s
32
metadata:
33
type: bash
34
cmd: "curl -sd 'test' http://podinfo-canary:9898/token | grep token"
35
- name: load-test
36
url: http://flagger-loadtester.test/
37
timeout: 5s
38
metadata:
39
cmd: "hey -z 1m -q 10 -c 2 http://podinfo.test:9898/"
Copied!
With the above configuration, Flagger will run a canary release with the following steps:
  • detect new revision (deployment spec, secrets or configmaps changes)
  • scale from zero the canary deployment
  • wait for the HPA to set the canary minimum replicas
  • check canary pods health
  • run the acceptance tests
  • abort the canary release if tests fail
  • start the load tests
  • mirror 100% of the traffic from primary to canary
  • check request success rate and request duration every minute
  • abort the canary release if the metrics check failure threshold is reached
  • stop traffic mirroring after the number of iterations is reached
  • route live traffic to the canary pods
  • promote the canary (update the primary secrets, configmaps and deployment spec)
  • wait for the primary deployment rollout to finish
  • wait for the HPA to set the primary minimum replicas
  • check primary pods health
  • switch live traffic back to primary
  • scale to zero the canary
  • send notification with the canary analysis result
The above procedure can be extended with custom metrics checks, webhooks, manual promotion approval and Slack or MS Teams notifications.
Last modified 9mo ago