Automatically scaling pods based on custom metrics

As a developer, you can use the custom metrics autoscaler to specify how OKD should automatically increase or decrease the number of pods for a deployment, stateful set, custom resource, or job based on custom metrics that are not based only on CPU or memory.

The Custom Metrics Autoscaler Operator for Red Hat OpenShift is an optional operator, based on the Kubernetes Event Driven Autoscaler (KEDA), that allows workloads to be scaled using additional metrics sources other than pod metrics.

The custom metrics autoscaler currently supports only the Prometheus, CPU, memory, and Apache Kafka metrics.

The custom metrics autoscaler is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.

For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.

Custom Metrics Autoscaler Operator release notes

The release notes for the Custom Metrics Autoscaler Operator for Red Hat Openshift describe new features and enhancements, deprecated features, and known issues.

The Custom Metrics Autoscaler Operator uses the Kubernetes-based Event Driven Autoscaler (KEDA) and is built on top of the OKD horizontal pod autoscaler (HPA).

The Custom Metrics Autoscaler Operator for Red Hat OpenShift is provided as an installable component, with a distinct release cycle from the core OKD. The Red Hat OpenShift Container Platform Life Cycle Policy outlines release compatibility.

Supported versions

The following table defines the Custom Metrics Autoscaler Operator versions for each OKD version.

VersionOKD versionGeneral availability

2.8.2-174

4.12

Technology Preview

2.8.2-174

4.11

Technology Preview

2.8.2-174

4.10

Technology Preview

Custom Metrics Autoscaler Operator 2.8.2-174 release notes

This release of the Custom Metrics Autoscaler Operator 2.8.2-174 provides new features and bug fixes for running the Operator in an OKD cluster. The components of the Custom Metrics Autoscaler Operator 2.8.2-174 were released in RHEA-2023:1683.

The Custom Metrics Autoscaler Operator is currently a Technology Preview feature.

New features and enhancements

Operator upgrade support

You can now upgrade from a prior version of the Custom Metrics Autoscaler Operator. See “Changing the update channel for an Operator” in the “Additional resources” for information on upgrading an Operator.

must-gather support

You can now collect data about the Custom Metrics Autoscaler Operator and its components by using the OKD must-gather tool. Currently, the process for using the must-gather tool with the Custom Metrics Autoscaler is different than for other operators. See “Gathering debugging data in the “Additional resources” for more information.

Custom Metrics Autoscaler Operator 2.8.2 release notes

This release of the Custom Metrics Autoscaler Operator 2.8.2 provides new features and bug fixes for running the Operator in an OKD cluster. The components of the Custom Metrics Autoscaler Operator 2.8.2 were released in RHSA-2023:1042.

The Custom Metrics Autoscaler Operator is currently a Technology Preview feature.

New features and enhancements

Audit Logging

You can now gather and view audit logs for the Custom Metrics Autoscaler Operator and its associated components. Audit logs are security-relevant chronological sets of records that document the sequence of activities that have affected the system by individual users, administrators, or other components of the system.

Scale applications based on Apache Kafka metrics

You can now use the KEDA Apache kafka trigger/scaler to scale deployments based on an Apache Kafka topic.

Autoscaling based on Apache Kafka metrics is a Technology Preview (TP) feature in all Custom Metrics Autoscaler TP releases and the Custom Metrics Autoscaler General Availability release.

Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production.

Scale applications based on CPU metrics

You can now use the KEDA CPU trigger/scaler to scale deployments based on CPU metrics.

Scale applications based on memory metrics

You can now use the KEDA memory trigger/scaler to scale deployments based on memory metrics.

Additional resources

Understanding the custom metrics autoscaler

The Custom Metrics Autoscaler Operator scales your pods up and down based on custom, external metrics from specific applications. Your other applications continue to use other scaling methods. You configure triggers, also known as scalers, which are the source of events and metrics that the custom metrics autoscaler uses to determine how to scale. The custom metrics autoscaler uses a metrics API to convert the external metrics to a form that OKD can use. The custom metrics autoscaler creates a horizontal pod autoscaler (HPA) that performs the actual scaling.

To use the custom metrics autoscaler, you create a ScaledObject or ScaledJob object, which is a custom resource (CR) that defines the scaling metadata. You specify the deployment or job to scale, the source of the metrics to scale on (trigger), and other parameters such as the minimum and maximum replica counts allowed.

You can create only one scaled object or scaled job for each workload that you want to scale. Also, you cannot use a scaled object or scaled job and the horizontal pod autoscaler (HPA) on the same workload.

The custom metrics autoscaler, unlike the HPA, can scale to zero. If you set the minReplicaCount value in the custom metrics autoscaler CR to 0, the custom metrics autoscaler scales the workload down from 1 to 0 replicas to or up from 0 replicas to 1. This is known as the activation phase. After scaling up to 1 replica, the HPA takes control of the scaling. This is known as the scaling phase.

Some triggers allow you to change the number of replicas that are scaled by the cluster metrics autoscaler. In all cases, the parameter to configure the activation phase always uses the same phrase, prefixed with activation. For example, if the threshold parameter configures scaling, activationThreshold would configure activation. Configuring the activation and scaling phases allows you more flexibility with your scaling policies. For example, you could configure a higher activation phase to prevent scaling up or down if the metric is particularly low.

The activation value has more priority than the scaling value in case of different decisions for each. For example, if the threshold is set to 10, and the activationThreshold is 50, if the metric reports 40, the scaler is not active and the pods are scaled to zero even if the HPA requires 4 instances.

You can verify that the autoscaling has taken place by reviewing the number of pods in your custom resource or by reviewing the Custom Metrics Autoscaler Operator logs for messages similar to the following:

  1. Successfully set ScaleTarget replica count
  1. Successfully updated ScaleTarget

You can temporarily pause the autoscaling of a workload object, if needed. For example, you could pause autoscaling before performing cluster maintenance.

Installing the custom metrics autoscaler

You can use the OKD web console to install the Custom Metrics Autoscaler Operator.

The installation creates five CRDs:

  • ClusterTriggerAuthentication

  • KedaController

  • ScaledJob

  • ScaledObject

  • TriggerAuthentication

Prerequisites

  • Ensure that you have downloaded the pull secret from the Red Hat OpenShift Cluster Manager as shown in Obtaining the installation program in the installation documentation for your platform.

    If you have the pull secret, add the redhat-operators catalog to the OperatorHub custom resource (CR) as shown in Configuring OKD to use Red Hat Operators.

  • If you use the community KEDA:

    • Uninstall the community KEDA. You cannot run both KEDA and the custom metrics autoscaler on the same OKD cluster.

    • Remove the KEDA 1.x custom resource definitions by running the following commands:

      1. $ oc delete crd scaledobjects.keda.k8s.io
      1. $ oc delete crd triggerauthentications.keda.k8s.io

Procedure

  1. In the OKD web console, click OperatorsOperatorHub.

  2. Choose Custom Metrics Autoscaler from the list of available Operators, and click Install.

  3. On the Install Operator page, ensure that the All namespaces on the cluster (default) option is selected for Installation Mode. This installs the Operator in all namespaces.

  4. Ensure that the openshift-keda namespace is selected for Installed Namespace. OKD creates the namespace, if not present in your cluster.

  5. Click Install.

  6. Verify the installation by listing the Custom Metrics Autoscaler Operator components:

    1. Navigate to WorkloadsPods.

    2. Select the openshift-keda project from the drop-down menu and verify that the custom-metrics-autoscaler-operator-* pod is running.

    3. Navigate to WorkloadsDeployments to verify that the custom-metrics-autoscaler-operator deployment is running.

  7. Optional: Verify the installation in the OpenShift CLI using the following commands:

    1. $ oc get all -n openshift-keda

    The output appears similar to the following:

    Example output

    1. NAME READY STATUS RESTARTS AGE
    2. pod/custom-metrics-autoscaler-operator-5fd8d9ffd8-xt4xp 1/1 Running 0 18m
    3. NAME READY UP-TO-DATE AVAILABLE AGE
    4. deployment.apps/custom-metrics-autoscaler-operator 1/1 1 1 18m
    5. NAME DESIRED CURRENT READY AGE
    6. replicaset.apps/custom-metrics-autoscaler-operator-5fd8d9ffd8 1 1 1 18m
  8. Install the KedaController custom resource, which creates the required CRDs:

    1. In the OKD web console, click OperatorsInstalled Operators.

    2. Click Custom Metrics Autoscaler.

    3. On the Operator Details page, click the KedaController tab.

    4. On the KedaController tab, click Create KedaController and edit the file.

      1. kind: KedaController
      2. apiVersion: keda.sh/v1alpha1
      3. metadata:
      4. name: keda
      5. namespace: openshift-keda
      6. spec:
      7. watchNamespace: '' (1)
      8. operator:
      9. logLevel: info (2)
      10. logEncoder: console (3)
      11. metricsServer:
      12. logLevel: '0' (4)
      13. auditConfig: (5)
      14. logFormat: "json"
      15. logOutputVolumeClaim: "persistentVolumeClaimName"
      16. policy:
      17. rules:
      18. - level: Metadata
      19. omitStages: "RequestReceived"
      20. omitManagedFields: false
      21. lifetime:
      22. maxAge: "2"
      23. maxBackup: "1"
      24. maxSize: "50"
      25. serviceAccount: {}
      1Specifies the namespaces that the custom autoscaler should watch. Enter names in a comma-separated list. Omit or set empty to watch all namespaces. The default is empty.
      2Specifies the level of verbosity for the Custom Metrics Autoscaler Operator log messages. The allowed values are debug, info, error. The default is info.
      3Specifies the logging format for the Custom Metrics Autoscaler Operator log messages. The allowed values are console or json. The default is console.
      4Specifies the logging level for the Custom Metrics Autoscaler Metrics Server. The allowed values are 0 for info and 4 or debug. The default is 0.
      5Activates audit logging for the Custom Metrics Autoscaler Operator and specifies the audit policy to use, as described in the “Configuring audit logging” section.
    5. Click Create to create the KEDAController.

Understanding the custom metrics autoscaler triggers

Triggers, also known as scalers, provide the metrics that the Custom Metrics Autoscaler Operator uses to scale your pods.

The custom metrics autoscaler currently supports only the Prometheus, CPU, memory, and Apache Kafka triggers.

You use a ScaledObject or ScaledJob custom resource to configure triggers for specific objects, as described in the sections that follow.

Understanding the Prometheus trigger

You can scale pods based on Prometheus metrics, which can use the installed OKD monitoring or an external Prometheus server as the metrics source. See Additional resources for information on the configurations required to use the OKD monitoring as a source for metrics.

If Prometheus is taking metrics from the application that the custom metrics autoscaler is scaling, do not set the minimum replicas to 0 in the custom resource. If there are no application pods, the custom metrics autoscaler does not have any metrics to scale on.

Example scaled object with a Prometheus target

  1. apiVersion: keda.sh/v1alpha1
  2. kind: ScaledObject
  3. metadata:
  4. name: prom-scaledobject
  5. namespace: my-namespace
  6. spec:
  7. ...
  8. triggers:
  9. - type: prometheus (1)
  10. metadata:
  11. serverAddress: https://thanos-querier.openshift-monitoring.svc.cluster.local:9092 (2)
  12. namespace: kedatest (3)
  13. metricName: http_requests_total (4)
  14. threshold: '5' (5)
  15. query: sum(rate(http_requests_total{job="test-app"}[1m])) (6)
  16. authModes: "basic" (7)
  17. cortexOrgID: my-org (8)
  18. ignoreNullValues: false (9)
  19. unsafeSsl: "false" (10)
1Specifies Prometheus as the scaler/trigger type.
2Specifies the address of the Prometheus server. This example uses OKD monitoring.
3Optional: Specifies the namespace of the object you want to scale. This parameter is mandatory if OKD monitoring as a source for the metrics.
4Specifies the name to identify the metric in the external.metrics.k8s.io API. If you are using more than one trigger, all metric names must be unique.
5Specifies the value to start scaling for.
6Specifies the Prometheus query to use.
7Specifies the authentication method to use. Prometheus scalers support bearer authentication (bearer), basic authentication (basic), or TLS authentication (tls). You configure the specific authentication parameters in a trigger authentication, as discussed in a following section. As needed, you can also use a secret.
8Optional: Passes the X-Scope-OrgID header to multi-tenant Cortex or Mimir storage for Prometheus. This parameter is required only with multi-tenant Prometheus storage, to indicate which data Prometheus should return.
9Optional: Specifies how the trigger should proceed if the Prometheus target is lost.
  • If true, the trigger continues to operate if the Prometheus target is lost. This is the default.

  • If false, the trigger returns an error if the Prometheus target is lost.

10Optional: Specifies whether the certificate check should be skipped. For example, you might skip the check if you use self-signed certificates at the Prometheus endpoint.
  • If true, the certificate check is performed.

  • If false, the certificate check is not performed. This is the default.

Understanding the CPU trigger

You can scale pods based on CPU metrics. This trigger uses cluster metrics as the source for metrics.

The custom metrics autoscaler scales the pods associated with an object to maintain the CPU usage that you specify. The autoscaler increases or decreases the number of replicas between the minimum and maximum numbers to maintain the specified CPU utilization across all pods. The memory trigger considers the memory utilization of the entire pod. If the pod has multiple containers, the memory utilization is the sum of all of the containers.

  • This trigger cannot be used with the ScaledJob custom resource.

  • When using a memory trigger to scale an object, the object does not scale to 0, even if you are using multiple triggers.

Example scaled object with a CPU target

  1. apiVersion: keda.sh/v1alpha1
  2. kind: ScaledObject
  3. metadata:
  4. name: cpu-scaledobject
  5. namespace: my-namespace
  6. spec:
  7. ...
  8. triggers:
  9. - type: cpu (1)
  10. metricType: Utilization (2)
  11. metadata:
  12. value: "60" (3)
  13. containerName: "api" (4)
1Specifies CPU as the scaler/trigger type.
2Specifies the type of metric to use, either Utilization or AverageValue.
3Specifies the value to trigger scaling actions upon:
  • When using Utilization, the target value is the average of the resource metrics across all relevant pods, represented as a percentage of the requested value of the resource for the pods.

  • When using AverageValue, the target value is the average of the metrics across all relevant pods.

4Optional. Specifies an individual container to scale, based on the memory utilization of only that container, rather than the entire pod. Here, only the container named api is to be scaled.

Understanding the memory trigger

You can scale pods based on memory metrics. This trigger uses cluster metrics as the source for metrics.

The custom metrics autoscaler scales the pods associated with an object to maintain the average memory usage that you specify. The autoscaler increases and decreases the number of replicas between the minimum and maximum numbers to maintain the specified memory utilization across all pods. The memory trigger considers the memory utilization of entire pod. If the pod has multiple containers, the memory utilization is the sum of all of the containers.

  • This trigger cannot be used with the ScaledJob custom resource.

  • When using a memory trigger to scale an object, the object does not scale to 0, even if you are using multiple triggers.

Example scaled object with a memory target

  1. apiVersion: keda.sh/v1alpha1
  2. kind: ScaledObject
  3. metadata:
  4. name: memory-scaledobject
  5. namespace: my-namespace
  6. spec:
  7. ...
  8. triggers:
  9. - type: memory (1)
  10. metricType: Utilization (2)
  11. metadata:
  12. value: "60" (3)
  13. containerName: "api" (4)
1Specifies memory as the scaler/trigger type.
2Specifies the type of metric to use, either Utilization or AverageValue.
3Specifies the value to trigger scaling actions for:
  • When using Utilization, the target value is the average of the resource metrics across all relevant pods, represented as a percentage of the requested value of the resource for the pods.

  • When using AverageValue, the target value is the average of the metrics across all relevant pods.

4Optional. Specifies an individual container to scale, based on the memory utilization of only that container, rather than the entire pod. Here, only the container named api is to be scaled.

Understanding the Kafka trigger

You can scale pods based on an Apache Kafka topic or other services that support the Kafka protocol. The custom metrics autoscaler does not scale higher than the number of Kafka partitions, unless you set the allowIdleConsumers parameter to true in the scaled object or scaled job.

If the number of consumer groups exceeds the number of partitions in a topic, the extra consumer groups sit idle.

To avoid this, by default the number of replicas does not exceed:

  • The number of partitions on a topic, if a topic is specified.

  • The number of partitions of all topics in the consumer group, if no topic is specified.

  • The maxReplicaCount specified in scaled object or scaled job CR.

You can use the allowIdleConsumers parameter to disable these default behaviors.

Example scaled object with a Kafka target

  1. apiVersion: keda.sh/v1alpha1
  2. kind: ScaledObject
  3. metadata:
  4. name: kafka-scaledobject
  5. namespace: my-namespace
  6. spec:
  7. ...
  8. triggers:
  9. - type: kafka (1)
  10. metadata:
  11. topic: my-topic (2)
  12. bootstrapServers: my-cluster-kafka-bootstrap.openshift-operators.svc:9092 (3)
  13. consumerGroup: my-group (4)
  14. lagThreshold: '10' (5)
  15. activationLagThreshold (6)
  16. offsetResetPolicy: 'latest' (7)
  17. allowIdleConsumers: true (8)
  18. scaleToZeroOnInvalidOffset: false (9)
  19. excludePersistentLag: false (10)
  20. version: 1.0.0 (11)
  21. partitionLimitation: '1,2,10-20,31' (12)
1Specifies Kafka as the scaler/trigger type.
2Specifies the name of the Kafka topic on which Kafka is processing the offset lag.
3Specifies a comma-separated list of Kafka brokers to connect to.
4Specifies the name of the Kafka consumer group used for checking the offset on the topic and processing the related lag.
5Optional: Specifies the average target value to trigger scaling actions. The default is 5.
6Optional: Specifies the target value for the activation phase.
7Optional: Specifies the Kafka offset reset policy for the Kafka consumer. The available values are: latest and earliest. The default is latest.
8Optional: Specifies whether the number of Kafka replicas can exceed the number of partitions on a topic.
  • If true, the number of Kafka replicas can exceed the number of partitions on a topic. This allows for idle Kafka consumers.

  • If false, the number of Kafka replicas cannot exceed the number of partitions on a topic. This is the default.

9Specifies how the trigger behaves when a Kafka partition does not have a valid offset.
  • If true, the consumers are scaled to zero for that partition.

  • If false, the scaler keeps a single consumer for that partition. This is the default.

10Optional: Specifies whether the trigger includes or excludes partition lag for partitions whose current offset is the same as the current offset of the previous polling cycle.
  • If true, the scaler excludes partition lag in these partitions.

  • If false, the trigger includes all consumer lag in all partitions. This is the default.

11Optional: Specifies the version of your Kafka brokers. The default is 1.0.0.
12Optional: Specifies a comma-separated list of partition IDs to scope the scaling on. If set, only the listed IDs are considered when calculating lag. The default is to consider all partitions.

Additional resources

Understanding custom metrics autoscaler trigger authentications

A trigger authentication allows you to include authentication information in a scaled object or a scaled job that can be used by the associated containers. You can use trigger authentications to pass OKD secrets, platform-native pod authentication mechanisms, environment variables, and so on.

You define a TriggerAuthentication object in the same namespace as the object that you want to scale. That trigger authentication can be used only by objects in that namespace.

Alternatively, to share credentials between objects in multiple namespaces, you can create a ClusterTriggerAuthentication object that can be used across all namespaces.

Trigger authentications and cluster trigger authentication use the same configuration. However, a cluster trigger authentication requires an additional kind parameter in the authentication reference of the scaled object.

Example trigger authentication with a secret

  1. kind: TriggerAuthentication
  2. apiVersion: keda.sh/v1alpha1
  3. metadata:
  4. name: secret-triggerauthentication
  5. namespace: my-namespace (1)
  6. spec:
  7. secretTargetRef: (2)
  8. - parameter: user-name (3)
  9. name: my-secret (4)
  10. key: USER_NAME (5)
  11. - parameter: password
  12. name: my-secret
  13. key: USER_PASSWORD
1Specifies the namespace of the object you want to scale.
2Specifies that this trigger authentication uses a secret for authorization.
3Specifies the authentication parameter to supply by using the secret.
4Specifies the name of the secret to use.
5Specifies the key in the secret to use with the specified parameter.

Example cluster trigger authentication with a secret

  1. kind: ClusterTriggerAuthentication
  2. apiVersion: keda.sh/v1alpha1
  3. metadata: (1)
  4. name: secret-cluster-triggerauthentication
  5. spec:
  6. secretTargetRef: (2)
  7. - parameter: user-name (3)
  8. name: secret-name (4)
  9. key: USER_NAME (5)
  10. - parameter: user-password
  11. name: secret-name
  12. key: USER_PASSWORD
1Note that no namespace is used with a cluster trigger authentication.
2Specifies that this trigger authentication uses a secret for authorization.
3Specifies the authentication parameter to supply by using the secret.
4Specifies the name of the secret to use.
5Specifies the key in the secret to use with the specified parameter.

Example trigger authentication with a token

  1. kind: TriggerAuthentication
  2. apiVersion: keda.sh/v1alpha1
  3. metadata:
  4. name: token-triggerauthentication
  5. namespace: my-namespace (1)
  6. spec:
  7. secretTargetRef: (2)
  8. - parameter: bearerToken (3)
  9. name: my-token-2vzfq (4)
  10. key: token (5)
  11. - parameter: ca
  12. name: my-token-2vzfq
  13. key: ca.crt
1Specifies the namespace of the object you want to scale.
2Specifies that this trigger authentication uses a secret for authorization.
3Specifies the authentication parameter to supply by using the token.
4Specifies the name of the token to use.
5Specifies the key in the token to use with the specified parameter.

Example trigger authentication with an environment variable

  1. kind: TriggerAuthentication
  2. apiVersion: keda.sh/v1alpha1
  3. metadata:
  4. name: env-var-triggerauthentication
  5. namespace: my-namespace (1)
  6. spec:
  7. env: (2)
  8. - parameter: access_key (3)
  9. name: ACCESS_KEY (4)
  10. containerName: my-container (5)
1Specifies the namespace of the object you want to scale.
2Specifies that this trigger authentication uses environment variables for authorization.
3Specify the parameter to set with this variable.
4Specify the name of the environment variable.
5Optional: Specify a container that requires authentication. The container must be in the same resource as referenced by scaleTargetRef in the scaled object.

Example trigger authentication with pod authentication providers

  1. kind: TriggerAuthentication
  2. apiVersion: keda.sh/v1alpha1
  3. metadata:
  4. name: pod-id-triggerauthentication
  5. namespace: my-namespace (1)
  6. spec:
  7. podIdentity: (2)
  8. provider: aws-eks (3)
1Specifies the namespace of the object you want to scale.
2Specifies that this trigger authentication uses a platform-native pod authentication method for authorization.
3Specifies a pod identity. Supported values are none, azure, aws-eks, or aws-kiam. The default is none.

Additional resources

Using trigger authentications

You use trigger authentications and cluster trigger authentications by using a custom resource to create the authentication, then add a reference to a scaled object or scaled job.

Prerequisites

  • The Custom Metrics Autoscaler Operator must be installed.

  • If you are using a secret, the Secret object must exist, for example:

    Example secret

    1. apiVersion: v1
    2. kind: Secret
    3. metadata:
    4. name: my-secret
    5. data:
    6. user-name: <base64_USER_NAME>
    7. password: <base64_USER_PASSWORD>

Procedure

  1. Create the TriggerAuthentication or ClusterTriggerAuthentication object.

    1. Create a YAML file that defines the object:

      Example trigger authentication with a secret

      1. kind: TriggerAuthentication
      2. apiVersion: keda.sh/v1alpha1
      3. metadata:
      4. name: prom-triggerauthentication
      5. namespace: my-namespace
      6. spec:
      7. secretTargetRef:
      8. - parameter: user-name
      9. name: my-secret
      10. key: USER_NAME
      11. - parameter: password
      12. name: my-secret
      13. key: USER_PASSWORD
    2. Create the TriggerAuthentication object:

      1. $ oc create -f <file-name>.yaml
  2. Create or edit a ScaledObject YAML file:

    Example scaled object

    1. apiVersion: keda.sh/v1alpha1
    2. kind: ScaledObject
    3. metadata:
    4. name: scaledobject
    5. namespace: my-namespace
    6. spec:
    7. scaleTargetRef:
    8. name: example-deployment
    9. maxReplicaCount: 100
    10. minReplicaCount: 0
    11. pollingInterval: 30
    12. triggers:
    13. - authenticationRef:
    14. type: prometheus
    15. metadata:
    16. serverAddress: https://thanos-querier.openshift-monitoring.svc.cluster.local:9092
    17. namespace: kedatest # replace <NAMESPACE>
    18. metricName: http_requests_total
    19. threshold: '5'
    20. query: sum(rate(http_requests_total{job="test-app"}[1m]))
    21. authModes: "basic"
    22. - authenticationRef: (1)
    23. name: prom-triggerauthentication
    24. metadata:
    25. name: prom-triggerauthentication
    26. type: object
    27. - authenticationRef: (2)
    28. name: prom-cluster-triggerauthentication
    29. kind: ClusterTriggerAuthentication
    30. metadata:
    31. name: prom-cluster-triggerauthentication
    32. type: object
    1Optional: Specify a trigger authentication.
    2Optional: Specify a cluster trigger authentication. You must include the kind: ClusterTriggerAuthentication parameter.

    It is not necessary to specify both a namespace trigger authentication and a cluster trigger authentication.

  3. Create the object. For example:

    1. $ oc apply -f <file-name>

Configuring the custom metrics autoscaler to use OKD monitoring

You can use the installed OKD Prometheus monitoring as a source for the metrics used by the custom metrics autoscaler. However, there are some additional configurations you must perform.

These steps are not required for an external Prometheus source.

You must perform the following tasks, as described in this section:

  • Create a service account to get a token.

  • Create a role.

  • Add that role to the service account.

  • Reference the token in the trigger authentication object used by Prometheus.

Prerequisites

  • OKD monitoring must be installed.

  • Monitoring of user-defined workloads must be enabled in OKD monitoring, as described in the Creating a user-defined workload monitoring config map section.

  • The Custom Metrics Autoscaler Operator must be installed.

Procedure

  1. Change to the project with the object you want to scale:

    1. $ oc project my-project
  2. Use the following command to create a service account, if your cluster does not have one:

    1. $ oc create serviceaccount <service_account>

    where:

    <service_account>

    Specifies the name of the service account.

  3. Use the following command to locate the token assigned to the service account:

    1. $ oc describe serviceaccount <service_account>

    where:

    <service_account>

    Specifies the name of the service account.

    Example output

    1. Name: thanos
    2. Namespace: my-project
    3. Labels: <none>
    4. Annotations: <none>
    5. Image pull secrets: thanos-dockercfg-nnwgj
    6. Mountable secrets: thanos-dockercfg-nnwgj
    7. Tokens: thanos-token-9g4n5 (1)
    8. Events: <none>
    1Use this token in the trigger authentication.
  4. Create a trigger authentication with the service account token:

    1. Create a YAML file similar to the following:

      1. apiVersion: keda.sh/v1alpha1
      2. kind: TriggerAuthentication
      3. metadata:
      4. name: keda-trigger-auth-prometheus
      5. spec:
      6. secretTargetRef: (1)
      7. - parameter: bearerToken (2)
      8. name: thanos-token-9g4n5 (3)
      9. key: token (4)
      10. - parameter: ca
      11. name: thanos-token-9g4n5
      12. key: ca.crt
      1Specifies that this object uses a secret for authorization.
      2Specifies the authentication parameter to supply by using the token.
      3Specifies the name of the token to use.
      4Specifies the key in the token to use with the specified parameter.
    2. Create the CR object:

      1. $ oc create -f <file-name>.yaml
  5. Create a role for reading Thanos metrics:

    1. Create a YAML file with the following parameters:

      1. apiVersion: rbac.authorization.k8s.io/v1
      2. kind: Role
      3. metadata:
      4. name: thanos-metrics-reader
      5. rules:
      6. - apiGroups:
      7. - ""
      8. resources:
      9. - pods
      10. verbs:
      11. - get
      12. - apiGroups:
      13. - metrics.k8s.io
      14. resources:
      15. - pods
      16. - nodes
      17. verbs:
      18. - get
      19. - list
      20. - watch
    2. Create the CR object:

      1. $ oc create -f <file-name>.yaml
  6. Create a role binding for reading Thanos metrics:

    1. Create a YAML file similar to the following:

      1. apiVersion: rbac.authorization.k8s.io/v1
      2. kind: RoleBinding
      3. metadata:
      4. name: thanos-metrics-reader (1)
      5. namespace: my-project (2)
      6. roleRef:
      7. apiGroup: rbac.authorization.k8s.io
      8. kind: Role
      9. name: thanos-metrics-reader
      10. subjects:
      11. - kind: ServiceAccount
      12. name: thanos (3)
      13. namespace: my-project (4)
      1Specifies the name of the role you created.
      2Specifies the namespace of the object you want to scale.
      3Specifies the name of the service account to bind to the role.
      4Specifies the namespace of the object you want to scale.
    2. Create the CR object:

      1. $ oc create -f <file-name>.yaml

You can now deploy a scaled object or scaled job to enable autoscaling for your application, as described in the following sections. To use OKD monitoring as the source, in the trigger, or scaler, specify the prometheus type and use https://thanos-querier.openshift-monitoring.svc.cluster.local:9092 as the serverAddress.

Additional resources

Pausing the custom metrics autoscaler for a workload

You can pause the autoscaling of a workload, as needed, by adding the autoscaling.keda.sh/paused-replicas annotation to the custom metrics autoscaler for that workload. The custom metrics autoscaler scales the replicas for that workload to the specified value and pauses autoscaling until the annotation is removed.

  1. apiVersion: keda.sh/v1alpha1
  2. kind: ScaledObject
  3. metadata:
  4. annotations:
  5. autoscaling.keda.sh/paused-replicas: "4"
  6. ...

To restart autoscaling, edit the ScaledObject CR to remove the annotation.

For example, you might want to pause autoscaling before performing cluster maintenance or to avoid resource starvation by removing non-mission-critical workloads.

Procedure

  1. Use the following command to edit the ScaledObject CR for your workload:

    1. $ oc edit ScaledObject scaledobject
  2. Add the autoscaling.keda.sh/paused-replicas annotation with any value:

    1. apiVersion: keda.sh/v1alpha1
    2. kind: ScaledObject
    3. metadata:
    4. annotations:
    5. autoscaling.keda.sh/paused-replicas: "4" (1)
    6. creationTimestamp: "2023-02-08T14:41:01Z"
    7. generation: 1
    8. name: scaledobject
    9. namespace: my-project
    10. resourceVersion: "65729"
    11. uid: f5aec682-acdf-4232-a783-58b5b82f5dd0
    1Specifies that the Custom Metrics Autoscaler Operator is to scale the replicas to the specified value and stop autoscaling.

Configuring audit logging

You can gather audit logs, which are a security-relevant chronological set of records documenting the sequence of activities that have affected the system by individual users, administrators, or other components of the system.

For example, audit logs can help you understand where an autoscaling request is coming from. This is key information when backends are getting overloaded by autoscaling requests made by user applications and you need to determine which is the troublesome application. You can configure auditing for the Custom Metrics Autoscaler Operator by editing the KedaController custom resource. The logs are sent to an audit log file on a volume that is secured by using a persistent volume claim in the KedaController CR.

Prerequisites

  • The Custom Metrics Autoscaler Operator must be installed.

Procedure

  1. Edit the KedaController custom resource to add the auditConfig stanza:

    1. kind: KedaController
    2. apiVersion: keda.sh/v1alpha1
    3. metadata:
    4. name: keda
    5. namespace: openshift-keda
    6. spec:
    7. ...
    8. metricsServer:
    9. ...
    10. auditConfig:
    11. logFormat: "json" (1)
    12. logOutputVolumeClaim: "pvc-audit-log" (2)
    13. policy:
    14. rules: (3)
    15. - level: Metadata
    16. omitStages: "RequestReceived" (4)
    17. omitManagedFields: false (5)
    18. lifetime: (6)
    19. maxAge: "2"
    20. maxBackup: "1"
    21. maxSize: "50"
    1Specifies the output format of the audit log, either legacy or json.
    2Specifies an existing persistent volume claim for storing the log data. All requests coming to the API server are logged to this persistent volume claim. If you leave this field empty, the log data is sent to stdout.
    3Specifies which events should be recorded and what data they should include:
    • None: Do not log events.

    • Metadata: Log only the metadata for the request, such as user, timestamp, and so forth. Do not log the request text and the response text. This is the default.

    • Request: Log only the metadata and the request text but not the response text. This option does not apply for non-resource requests.

    • RequestResponse: Log event metadata, request text, and response text. This option does not apply for non-resource requests.

    4Specifies stages for which no event is created.
    5Specifies whether to omit the managed fields of the request and response bodies from being written to the API audit log, either true to omit the fields or false to include the fields.
    6Specifies the size and lifespan of the audit logs.
    • maxAge: The maximum number of days to retain audit log files, based on the timestamp encoded in their filename.

    • maxBackup: The maximum number of audit log files to retain. Set to 0 to retain all audit log files.

    • maxSize: The maximum size in megabytes of an audit log file before it gets rotated.

Verification

  1. View the audit log file directly:

    1. Obtain the name of the keda-metrics-apiserver-* pod:

      1. oc get pod -n openshift-keda

      Example output

      1. NAME READY STATUS RESTARTS AGE
      2. custom-metrics-autoscaler-operator-5cb44cd75d-9v4lv 1/1 Running 0 8m20s
      3. keda-metrics-apiserver-65c7cc44fd-rrl4r 1/1 Running 0 2m55s
      4. keda-operator-776cbb6768-zpj5b 1/1 Running 0 2m55s
    2. View the log data by using a command similar to the following:

      1. $ oc logs keda-metrics-apiserver-<hash>|grep -i metadata (1)
      1Optional: You can use the grep command to specify the log level to display: Metadata, Request, RequestResponse.

      For example:

      1. $ oc logs keda-metrics-apiserver-65c7cc44fd-rrl4r|grep -i metadata

      Example output

      1. ...
      2. {"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Metadata","auditID":"4c81d41b-3dab-4675-90ce-20b87ce24013","stage":"ResponseComplete","requestURI":"/healthz","verb":"get","user":{"username":"system:anonymous","groups":["system:unauthenticated"]},"sourceIPs":["10.131.0.1"],"userAgent":"kube-probe/1.26","responseStatus":{"metadata":{},"code":200},"requestReceivedTimestamp":"2023-02-16T13:00:03.554567Z","stageTimestamp":"2023-02-16T13:00:03.555032Z","annotations":{"authorization.k8s.io/decision":"allow","authorization.k8s.io/reason":""}}
      3. ...
  2. Alternatively, you can view a specific log:

    1. Use a command similar to the following to log into the keda-metrics-apiserver-* pod:

      1. $ oc rsh pod/keda-metrics-apiserver-<hash> -n openshift-keda

      For example:

      1. $ oc rsh pod/keda-metrics-apiserver-65c7cc44fd-rrl4r -n openshift-keda
    2. Change to the /var/audit-policy/ directory:

      1. sh-4.4$ cd /var/audit-policy/
    3. List the available logs:

      1. sh-4.4$ ls

      Example output

      1. log-2023.02.17-14:50 policy.yaml
    4. View the log, as needed:

      1. sh-4.4$ cat <log_name>/<pvc_name>|grep -i <log_level> (1)
      1Optional: You can use the grep command to specify the log level to display: Metadata, Request, RequestResponse.

      For example:

      1. sh-4.4$ cat log-2023.02.17-14:50/pvc-audit-log|grep -i Request

      Example output

      1. ...
      2. {"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Request","auditID":"63e7f68c-04ec-4f4d-8749-bf1656572a41","stage":"ResponseComplete","requestURI":"/openapi/v2","verb":"get","user":{"username":"system:aggregator","groups":["system:authenticated"]},"sourceIPs":["10.128.0.1"],"responseStatus":{"metadata":{},"code":304},"requestReceivedTimestamp":"2023-02-17T13:12:55.035478Z","stageTimestamp":"2023-02-17T13:12:55.038346Z","annotations":{"authorization.k8s.io/decision":"allow","authorization.k8s.io/reason":"RBAC: allowed by ClusterRoleBinding \"system:discovery\" of ClusterRole \"system:discovery\" to Group \"system:authenticated\""}}
      3. ...

Additional resources

Gathering debugging data

You can use the must-gather tool to collect data about the Custom Metrics Autoscaler Operator and its components, including:

  • The openshift-keda namespace and its child objects.

  • The Custom Metric Autoscaler Operator installation objects.

  • The Custom Metric Autoscaler Operator CRD objects.

The following command runs the must-gather tool for the Custom Metrics Autoscaler Operator:

  1. $ oc adm must-gather --image="$(oc get packagemanifests openshift-custom-metrics-autoscaler-operator \
  2. -n openshift-marketplace \
  3. -o jsonpath='{.status.channels[?(@.name=="stable")].currentCSVDesc.annotations.containerImage}')"

The standard OKD must-gather command, oc adm must-gather, does not collect Custom Metrics Autoscaler Operator data.

Prerequisites

  • Access to the cluster as a user with the cluster-admin role.

  • The OKD CLI (oc) installed.

Procedure

  1. Navigate to the directory where you want to store the must-gather data.

    If your cluster is using a restricted network, you must take additional steps. If your mirror registry has a trusted CA, you must first add the trusted CA to the cluster. For all clusters on restricted networks, you must import the default must-gather image as an image stream by running the following command.

    1. $ oc import-image is/must-gather -n openshift
  2. Perform one of the following:

    • To get only the Custom Metrics Autoscaler Operator must-gather data, use the following command:

      1. $ oc adm must-gather --image="$(oc get packagemanifests openshift-custom-metrics-autoscaler-operator \
      2. -n openshift-marketplace \
      3. -o jsonpath='{.status.channels[?(@.name=="stable")].currentCSVDesc.annotations.containerImage}')"

      The custom image for the must-gather command is pulled directly from the Operator package manifests, so that it works on any cluster where the Custom Metric Autoscaler Operator is available.

    • To gather the default must-gather data in addition to the Custom Metric Autoscaler Operator information:

      1. Use the following command to obtain the Custom Metrics Autoscaler Operator image and set it as an environment variable:

        1. $ IMAGE="$(oc get packagemanifests openshift-custom-metrics-autoscaler-operator \
        2. -n openshift-marketplace \
        3. -o jsonpath='{.status.channels[?(@.name=="stable")].currentCSVDesc.annotations.containerImage}')"
      2. Use the oc adm must-gather with the Custom Metrics Autoscaler Operator image:

        1. $ oc adm must-gather --image-stream=openshift/must-gather --image=${IMAGE}
  1. Example must-gather output for the Custom Metric Autoscaler:
  2. ```
  3. └── openshift-keda
  4. ├── apps
  5. │ ├── daemonsets.yaml
  6. │ ├── deployments.yaml
  7. │ ├── replicasets.yaml
  8. │ └── statefulsets.yaml
  9. ├── apps.openshift.io
  10. │ └── deploymentconfigs.yaml
  11. ├── autoscaling
  12. │ └── horizontalpodautoscalers.yaml
  13. ├── batch
  14. │ ├── cronjobs.yaml
  15. │ └── jobs.yaml
  16. ├── build.openshift.io
  17. │ ├── buildconfigs.yaml
  18. │ └── builds.yaml
  19. ├── core
  20. │ ├── configmaps.yaml
  21. │ ├── endpoints.yaml
  22. │ ├── events.yaml
  23. │ ├── persistentvolumeclaims.yaml
  24. │ ├── pods.yaml
  25. │ ├── replicationcontrollers.yaml
  26. │ ├── secrets.yaml
  27. │ └── services.yaml
  28. ├── discovery.k8s.io
  29. │ └── endpointslices.yaml
  30. ├── image.openshift.io
  31. │ └── imagestreams.yaml
  32. ├── k8s.ovn.org
  33. │ ├── egressfirewalls.yaml
  34. │ └── egressqoses.yaml
  35. ├── keda.sh
  36. │ ├── kedacontrollers
  37. │ │ └── keda.yaml
  38. │ ├── scaledobjects
  39. │ │ └── example-scaledobject.yaml
  40. │ └── triggerauthentications
  41. │ └── example-triggerauthentication.yaml
  42. ├── monitoring.coreos.com
  43. │ └── servicemonitors.yaml
  44. ├── networking.k8s.io
  45. │ └── networkpolicies.yaml
  46. ├── openshift-keda.yaml
  47. ├── pods
  48. │ ├── custom-metrics-autoscaler-operator-58bd9f458-ptgwx
  49. │ │ ├── custom-metrics-autoscaler-operator
  50. │ │ │ └── custom-metrics-autoscaler-operator
  51. │ │ │ └── logs
  52. │ │ │ ├── current.log
  53. │ │ │ ├── previous.insecure.log
  54. │ │ │ └── previous.log
  55. │ │ └── custom-metrics-autoscaler-operator-58bd9f458-ptgwx.yaml
  56. │ ├── custom-metrics-autoscaler-operator-58bd9f458-thbsh
  57. │ │ └── custom-metrics-autoscaler-operator
  58. │ │ └── custom-metrics-autoscaler-operator
  59. │ │ └── logs
  60. │ ├── keda-metrics-apiserver-65c7cc44fd-6wq4g
  61. │ │ ├── keda-metrics-apiserver
  62. │ │ │ └── keda-metrics-apiserver
  63. │ │ │ └── logs
  64. │ │ │ ├── current.log
  65. │ │ │ ├── previous.insecure.log
  66. │ │ │ └── previous.log
  67. │ │ └── keda-metrics-apiserver-65c7cc44fd-6wq4g.yaml
  68. │ └── keda-operator-776cbb6768-fb6m5
  69. │ ├── keda-operator
  70. │ │ └── keda-operator
  71. │ │ └── logs
  72. │ │ ├── current.log
  73. │ │ ├── previous.insecure.log
  74. │ │ └── previous.log
  75. │ └── keda-operator-776cbb6768-fb6m5.yaml
  76. ├── policy
  77. │ └── poddisruptionbudgets.yaml
  78. └── route.openshift.io
  79. └── routes.yaml
  80. ```

Additional resources

Accessing performance metrics

The Custom Metrics Autoscaler Operator exposes ready-to-use metrics that it pulls from the on-cluster monitoring component. You can query the metrics by using the Prometheus Query Language (PromQL) to analyze and diagnose issues. All metrics are reset when the controller pod restarts.

You can access the metrics and run queries by using the OKD web console.

Procedure

  1. Select the Administrator perspective in the OKD web console.

  2. Select ObserveMetrics.

  3. To create a custom query, add your PromQL query to the Expression field.

  4. To add multiple queries, select Add Query.

Provided metrics

The Custom Metrics Autoscaler Operator exposes the following metrics, which you can view by using the OKD web console.

Table 1. Custom Metric Autoscaler Operator metrics
Metric nameDescription

keda_scaler_activity

Whether the particular scaler is active or inactive. A value of 1 indicates the scaler is active; a value of 0 indicates the scaler is inactive.

keda_scaler_metrics_value

The current value for each scaler’s metric, which is used by the Horizontal Pod Autoscaler (HPA) in computing the target average.

keda_scaler_metrics_latency

The latency of retrieving the current metric from each scaler.

keda_scaler_errors

The number of errors that have occurred for each scaler.

keda_scaler_errors_total

The total number of errors encountered for all scalers.

keda_scaled_object_errors

The number of errors that have occurred for each scaled obejct.

keda_resource_totals

The total number of Custom Metrics Autoscaler custom resources in each namespace for each custom resource type.

keda_trigger_totals

The total number of triggers by trigger type.

Custom Metrics Autoscaler Admission webhook metrics

The Custom Metrics Autoscaler Admission webhook also exposes the following Prometheus metrics.

Metric nameDescription

keda_scaled_object_validation_total

The number of scaled object validations.

keda_scaled_object_validation_errors

The number of validation errors.

Understanding how to add custom metrics autoscalers

To add a custom metrics autoscaler, create a ScaledObject custom resource for a deployment, stateful set, or custom resource. Create a ScaledJob custom resource for a job.

You can create only one scaled object or scaled job for each workload that you want to scale. Also, you cannot use a scaled object or scaled job and the horizontal pod autoscaler (HPA) on the same workload.

Adding a custom metrics autoscaler to a workload

You can create a custom metrics autoscaler for a workload that is created by a Deployment, StatefulSet, or custom resource object.

Prerequisites

  • The Custom Metrics Autoscaler Operator must be installed.

  • If you use a custom metrics autoscaler for scaling based on CPU or memory:

    • Your cluster administrator must have properly configured cluster metrics. You can use the oc describe PodMetrics <pod-name> command to determine if metrics are configured. If metrics are configured, the output appears similar to the following, with CPU and Memory displayed under Usage.

      1. $ oc describe PodMetrics openshift-kube-scheduler-ip-10-0-135-131.ec2.internal

      Example output

      1. Name: openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
      2. Namespace: openshift-kube-scheduler
      3. Labels: <none>
      4. Annotations: <none>
      5. API Version: metrics.k8s.io/v1beta1
      6. Containers:
      7. Name: wait-for-host-port
      8. Usage:
      9. Memory: 0
      10. Name: scheduler
      11. Usage:
      12. Cpu: 8m
      13. Memory: 45440Ki
      14. Kind: PodMetrics
      15. Metadata:
      16. Creation Timestamp: 2019-05-23T18:47:56Z
      17. Self Link: /apis/metrics.k8s.io/v1beta1/namespaces/openshift-kube-scheduler/pods/openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
      18. Timestamp: 2019-05-23T18:47:56Z
      19. Window: 1m0s
      20. Events: <none>
    • The pods associated with the object you want to scale must include specified memory and CPU limits. For example:

      Example pod spec

      1. apiVersion: v1
      2. kind: Pod
      3. ...
      4. spec:
      5. containers:
      6. - name: app
      7. image: images.my-company.example/app:v4
      8. resources:
      9. limits:
      10. memory: "128Mi"
      11. cpu: "500m"

Procedure

  1. Create a YAML file similar to the following. Only the name <2>, object name <4>, and object kind <5> are required:

    Example scaled object

    1. apiVersion: keda.sh/v1alpha1
    2. kind: ScaledObject
    3. metadata:
    4. annotations:
    5. autoscaling.keda.sh/paused-replicas: "0" (1)
    6. name: scaledobject (2)
    7. namespace: my-namespace
    8. spec:
    9. scaleTargetRef:
    10. apiVersion: apps/v1 (3)
    11. name: example-deployment (4)
    12. kind: Deployment (5)
    13. envSourceContainerName: .spec.template.spec.containers[0] (6)
    14. cooldownPeriod: 200 (7)
    15. maxReplicaCount: 100 (8)
    16. minReplicaCount: 0 (9)
    17. metricsServer: (10)
    18. auditConfig:
    19. logFormat: "json"
    20. logOutputVolumeClaim: "persistentVolumeClaimName"
    21. policy:
    22. rules:
    23. - level: Metadata
    24. omitStages: "RequestReceived"
    25. omitManagedFields: false
    26. lifetime:
    27. maxAge: "2"
    28. maxBackup: "1"
    29. maxSize: "50"
    30. fallback: (11)
    31. failureThreshold: 3
    32. replicas: 6
    33. pollingInterval: 30 (12)
    34. advanced:
    35. restoreToOriginalReplicaCount: false (13)
    36. horizontalPodAutoscalerConfig:
    37. name: keda-hpa-scale-down (14)
    38. behavior: (15)
    39. scaleDown:
    40. stabilizationWindowSeconds: 300
    41. policies:
    42. - type: Percent
    43. value: 100
    44. periodSeconds: 15
    45. triggers:
    46. - type: prometheus (16)
    47. metadata:
    48. serverAddress: https://thanos-querier.openshift-monitoring.svc.cluster.local:9092
    49. namespace: kedatest
    50. metricName: http_requests_total
    51. threshold: '5'
    52. query: sum(rate(http_requests_total{job="test-app"}[1m]))
    53. authModes: "basic"
    54. - authenticationRef: (17)
    55. name: prom-triggerauthentication
    56. metadata:
    57. name: prom-triggerauthentication
    58. type: object
    59. - authenticationRef: (18)
    60. name: prom-cluster-triggerauthentication
    61. metadata:
    62. name: prom-cluster-triggerauthentication
    63. type: object
    1Optional: Specifies that the Custom Metrics Autoscaler Operator is to scale the replicas to the specified value and stop autoscaling, as described in the “Pausing the custom metrics autoscaler for a workload” section.
    2Specifies a name for this custom metrics autoscaler.
    3Optional: Specifies the API version of the target resource. The default is apps/v1.
    4Specifies the name of the object that you want to scale.
    5Specifies the kind as Deployment, StatefulSet or CustomResource.
    6Optional: Specifies the name of the container in the target resource, from which the custom metrics autoscaler gets environment variables holding secrets and so forth. The default is .spec.template.spec.containers[0].
    7Optional. Specifies the period in seconds to wait after the last trigger is reported before scaling the deployment back to 0 if the minReplicaCount is set to 0. The default is 300.
    8Optional: Specifies the maximum number of replicas when scaling up. The default is 100.
    9Optional: Specifies the minimum number of replicas when scaling down.
    10Optional: Specifies the parameters for audit logs. as described in the “Configuring audit logging” section.
    11Optional: Specifies the number of replicas to fall back to if a scaler fails to get metrics from the source for the number of times defined by the failureThreshold parameter. For more information on fallback behavior, see the KEDA documentation.
    12Optional: Specifies the interval in seconds to check each trigger on. The default is 30.
    13Optional: Specifies whether to scale back the target resource to the original replica count after the scaled object is deleted. The default is false, which keeps the replica count as it is when the scaled object is deleted.
    14Optional: Specifies a name for the horizontal pod autoscaler. The default is keda-hpa-{scaled-object-name}.
    15Optional: Specifies a scaling policy to use to control the rate to scale pods up or down, as described in the “Scaling policies” section.
    16Specifies the trigger to use as the basis for scaling, as described in the “Understanding the custom metrics autoscaler triggers” section. This example uses OKD monitoring.
    17Optional: Specifies a trigger authentication, as described in the “Creating a custom metrics autoscaler trigger authentication” section.
    18Optional: Specifies a cluster trigger authentication, as described in the “Creating a custom metrics autoscaler trigger authentication” section.

    It is not necessary to specify both a namespace trigger authentication and a cluster trigger authentication.

  2. Create the custom metrics autoscaler:

    1. $ oc create -f <file-name>.yaml

Verification

  • View the command output to verify that the custom metrics autoscaler was created:

    1. $ oc get scaledobject <scaled_object_name>

    Example output

    1. NAME SCALETARGETKIND SCALETARGETNAME MIN MAX TRIGGERS AUTHENTICATION READY ACTIVE FALLBACK AGE
    2. scaledobject apps/v1.Deployment example-deployment 0 50 prometheus prom-triggerauthentication True True True 17s

    Note the following fields in the output:

  • TRIGGERS: Indicates the trigger, or scaler, that is being used.

  • AUTHENTICATION: Indicates the name of any trigger authentication being used.

  • READY: Indicates whether the scaled object is ready to start scaling:

    • If True, the scaled object is ready.

    • If False, the scaled object is not ready because of a problem in one or more of the objects you created.

  • ACTIVE: Indicates whether scaling is taking place:

    • If True, scaling is taking place.

    • If False, scaling is not taking place because there are no metrics or there is a problem in one or more of the objects you created.

  • FALLBACK: Indicates whether the custom metrics autoscaler is able to get metrics from the source

    • If False, the custom metrics autoscaler is getting metrics.

    • If True, the custom metrics autoscaler is getting metrics because there are no metrics or there is a problem in one or more of the objects you created.

Additional resources

Adding a custom metrics autoscaler to a job

You can create a custom metrics autoscaler for any Job object.

Prerequisites

  • The Custom Metrics Autoscaler Operator must be installed.

Procedure

  1. Create a YAML file similar to the following:

    1. kind: ScaledJob
    2. apiVersion: keda.sh/v1alpha1
    3. metadata:
    4. name: scaledjob
    5. namespace: my-namespace
    6. spec:
    7. failedJobsHistoryLimit: 5
    8. jobTargetRef:
    9. activeDeadlineSeconds: 600 (1)
    10. backoffLimit: 6 (2)
    11. parallelism: 1 (3)
    12. completions: 1 (4)
    13. template: (5)
    14. metadata:
    15. name: pi
    16. spec:
    17. containers:
    18. - name: pi
    19. image: perl
    20. command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
    21. maxReplicaCount: 100 (6)
    22. pollingInterval: 30 (7)
    23. successfulJobsHistoryLimit: 5 (8)
    24. failedJobsHistoryLimit: 5 (9)
    25. envSourceContainerName: (10)
    26. rolloutStrategy: gradual (11)
    27. scalingStrategy: (12)
    28. strategy: "custom"
    29. customScalingQueueLengthDeduction: 1
    30. customScalingRunningJobPercentage: "0.5"
    31. pendingPodConditions:
    32. - "Ready"
    33. - "PodScheduled"
    34. - "AnyOtherCustomPodCondition"
    35. multipleScalersCalculation : "max"
    36. triggers:
    37. - type: prometheus (13)
    38. metadata:
    39. serverAddress: https://thanos-querier.openshift-monitoring.svc.cluster.local:9092
    40. namespace: kedatest
    41. metricName: http_requests_total
    42. threshold: '5'
    43. query: sum(rate(http_requests_total{job="test-app"}[1m]))
    44. authModes: "bearer"
    45. - authenticationRef: (14)
    46. name: prom-triggerauthentication
    47. metadata:
    48. name: prom-triggerauthentication
    49. type: object
    50. - authenticationRef: (15)
    51. name: prom-cluster-triggerauthentication
    52. metadata:
    53. name: prom-cluster-triggerauthentication
    54. type: object
    1Specifies the maximum duration the job can run.
    2Specifies the number of retries for a job. The default is 6.
    3Optional: Specifies how many pod replicas a job should run in parallel; defaults to 1.
    • For non-parallel jobs, leave unset. When unset, the default is 1.

    4Optional: Specifies how many successful pod completions are needed to mark a job completed.
    • For non-parallel jobs, leave unset. When unset, the default is 1.

    • For parallel jobs with a fixed completion count, specify the number of completions.

    • For parallel jobs with a work queue, leave unset. When unset the default is the value of the parallelism parameter.

    5Specifies the template for the pod the controller creates.
    6Optional: Specifies the maximum number of replicas when scaling up. The default is 100.
    7Optional: Specifies the interval in seconds to check each trigger on. The default is 30.
    8Optional: Specifies the number of successful finished jobs should be kept. The default is 100.
    9Optional: Specifies how many failed jobs should be kept. The default is 100.
    10Optional: Specifies the name of the container in the target resource, from which the custom autoscaler gets environment variables holding secrets and so forth. The default is .spec.template.spec.containers[0].
    11Optional: Specifies whether existing jobs are terminated whenever a scaled job is being updated:
    • default: The autoscaler terminates an existing job if its associated scaled job is updated. The autoscaler recreates the job with the latest specs.

    • gradual: The autoscaler does not terminate an existing job if its associated scaled job is updated. The autoscaler creates new jobs with the latest specs.

    12Optional: Specifies a scaling strategy: default, custom, or accurate. The default is default. For more information, see the link in the “Additional resources” section that follows.
    13Specifies the trigger to use as the basis for scaling, as described in the “Understanding the custom metrics autoscaler triggers” section.
    14Optional: Specifies a trigger authentication, as described in the “Creating a custom metrics autoscaler trigger authentication” section.
    15Optional: Specifies a cluster trigger authentication, as described in the “Creating a custom metrics autoscaler trigger authentication” section.

    It is not necessary to specify both a namespace trigger authentication and a cluster trigger authentication.

  2. Create the custom metrics autoscaler:

    1. $ oc create -f <file-name>.yaml

Verification

  • View the command output to verify that the custom metrics autoscaler was created:

    1. $ oc get scaledjob <scaled_job_name>

    Example output

    1. NAME MAX TRIGGERS AUTHENTICATION READY ACTIVE AGE
    2. scaledjob 100 prometheus prom-triggerauthentication True True 8s

    Note the following fields in the output:

  • TRIGGERS: Indicates the trigger, or scaler, that is being used.

  • AUTHENTICATION: Indicates the name of any trigger authentication being used.

  • READY: Indicates whether the scaled object is ready to start scaling:

    • If True, the scaled object is ready.

    • If False, the scaled object is not ready because of a problem in one or more of the objects you created.

  • ACTIVE: Indicates whether scaling is taking place:

    • If True, scaling is taking place.

    • If False, scaling is not taking place because there are no metrics or there is a problem in one or more of the objects you created.

Additional resources

Uninstalling the Custom Metrics Autoscaler Operator

You can remove the custom metrics autoscaler from your OKD cluster. After removing the Custom Metrics Autoscaler Operator, remove other components associated with the Operator to avoid potential issues.

You should delete the KedaController custom resource (CR) first. If you do not specifically delete the CR, OKD can hang when you delete the openshift-keda project. If you delete the Custom Metrics Autoscaler Operator before deleting the CR, you are not able to delete the CR.

Prerequisites

  • The Custom Metrics Autoscaler Operator must be installed.

Procedure

  1. In the OKD web console, click OperatorsInstalled Operators.

  2. Switch to the openshift-keda project.

  3. Remove the KedaController custom resource.

    1. Find the CustomMetricsAutoscaler Operator and click the KedaController tab.

    2. Find the custom resource, and then click Delete KedaController.

    3. Click Uninstall.

  4. Remove the Custom Metrics Autoscaler Operator:

    1. Click OperatorsInstalled Operators.

    2. Find the CustomMetricsAutoscaler Operator and click the Options menu kebab and select Uninstall Operator.

    3. Click Uninstall.

  5. Optional: Use the OpenShift CLI to remove the custom metrics autoscaler components:

    1. Delete the custom metrics autoscaler CRDs:

      • clustertriggerauthentications.keda.sh

      • kedacontrollers.keda.sh

      • scaledjobs.keda.sh

      • scaledobjects.keda.sh

      • triggerauthentications.keda.sh

      1. $ oc delete crd clustertriggerauthentications.keda.sh kedacontrollers.keda.sh scaledjobs.keda.sh scaledobjects.keda.sh triggerauthentications.keda.sh

      Deleting the CRDs removes the associated roles, cluster roles, and role bindings. However, there might be a few cluster roles that must be manually deleted.

    2. List any custom metrics autoscaler cluster roles:

      1. $ oc get clusterrole | grep keda.sh
    3. Delete the listed custom metrics autoscaler cluster roles. For example:

      1. $ oc delete clusterrole.keda.sh-v1alpha1-admin
    4. List any custom metrics autoscaler cluster role bindings:

      1. $ oc get clusterrolebinding | grep keda.sh
    5. Delete the listed custom metrics autoscaler cluster role bindings. For example:

      1. $ oc delete clusterrolebinding.keda.sh-v1alpha1-admin
  6. Delete the custom metrics autoscaler project:

    1. $ oc delete project openshift-keda
  7. Delete the Cluster Metric Autoscaler Operator:

    1. $ oc delete operator/openshift-custom-metrics-autoscaler-operator.openshift-keda