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.
Prerequisites
Flagger requires a Kubernetes cluster v1.16 or newer.
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:
kubectlcreatenstest
Create a deployment and a horizontal pod autoscaler:
apiVersion:flagger.app/v1beta1kind:Canarymetadata:name:podinfonamespace:testspec:# service mesh provider can be: kubernetes, istio, appmesh, nginx, glooprovider:kubernetes# deployment referencetargetRef:apiVersion:apps/v1kind:Deploymentname:podinfo# the maximum time in seconds for the canary deployment# to make progress before rollback (default 600s)progressDeadlineSeconds:60# HPA reference (optional)autoscalerRef:apiVersion:autoscaling/v2kind:HorizontalPodAutoscalername:podinfoservice:port:9898portDiscovery:trueanalysis:# schedule interval (default 60s)interval:30s# max number of failed checks before rollbackthreshold:2# number of checks to run before rollbackiterations:10# Prometheus checks based on # http_request_duration_seconds histogrammetrics: - name:request-success-rate# minimum req success rate (non 5xx responses)# percentage (0-100)thresholdRange:min:99interval:1m - name:request-duration# maximum req duration P99# millisecondsthresholdRange:max:500interval:30s# acceptance/load testing hookswebhooks: - name:smoke-testtype:pre-rollouturl:http://flagger-loadtester.test/timeout:15smetadata:type:bashcmd:"curl -sd 'anon' http://podinfo-canary.test:9898/token | grep token" - name:load-testurl:http://flagger-loadtester.test/timeout:5smetadata:type:cmdcmd:"hey -z 1m -q 10 -c 2 http://podinfo-canary.test:9898/"
The above configuration will run an analysis for five minutes.
Save the above resource as podinfo-canary.yaml and then apply it:
kubectlapply-f./podinfo-canary.yaml
After a couple of seconds Flagger will create the canary objects:
When the number of failed checks reaches the analysis threshold, the green version is scaled to zero and the rollout is marked as failed.
kubectl -n test describe canary/podinfo
Status:
Failed Checks: 2
Phase: Failed
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Synced 3m flagger New revision detected podinfo.test
Normal Synced 3m flagger Advance podinfo.test canary iteration 1/10
Normal Synced 3m flagger Advance podinfo.test canary iteration 2/10
Normal Synced 3m flagger Advance podinfo.test canary iteration 3/10
Normal Synced 3m flagger Halt podinfo.test advancement success rate 69.17% < 99%
Normal Synced 2m flagger Halt podinfo.test advancement success rate 61.39% < 99%
Warning Synced 2m flagger Rolling back podinfo.test failed checks threshold reached 2
Warning Synced 1m flagger Canary failed! Scaling down podinfo.test
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:
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:
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.