Flagger is a progressive delivery tool that automates the release process for applications running on Kubernetes. It reduces the risk of introducing a new software version in production by gradually shifting traffic to the new version while measuring metrics and running conformance tests.
Flagger implements several deployment strategies (Canary releases, A/B testing, Blue/Green mirroring) using a service mesh (App Mesh, Istio, Linkerd, Kuma, Open Service Mesh) or an ingress controller (Contour, Gloo, NGINX, Skipper, Traefik, APISIX) for traffic routing. For release analysis, Flagger can query Prometheus, InfluxDB, Datadog, New Relic, CloudWatch, Stackdriver or Graphite and for alerting it uses Slack, MS Teams, Discord and Rocket.
Flagger can be configured with Kubernetes custom resources and is compatible with any CI/CD solutions made for Kubernetes. Since Flagger is declarative and reacts to Kubernetes events, it can be used in GitOps pipelines together with tools like Flux, JenkinsX, Carvel, Argo, etc.
Traefik Canary Deployments
This guide shows you how to use the Traefik and Flagger to automate canary deployments.
Prerequisites
Flagger requires a Kubernetes cluster v1.16 or newer and Traefik v2.3 or newer.
Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployments, ClusterIP services and TraefikService). These objects expose the application outside the cluster and drive the canary analysis and promotion.
Create a test namespace:
kubectlcreatenstest
Create a deployment and a horizontal pod autoscaler:
Save the above resource as podinfo-ingressroute.yaml and then apply it:
kubectlapply-f./podinfo-ingressroute.yaml
Create a canary custom resource (replace app.example.com with your own domain):
apiVersion:flagger.app/v1beta1kind:Canarymetadata:name:podinfonamespace:testspec:provider:traefik# deployment referencetargetRef:apiVersion:apps/v1kind:Deploymentname:podinfo# HPA reference (optional)autoscalerRef:apiVersion:autoscaling/v2beta2kind:HorizontalPodAutoscalername:podinfo# the maximum time in seconds for the canary deployment# to make progress before it is rollback (default 600s)progressDeadlineSeconds:60service:# ClusterIP port numberport:80# container port number or nametargetPort:9898analysis:# schedule interval (default 60s)interval:10s# max number of failed metric checks before rollbackthreshold:10# max traffic percentage routed to canary# percentage (0-100)maxWeight:50# canary increment step# percentage (0-100)stepWeight:5# Traefik Prometheus checksmetrics: - name:request-success-rateinterval:1m# minimum req success rate (non 5xx responses)# percentage (0-100)thresholdRange:min:99 - name:request-durationinterval:1m# maximum req duration P99# millisecondsthresholdRange:max:500webhooks: - name:acceptance-testtype:pre-rollouturl:http://flagger-loadtester.test/timeout:10smetadata:type:bashcmd:"curl -sd 'test' http://podinfo-canary.test/token | grep token" - name:load-testtype:rollouturl:http://flagger-loadtester.test/timeout:5smetadata:type:cmdcmd:"hey -z 10m -q 10 -c 2 -host app.example.com http://traefik.traefik"logCmdOutput:"true"
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:
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 or MS Teams.
Trigger a canary deployment by updating the container image:
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.
The above configuration validates the canary 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 canary fails.
Trigger a canary deployment by updating the container image: