Flagger Basics: Canary Analysis and Deployment Strategies
A detailed summary of the basic features of Flagger (part of the Flux GitOps family and a delivery tool for operating Kubernetes) along with the Canary custom resource.
Introduction
Section titled “Introduction”Flagger is a tool for automating the release process of applications running on K8s. It gradually shifts traffic to the new version of an application, evaluates suitability based on metrics, and determines whether to promote so that a healthy application can be maintained in production.
For Traffic Routing, Flagger can use a Service Mesh (App Mesh, Istio, Linkerd) or an Ingress Controller (Contour, Gloo, NGINX, Skipper, Traefik) to implement various deployment strategies (Canary release, A/B testing, Blue/Green mirroring).
In addition, for analysis of release versions, it can query Prometheus, Datadog, New Relic, CloudWatch, and Graphite, and it provides Alert Providers that can raise alerts to Slack, MS Teams, Discord, Rocket, and more.
Flagger composes the release process using the following 3 Custom Resources.
- k8s api resources
NAME APIVERSION NAMESPACED KIND
alertproviders flagger.app/v1beta1 true AlertProvidercanaries flagger.app/v1beta1 true Canarymetrictemplates flagger.app/v1beta1 true MetricTemplateSuch custom resource configurations are managed declaratively, and because they operate by reacting to K8s events, they can be used together with GitOps Pipelines you already use, such as Flux, JenkinsX, Carvel, and ArgoCD.
- Flagger Overview Diagram
Source: Flagger GitHub README, The Flux authors, Apache-2.0
See the Flagger Official Docs. https://docs.flagger.app/
Features
Section titled “Features”Below is a summary of the features supported by each type of Service Mesh and Ingress Controller.
- Service Mesh
| Traffic mirroring, which is only available in Istio, is a method that copies request traffic and sends it to both the Blue and Green environments. It can be used as a way to test release versions using requests that are idempotent or safe to run twice.
| Deployment Strategy | App Mesh | Istio | Linkerd | SMI | Kubernetes CNI |
|---|---|---|---|---|---|
| Canary deployments (weighted traffic) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: |
| A/B testing (headers and cookies routing) | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_minus_sign: | :heavy_minus_sign: |
| Blue/Green deployments (traffic switch) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Blue/Green deployments (traffic mirroring) | :heavy_minus_sign: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_minus_sign: | :heavy_minus_sign: |
| Mertric Check | App Mesh | Istio | Linkerd | SMI | Kubernetes CNI |
|---|---|---|---|---|---|
| Request success rate check (L7 metric) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_minus_sign: |
| Request duration check (L7 metric) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_minus_sign: |
| Custom metric checks | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
- Ingress
| When not deployed in a Service Mesh, Flagger can handle traffic through Kubernetes L4 Networking. However, with the NGINX Ingress Controller, L7 metric-based request check tests and the like are not possible.
| Deployment Strategy | Contour | Gloo | NGINX | Skipper | Traefik |
|---|---|---|---|---|---|
| Canary deployments (weighted traffic) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| A/B testing (headers and cookies routing) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_minus_sign: |
| Blue/Green deployments (traffic switch) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Mertric Check | Contour | Gloo | NGINX | Skipper | Traefik |
|---|---|---|---|---|---|
| Request success rate check (L7 metric) | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_check_mark: | :heavy_check_mark: |
| Request duration check (L7 metric) | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign: | :heavy_check_mark: | :heavy_check_mark: |
| Custom metric checks | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
See the Flagger GitHub https://github.com/fluxcd/flagger/blob/main/README.md
Canary CRD
Section titled “Canary CRD”By creating Flagger’s Canary custom resource, you can define the deployment method for an application’s release version.
Below is a brief summary of the items defined when creating a Canary.
provider
Section titled “provider”Basically, you define the service mesh / ingress controller that fits your environment as the provider.
- kubernetes, istio, linkerd, appmesh, nginx, skipper, contour, gloo, supergloo, traefik
- SMI TrafficSplit: smi:v1alpha1, smi:v1alpha2, smi:v1alpha3
apiVersion: flagger.app/v1beta1kind: Canarymetadata: name: <name>spec: provider: appmesh:v1beta2
...target
Section titled “target”targetRef can be specified as a Deployment or a DaemonSet. If an HPA is configured, define it in autoscalerRef.
spec: targetRef: apiVersion: apps/v1 kind: Deployment name: <name> autoscalerRef: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler name: <name>When you create a Canary resource defined as above, one additional primary deployment and HPA are created, as shown below.
- deployment/<targetRef.name>-primary
- hpa/<autoscalerRef.name>-primary
Flagger manages the primary deployment created above as the stable release and routes all production traffic to the primary. The configmap and secret referenced by the deployment defined in target are likewise created as primary. When changes occur in the deployment, configmap, secret, etc. defined as target, Flagger performs a promotion to reflect the same changes to the primary, and during this process you can define canary analysis to determine whether promotion is possible.
service
Section titled “service”You must define a service to specify how the Canary’s target deployment is exposed within the cluster.
spec: service: name: <name> port: 9898 portName: http targetPort: 9898When you create a Canary resource defined as above, 3 types of services are created, as shown below.
<service.name>.<namespace>.svc.cluster.local:selector app=<name>-primary<service.name>-primary.<namespace>.svc.cluster.local:selector app=<name>-primary<service.name>-canary.<namespace>.svc.cluster.local:selector app=<name>
The default service / primary service route to the target primary pod. When canary analysis is in progress, traffic is forwarded to the canary service according to the weight, and that service routes traffic to the target pod created by the release version.
- Endpoints Example
NAME ENDPOINTSaccount 10.0.0.20:8180account-canary 10.0.0.21:8180account-primary 10.0.0.20:8180In addition, for Flagger deployed in a Service Mesh, it also creates the corresponding abstracted objects.
- App Mesh Example
In the case of Amazon App Mesh, the VirtualService receives the proxy and forwards traffic to the router based on weights, and the Virtual Router distributes it to the Virtual Nodes that abstract the actual K8s cluster local services.
NAME ARN AGEvirtualservice.appmesh.k8s.aws/account arn:aws:appmesh:ap-northeast-2:170247361816:mesh/global/virtualService/account.shop 26dvirtualservice.appmesh.k8s.aws/account-canary arn:aws:appmesh:ap-northeast-2:170247361816:mesh/global/virtualService/account-canary.shop 26d
NAME ARN AGEvirtualrouter.appmesh.k8s.aws/account arn:aws:appmesh:ap-northeast-2:170247361816:mesh/global/virtualRouter/account_shop 26dvirtualrouter.appmesh.k8s.aws/account-canary arn:aws:appmesh:ap-northeast-2:170247361816:mesh/global/virtualRouter/account-canary_shop 26d
NAME ARN AGEvirtualnode.appmesh.k8s.aws/account-canary arn:aws:appmesh:ap-northeast-2:170247361816:mesh/global/virtualNode/account-canary_shop 26dvirtualnode.appmesh.k8s.aws/account-primary arn:aws:appmesh:ap-northeast-2:170247361816:mesh/global/virtualNode/account-primary_shop 26dstatus
Section titled “status”Through the status of the created canary resource, you can check whether the release process is being performed normally, and conditions such as success / fail are managed in the status.
- Real-time query of canary weight adjustment status and promotion progress
$ kubectl get canary account -w
NAME STATUS WEIGHT LASTTRANSITIONTIMEaccount Succeeded 0 2021-07-27T03:27:18Zaccount Progressing 0 2021-07-27T05:14:48Zaccount Progressing 20 2021-07-27T05:15:49Zaccount Progressing 40 2021-07-27T05:16:18Zaccount Progressing 60 2021-07-27T05:16:48Zaccount Progressing 80 2021-07-27T05:17:18Zaccount Promoting 0 2021-07-27T05:17:48Zaccount Finalising 0 2021-07-27T05:18:18Zaccount Succeeded 0 2021-07-27T05:18:48Z- Example canary status of a Success Process
status: canaryWeight: 0 conditions: - lastTransitionTime: "2021-07-27T03:27:18Z" lastUpdateTime: "2021-07-27T03:27:18Z" message: Canary analysis completed successfully, promotion finished. reason: Succeeded status: "True" type: Promoted failedChecks: 1 iterations: 0 lastAppliedSpec: 6c67dd598f lastTransitionTime: "2021-07-27T03:27:18Z" phase: Succeeded trackedConfigs: configmap/account: f537fc97adad7769 configmap/xray-sampling-rules: b6fc9ea0629f67d0analysis
Section titled “analysis”Through analysis, you define the promotion-related settings for the application when deploying a release version. The deployment strategy is defined here.
The canary analysis defines:
- the type of deployment strategy
- the metrics used to validate the canary version
- the webhooks used for conformance testing, load testing and manual gating
- the alerting settings
analysis: # schedule interval (default 60s) interval: # max number of failed metric checks before rollback threshold: # max traffic percentage routed to canary # percentage (0-100) maxWeight: # canary increment step # percentage (0-100) stepWeight: # promotion increment step # percentage (0-100) stepWeightPromotion: # total number of iterations # used for A/B Testing and Blue/Green iterations: # canary match conditions # used for A/B Testing match: - # HTTP header # key performance indicators metrics: - # metric check # alerting alerts: - # alert provider # external checks webhooks: - # hookDeployment Strategy
Section titled “Deployment Strategy”Through Flagger’s canary analysis definition, you can consider various deployment strategies. This is a more detailed summary of cases where Canary Release and Blue/Green Deployment are executed by Flagger installed on a Service Mesh.
The Canary Analysis task is triggered by change events on the following objects.
- Changes to the Deployment PodSpec (container image, command, ports, env, resources, etc), or a rollout restart
- Changes to a ConfigMap mounted as a Volume or mapped as an environment variable
- Changes to a Secret mounted as a Volume
Canary Release
Section titled “Canary Release”Flagger gradually shifts traffic to the canary object while measuring HTTP request success rate, duration, etc. It periodically shifts traffic until the defined maximum traffic weight is reached or the check failure count reaches the threshold, and determines whether the release can be promoted.
Release through A/B Testing using HTTP headers, cookies, etc. is also possible. (Within the range of supported service meshes and ingress controllers.)
-
Example of Flagger’s Canary Release progression stages (Max Weight: 50)
Source: Flagger docs, Deployment Strategies, The Flux authors, Apache-2.0
-
Canary promotion stage example
- Scan canary deployment and detect a new Revision
- Check Primary and Canary deployment status
: If a Rolling Update is in progress, the release is halted
: If the Pod status is abnormal, the release is halted
[confirm-rollout webhook][pre-rollout webhook] - Increase Canary traffic according to the defined stepWeight (until maxWeight is reached, at the defined interval)
[rollout webhook] - (Optional) Execute the Canary HTTP Request defined in the Webhook and verify the result
: Check HTTP request success rate / duration, etc.; if below the threshold specified in the metric, the release is halted
: When using the rollout webhook, each time the weight increases, the canary status can be checked via the webhook call API
[confirm-promotion webhook] - Promote canary to primary : Copy ConfigMap and Secret to primary : Copy the Deployment spec template to primary
- Primary Deployment rolling update : If the Pod status is abnormal, the rolling update is halted
- Route all Traffic to primary
- Scale the Canary Release down to 0
- End the rollout state
[post-rollout webhook] - (Optional) Send the Canary analysis result alert via the AlertProvider
- Additional notes
- If you define skipAnalysis=true, canary analysis is not performed and the canary release version is promoted immediately. It also appears usable as a way to automatically restart a deployment by detecting configmap, secret changes, etc.
- The feature that copies and manages configmap and secretmap can be disabled when installing Flagger.
Blue/Green Deployment
Section titled “Blue/Green Deployment”By replacing the definition of stepWeight/maxWeight with iterations, you can easily achieve a Blue/Green deployment strategy. When not in a Service Mesh environment, Flagger supports Blue/Green deployments through Kubernetes default L4 Networking.
-
Example of Flagger Blue/Green Deployment stages
Source: Flagger docs, Deployment Strategies, The Flux authors, Apache-2.0
-
Blue/Green analysis definition example : With the configuration below, you can automatically run the canary pod’s conformance and load tests via webhook calls for 10 minutes, or run the tests manually. As with Canary Release, when a metric definition fails, the failure counter increases, and when it reaches the threshold criteria, the release is halted.
analysis: # schedule interval (default 60s) interval: 1m # total number of iterations iterations: 10 # max number of failed iterations before rollback threshold: 2-
Blue/Green rollout stage example
- Scan canary deployment and detect a new Revision
- Scale up the Canary Deployment (Green)
- Run tests against the Canary Pod : If the failure threshold is reached, the release is halted
- Route all traffic to the Canary
- Copy the Canary spec to primary and promote (Blue)
- Primary rollout
- Scale down the Canary
Since the primary is rolled out only after all analysis tasks are completed and traffic is routed to the canary (Green) environment, you can prevent requests from being lost.