Flagger
Search…
Blue/Green Deployments
This guide shows you how to automate Blue/Green deployments with Flagger and Kubernetes.
For applications that are not deployed on a service mesh, Flagger can orchestrate Blue/Green style deployments with Kubernetes L4 networking. When using a service mesh blue/green can be used as specified here.
Flagger Blue/Green Stages

Prerequisites

Flagger requires a Kubernetes cluster v1.16 or newer.
Install Flagger and the Prometheus add-on:
1
helm repo add flagger https://flagger.app
2
3
helm upgrade -i flagger flagger/flagger \
4
--namespace flagger \
5
--set prometheus.install=true \
6
--set meshProvider=kubernetes
Copied!
If you already have a Prometheus instance running in your cluster, you can point Flagger to the ClusterIP service with:
1
helm upgrade -i flagger flagger/flagger \
2
--namespace flagger \
3
--set metricsServer=http://prometheus.monitoring:9090
Copied!
Optionally you can enable Slack notifications:
1
helm upgrade -i flagger flagger/flagger \
2
--reuse-values \
3
--namespace flagger \
4
--set slack.url=https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK \
5
--set slack.channel=general \
6
--set slack.user=flagger
Copied!

Bootstrap

Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployment and ClusterIP services). These objects expose the application inside the cluster and drive the canary analysis and Blue/Green 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 analysis:
1
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/tester?ref=main
Copied!
Create a canary custom resource:
1
apiVersion: flagger.app/v1beta1
2
kind: Canary
3
metadata:
4
name: podinfo
5
namespace: test
6
spec:
7
# service mesh provider can be: kubernetes, istio, appmesh, nginx, gloo
8
provider: kubernetes
9
# deployment reference
10
targetRef:
11
apiVersion: apps/v1
12
kind: Deployment
13
name: podinfo
14
# the maximum time in seconds for the canary deployment
15
# to make progress before rollback (default 600s)
16
progressDeadlineSeconds: 60
17
# HPA reference (optional)
18
autoscalerRef:
19
apiVersion: autoscaling/v2beta2
20
kind: HorizontalPodAutoscaler
21
name: podinfo
22
service:
23
port: 9898
24
portDiscovery: true
25
analysis:
26
# schedule interval (default 60s)
27
interval: 30s
28
# max number of failed checks before rollback
29
threshold: 2
30
# number of checks to run before rollback
31
iterations: 10
32
# Prometheus checks based on
33
# http_request_duration_seconds histogram
34
metrics:
35
- name: request-success-rate
36
# minimum req success rate (non 5xx responses)
37
# percentage (0-100)
38
thresholdRange:
39
min: 99
40
interval: 1m
41
- name: request-duration
42
# maximum req duration P99
43
# milliseconds
44
thresholdRange:
45
max: 500
46
interval: 30s
47
# acceptance/load testing hooks
48
webhooks:
49
- name: smoke-test
50
type: pre-rollout
51
url: http://flagger-loadtester.test/
52
timeout: 15s
53
metadata:
54
type: bash
55
cmd: "curl -sd 'anon' http://podinfo-canary.test:9898/token | grep token"
56
- name: load-test
57
url: http://flagger-loadtester.test/
58
timeout: 5s
59
metadata:
60
type: cmd
61
cmd: "hey -z 1m -q 10 -c 2 http://podinfo-canary.test:9898/"
Copied!
The above configuration will run an analysis for five minutes.
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
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
Copied!
Blue/Green scenario:
  • on bootstrap, Flagger will create three ClusterIP services (app-primary,app-canary, app)
    and a shadow deployment named app-primary that represents the blue version
  • when a new version is detected, Flagger would scale up the green version and run the conformance tests
    (the tests should target the app-canary ClusterIP service to reach the green version)
  • if the conformance tests are passing, Flagger would start the load tests and validate them with custom Prometheus queries
  • if the load test analysis is successful, Flagger will promote the new version to app-primary and scale down the green version

Automated Blue/Green promotion

Trigger a 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
Events:
4
5
New revision detected podinfo.test
6
Waiting for podinfo.test rollout to finish: 0 of 1 updated replicas are available
7
Pre-rollout check acceptance-test passed
8
Advance podinfo.test canary iteration 1/10
9
Advance podinfo.test canary iteration 2/10
10
Advance podinfo.test canary iteration 3/10
11
Advance podinfo.test canary iteration 4/10
12
Advance podinfo.test canary iteration 5/10
13
Advance podinfo.test canary iteration 6/10
14
Advance podinfo.test canary iteration 7/10
15
Advance podinfo.test canary iteration 8/10
16
Advance podinfo.test canary iteration 9/10
17
Advance podinfo.test canary iteration 10/10
18
Copying podinfo.test template spec to podinfo-primary.test
19
Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
20
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 100 2019-06-16T14:05:07Z
5
prod frontend Succeeded 0 2019-06-15T16:15:07Z
6
prod backend Failed 0 2019-06-14T17:05:07Z
Copied!

Automated rollback

During the analysis you can generate HTTP 500 errors and high latency to test Flagger's rollback.
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.test:9898/status/500
Copied!
Generate latency:
1
watch curl http://podinfo-canary.test:9898/delay/1
Copied!
When the number of failed checks reaches the analysis threshold, the green version is scaled to zero and the rollout is marked as failed.
1
kubectl -n test describe canary/podinfo
2
3
Status:
4
Failed Checks: 2
5
Phase: Failed
6
Events:
7
Type Reason Age From Message
8
---- ------ ---- ---- -------
9
Normal Synced 3m flagger New revision detected podinfo.test
10
Normal Synced 3m flagger Advance podinfo.test canary iteration 1/10
11
Normal Synced 3m flagger Advance podinfo.test canary iteration 2/10
12
Normal Synced 3m flagger Advance podinfo.test canary iteration 3/10
13
Normal Synced 3m flagger Halt podinfo.test advancement success rate 69.17% < 99%
14
Normal Synced 2m flagger Halt podinfo.test advancement success rate 61.39% < 99%
15
Warning Synced 2m flagger Rolling back podinfo.test failed checks threshold reached 2
16
Warning Synced 1m flagger Canary failed! Scaling down podinfo.test
Copied!

Custom metrics

The 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 (green version).
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.flagger: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 (green version) 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 rollout is rolled back.
Trigger a 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 http://podinfo-canary.test:9898/status/400
Copied!
Watch Flagger logs:
1
kubectl -n flagger logs deployment/flagger -f | jq .msg
2
3
New revision detected podinfo.test
4
Scaling up podinfo.test
5
Advance podinfo.test canary iteration 1/10
6
Halt podinfo.test advancement 404s percentage 6.20 > 5
7
Halt podinfo.test advancement 404s percentage 6.45 > 5
8
Rolling back podinfo.test failed checks threshold reached 2
9
Canary failed! Scaling down podinfo.test
Copied!
If you have alerting configured, Flagger will send a notification with the reason why the canary failed.

Conformance Testing with Helm

Flagger comes with a testing service that can run Helm tests when configured as a pre-rollout webhook.
Deploy the Helm test runner in the kube-system namespace using the tiller service account:
1
helm repo add flagger https://flagger.app
2
3
helm upgrade -i flagger-helmtester flagger/loadtester \
4
--namespace=kube-system \
5
--set serviceAccountName=tiller
Copied!
When deployed the Helm tester API will be available at http://flagger-helmtester.kube-system/.
Add a helm test pre-rollout hook to your chart:
1
analysis:
2
webhooks:
3
- name: "conformance testing"
4
type: pre-rollout
5
url: http://flagger-helmtester.kube-system/
6
timeout: 3m
7
metadata:
8
type: "helm"
9
cmd: "test {{ .Release.Name }} --cleanup"
Copied!
When the canary analysis starts, Flagger will call the pre-rollout webhooks. If the helm test fails, Flagger will retry until the analysis threshold is reached and the canary is rolled back.
For an in-depth look at the analysis process read the usage docs.
Last modified 9mo ago