Using KEDA autoscaler with groundcover

groundcover can be used as a Promethues datasource for KEDA autoscaler. Any metric stored in groundcover can be queried and used to automatically make decisions about scaling your infrastructure or deployments.

In the following example we will be controlling the replica count of a Kubernetes deployment. This common example can be extended and used to query any type of metrics, and we suggest experimenting with it to fit your own use cases.

Preparing to query the groundcover Prometheus datasource

groundcover exposes APIs to query data stored in the platform from outside the UI. In our case we will be using the Prometheus endpoint: https://ds.groundcover.com/datasources/prometheus

Querying the groundcover datasources APIs requires setting up a dedicated apikey for the authentication. If you haven't done this already, please follow these steps before proceeding. We will be referring to this apikey below as ds-api-key.

Setting up KEDA authentication with groundcover

KEDA requires access to the ds-api-key so it can add it to the Prometheus requests. We will set up two Kubernetes object for this purpose - a Secret object and a ClusterTriggerAuthentication object.

Defining the Secret object

We will be using a simple static Secret object, but any type can be used, including external secrets. Make sure to replace the <ds-api-key> placeholder with the api key previously created.

apiVersion: v1
kind: Secret 
metadata:
  name: groundcover-ds-api-key-header
  namespace: keda
stringData:
  headerName: apikey
  headerValue: <ds-api-key> # API key generated for querying the datasources

Defining the ClusterTriggerAuthentication object

Now that we have the secret object defined, we need to tell KEDA how to use it. groundcover requires the ds-api-key to appear as an additional header in the requests to our datasources, and that's exactly what we will define below.

apiVersion: keda.sh/v1alpha1
kind: ClusterTriggerAuthentication
metadata:
  name: groundcover-trigger-auth
spec:
  secretTargetRef: # this is a cluster-scoped resource. Assumes ref is to a secret in the same namespace as keda controller
    - parameter: customAuthHeader
      name: groundcover-ds-api-key-header
      key: headerName
    - parameter: customAuthValue
      name: groundcover-ds-api-key-header
      key: headerValue

Defining the auto-scaled object

Now that we have set up KEDA to properly authenticate with groundcover, we can define our first auto-scaling behavior.

We will be defining a Prometheus trigger, querying the groundcover_kube_deployment_status_replicas_ready which indicates the amount of ready replicas for our deployment. Note that we apply the deployment="example" filter, to query the metric specifically for our example deployment.

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: example
  namespace: example
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: example
  minReplicaCount: 1
  maxReplicaCount: 3
  pollingInterval: 60
  triggers:
    - type: prometheus
      metadata:
        serverAddress: https://ds.groundcover.com/datasources/prometheus
        threshold: "0.5"
        query: groundcover_kube_deployment_status_replicas_ready{deployment="example"} 
        authModes: custom
      authenticationRef:
        name: groundcover-trigger-auth
        kind: ClusterTriggerAuthentication

Last updated