This guide shows you how to use Flagger with KEDA ScaledObjects to autoscale workloads during a Canary analysis run. We will be using a Blue/Green deployment strategy with the Kubernetes provider for the sake of this tutorial, but you can use any deployment strategy combined with any supported provider.
Prerequisites
Flagger requires a Kubernetes cluster v1.16 or newer. For this tutorial, we'll need KEDA 2.7.1 or newer.
Flagger takes a Kubernetes deployment and a KEDA ScaledObject targeting the deployment. It then creates a series of objects (Kubernetes deployments, ClusterIP services and another KEDA ScaledObject targeting the created Deployment). These objects expose the application inside the mesh and drive the Canary analysis and Blue/Green promotion.
Create a canary custom resource for the podinfo deployment:
apiVersion:flagger.app/v1beta1kind:Canarymetadata:name:podinfonamespace:testspec:provider:kubernetes# deployment referencetargetRef:apiVersion:apps/v1kind:Deploymentname:podinfo# Scaler referenceautoscalerRef:apiVersion:keda.sh/v1alpha1kind:ScaledObject# ScaledObject targeting the canary deploymentname:podinfo-so# Mapping between trigger names and the related query to use for the generated # ScaledObject targeting the primary deployment. (Optional)primaryScalerQueries:prom-trigger:sum(rate(http_requests_total{ app="podinfo-primary" }[30s]))# Overriding replica scaling configuration for the generated ScaledObject# targeting the primary deployment. (Optional)primaryScalerReplicas:minReplicas:2maxReplicas:5# the maximum time in seconds for the canary deployment# to make progress before rollback (default 600s)progressDeadlineSeconds:60service:port:80targetPort:9898name:podinfo-svcportDiscovery:trueanalysis:# schedule interval (default 60s)interval:15s# max number of failed checks before rollbackthreshold:5# number of checks to run before promotioniterations:5# Prometheus checks based on # http_request_duration_seconds histogrammetrics: - name:request-success-rateinterval:1mthresholdRange:min:99 - name:request-durationinterval:30sthresholdRange:max:500# load testing hookswebhooks: - name:load-testurl:http://flagger-loadtester.test/timeout:5smetadata:type:cmdcmd:"hey -z 2m -q 20 -c 2 http://podinfo-svc-canary.test/"
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:
We refer to our ScaledObject for the canary deployment using .spec.autoscalerRef. Flagger will use this to generate a ScaledObject which will scale the primary deployment. By default, Flagger will try to guess the query to use for the primary ScaledObject, by replacing all mentions of .spec.targetRef.Name and {.spec.targetRef.Name}-canary with {.spec.targetRef.Name}-primary, for all triggers. For eg, if your ScaledObject has a trigger query defined as: sum(rate(http_requests_total{ app="podinfo" }[30s])) or sum(rate(http_requests_total{ app="podinfo-primary" }[30s])), then the primary ScaledObject will have the same trigger with a query defined as sum(rate(http_requests_total{ app="podinfo-primary" }[30s])).
If, the generated query does not meet your requirements, you can specify the query for autoscaling the primary deployment explicitly using .spec.autoscalerRef.primaryScalerQueries, which lets you define a query for each trigger. Please note that, your ScaledObject's .spec.triggers[@].name must not be blank, as Flagger needs that to identify each trigger uniquely.
In the situation when it is desired to have different scaling replica configuration between the canary and primary deployment ScaledObject you can use the .spec.autoscalerRef.primaryScalerReplicas to override these values for the generated primary ScaledObject.
After the boostrap, the podinfo deployment will be scaled to zero and the traffic to podinfo.test will be routed to the primary pods. To keep the podinfo deployment at 0 replicas and pause auto scaling, Flagger will add an annotation to your ScaledObject: autoscaling.keda.sh/paused-replicas: 0. During the canary analysis, the annotation is removed, to enable auto scaling for the podinfo deployment. The podinfo-canary.test address can be used to target directly the canary pods. When the canary analysis starts, Flagger will call the pre-rollout webhooks before routing traffic to the canary. The Blue/Green deployment will run for five iterations while validating the HTTP metrics and rollout hooks every 15 seconds.
Automated Blue/Green promotion
Trigger a deployment by updating the container image: