$ oc new-project <namespace>
You can use the HTTP Add-on to extend the Custom Metrics Autoscaler Operator to enable automatic scaling of HTTP-based applications based on incoming traffic metrics.
With the HTTP Add-on, you can scale applications from zero replicas during low traffic periods to peak capacity when demand returns.
The HTTP Add-on scales a workload based on HTTP traffic without changing application code. An interceptor proxy counts requests, and a scaler reports those counts to the Custom Metrics Autoscaler. You set targetValue on the InterceptorRoute custom resource to control when the horizontal pod autoscaler adds replicas.
The HTTP Add-on extends the Custom Metrics Autoscaler Operator to automatically scale HTTP-traffic-based applications on incoming request metrics to increase or decrease replicas as needed to meet demand.
During periods of low HTTP traffic, the scaler decreases applications down to zero replicas or to the the minReplicaCount value you set. When requests arrive the application gets scaled up from zero up to maxReplicaCount value you set.
The HTTP Add-on is a specialized extension that integrates with the Custom Metrics Autoscaler Operator to provide HTTP-traffic-based scaling for containerized applications. Unlike traditional horizontal pod autoscaling (HPA) that requires persistent metrics collection, the HTTP Add-on uses an interceptor proxy to observe HTTP traffic in real time and make intelligent scaling decisions.
The HTTP Add-on consists of several components deployed in the openshift-keda namespace:
Interceptor. The proxy that sits between your traffic source and application, collecting HTTP metrics.
Scaler. Analyzes interceptor metrics and provides scaling decisions to the Custom Metrics Autoscaler Operator.
Operator. Manages the lifecycle of interceptor and scaler resources based on your scaling configuration.
Custom Metrics Adapter. Part of the Custom Metrics Autoscaler Operator that exposes interceptor metrics to the Kubernetes metrics API so the HPA can consume them.
To configure HTTP-traffic-based scaling, you create two custom resources for each application, in addition to an edge route and service:
InterceptorRoute: Defines how the HTTP Add-on intercepts and collects traffic for your application. The interceptor route specifies routing rules, scaling metrics configuration, and the target application service.
ScaledObject: Defines how the Custom Metrics Autoscaler uses HTTP traffic metrics to scale your application.
The HTTP Add-on supports the autoscaler activation and scaling phases, where the Cluster Metrics Autoscaler Operator handles scaling between zero and one replica. After the application has at least one replica, the HPA takes control and scales the application based on your configured metrics.
When traffic arrives at your application, the interceptor measures request volume and concurrency. Based on configurable thresholds, the scaler determines whether to scale from zero to one replica or allow the HPA to scale to a higher replica count. When traffic drops below the activation threshold, the application scales back to zero, reducing infrastructure costs.
You can set minReplicaCount to 0 in the ScaledObject custom resource so that the application scales to zero replicas after requests stop. Any request causes the Custom Metrics Autoscaler Operator to scale the application from zero to one replica. Set the targetValue parameter in the InterceptorRoute custom resource to control when the horizontal pod autoscaler adds more replicas, up to maxReplicaCount in the ScaledObject custom resource.
For example, with the targetValue parameter set to 100 in the InterceptorRoute CR and the maxReplicaCount parameter set to 10 in the ScaledObject CR, the first request starts one pod. The autoscaler adds pods while the rate stays above 100 requests per second per pod, and it stops at 10 pods.
For more information about the HTTP Add-on, see the KEDA HTTP Add-on documentation.
After installing the HTTP Add-on, you configure per-application autoscaling by creating an InterceptorRoute custom resource (CR) to define traffic routing and scaling metrics and a ScaledObject custom resource to link your scaling configuration to your application deployment.
The HTTP Add-on is installed in your cluster. For more information, see "Install the KEDA HTTP Add-on extension".
You have access to a terminal with the oc command-line tool installed and authenticated to your OKD cluster.
You have permissions to create deployment, service, route, interceptor route, and scaled object custom resources in the default namespace.
Create a namespace for your application by running the following command:
$ oc new-project <namespace>
Replace <namespace> with a name for your namespace.
Create your HTTP application deployment.
Create a file with the content required to launch your application, similar to the following example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-http-app
namespace: http-add-on-example
spec:
replicas: 1
selector:
matchLabels:
app: sample-http-app
template:
metadata:
labels:
app: sample-http-app
spec:
containers:
- name: app
image: openshift/hello-openshift
ports:
- containerPort: 8080
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
Create the deployment:
$ oc create -f <file_name>.yaml
Verify the deployment is operating by running the following command:
$ oc get pods -n <namespace>
Replace <namespace> with the name of the namespace where you created the deployment.
NAME READY STATUS RESTARTS AGE
sample-http-app-5fc8679bf4-prb46 1/1 Running 0 6m57s
Create a service to expose the application:
Create a file with content similar to the following example:
apiVersion: v1
kind: Service
metadata:
name: sample-http-app
namespace: http-add-on-example
spec:
selector:
app: sample-http-app
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
where:
name.namespace specifies the namespace where you deployed your application.
spec.selector specifies which pods the service should route traffic to. This value must match the matchLabels specified in the deployment.
Create the service:
$ oc create -f <file_name>.yaml
Verify that the service was created by running the following command:
$ oc get service -n <namespace>
Replace <namespace> with the name of the namespace where you created the service.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sample-http-app ClusterIP 172.30.154.127 <none> 8080/TCP 6m19s
Create an edge route to expose the application externally.
Create the route in the openshift-keda namespace by running the following command:
oc create route edge <name> \
--service=keda-add-ons-http-interceptor-proxy \
--port=proxy \
-n openshift-keda
where:
<name> specifies a name for your route. Replace <name> with a name for your route.
service=keda-add-ons-http-interceptor-proxy specifies the service you want to expose. This must be keda-add-ons-http-interceptor-proxy.
port=proxy specifies the port on the keda-add-ons-http-interceptor-proxy service that the route uses. Specifying proxy sets the port to 8080. With an edge route, the port must be 8080.
openshift-keda specifies the namespace where your route will be created. This must be openshift-keda.
Leave the spec.host parameter unset to allow OKD to assign the host name on your cluster’s *.apps domain.
Get the route host name generated by OKD for use in the following step by running the following command:
$ oc get route <route_name> -n openshift-keda -o jsonpath={.spec.host}
Replace <route_name> with the name of the route.
sample-http-app-openshift-keda.apps.ci-ln-wszd8jt-72292.origin-ci-int-gce.dev.rhcloud.com
Create an InterceptorRoute custom resource to intercept traffic:
Create a file with content similar to the following example:
apiVersion: http.keda.sh/v1beta1
kind: InterceptorRoute
metadata:
name: sample-app-interceptor
namespace: http-add-on-example
spec:
rules:
- hosts:
- sample-http-app-openshift-keda.apps.ci-ln-74rphxb-72292.gcp-3.ci.openshift.org
target:
service: sample-http-app
port: 8080
scalingMetric:
requestRate:
targetValue: 5
window: 10s
granularity: 1s
Modify the following fields for your application:
metadata.name specifies a descriptive name for this interceptor route.
metadata.namespace specifies the namespace where you deployed your application.
spec.rules.hosts specifies the host name where traffic arrives for your application from your route or ingress. Use the generated route host name from the previous step.
spec.target.service specifies the name of your application’s service.
spec.target.port specifies the port exposed by the service. For example, if the service exposes port 80 and forwards to targetPort: 8080, the interceptor route must use port 80.
spec.scalingMetric specifies the metric that drives autoscaling. Set requests per second (requestRate), concurrent requests (concurrency), or both. If you set both, scaling uses whichever metric requires more replicas.
requestRate measures the number of requests per second, averaged over a sliding time window. You can set the following parameters:
targetValue specifies the target number of requests per second for each replica. The horizontal pod autoscaler (HPA) adds replicas when the average request rate for each pod rises above this value.
window specifies the sliding time window used to calculate the average request rate. The default is 1 minute.
granularity specifies the bucket size within that window. The default is 1 second. A smaller value makes scaling more responsive and more sensitive to short bursts of traffic.
concurrency measures the number of in-flight HTTP requests. Set targetValue to the target number of concurrent requests for each replica. The HPA adds replicas when the number of in-flight requests for each pod rises above this value. concurrency uses only targetValue.
Create the interceptor route by running the following command:
$ oc create -f <file_name>.yaml
Verify that the interceptor route was created by running the following command:
$ oc get interceptorroute <route_name> -n <namespace_name>
Replace <route_name> with the name of the interceptor route and <namespace_name> with the namespace where you created the interceptor route.
NAME TARGETSERVICE READY AGE
sample-app-interceptor sample-http-app True 11s
Wait until the route interceptor is the READY:True state.
Create a scaled object to configure scaling:
Create a file with content similar to the following example:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: sample-app-scaler
namespace: http-add-on-example
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: sample-http-app
minReplicaCount: 0
maxReplicaCount: 10
cooldownPeriod: 30
triggers:
- type: external-push
metadata:
scalerAddress: keda-add-ons-http-external-scaler.openshift-keda.svc:9090
interceptorRoute: sample-app-interceptor
Modify the following fields for your application:
metadata.name specifies a descriptive name for this scaled object.
metadata.namespace specifies the namespace where your application is deployed. Must be the same namespace as the interceptor route.
spec.scaleTargetRef.apiVersion specifies the API version for the object you used to create your application pod. The default is apps/v1.
spec.scaleTargetRef.kind specifies the object kind, either Deployment, StatefulSet or CustomResource.
spec.scaleTargetRef.name specifies the exact name of the object.
spec.minReplicaCount specifies the minimum replicas your application should scale down to.
spec.maxReplicaCount specifies the maximum replicas your application should scale up to.
spec.cooldownPeriod specifies the time in seconds to wait before scaling to zero after traffic drops.
spec.triggers.type specifies the underlying core metric scaler engine that watches your workload. Must be external-push.
spec.triggers.metadata.scalerAddress specifies the endpoint for the HTTP Add-on scaler service. Must be keda-add-ons-http-external-scaler.openshift-keda.svc:9090.
spec.triggers.metadata.interceptorRoute specifies a name for your interceptor route.
Create the scaled object by running the following command:
$ oc apply -f <file_name>.yaml
Verify that the scaled object is created by running the following command:
$ oc get scaledobject <object_name> -n <namespace_name>
Replace <object_name> with the name of the scaled object and <namespace_name> with the namespace where you created the interceptor route.
NAME SCALETARGETKIND SCALETARGETNAME MIN MAX READY ACTIVE FALLBACK PAUSED TRIGGERS AUTHENTICATIONS AGE
sample-app-scaler apps/v1.Deployment sample-http-app 0 10 True Unknown False False external-push 12s
For more information on scaled objects, see "Add a custom metrics autoscaler to a workload".
Verify that the autoscaling configuration is active:
Verify that the deployment currently has zero replicas by running the following command:
$ oc get deployment <deployment_name> -n <namespace_name>
Replace <deployment_name> with the name of the deployment and <namespace_name> with the namespace where you created the deployment.
NAME READY UP-TO-DATE AVAILABLE AGE
sample-http-app 0/0 0 0 5m
There are no replicas because there is no traffic going to the application.
Check that the scaled object create an HPA by running the following command:
$ oc get hpa -n <namespace_name>
Replace <namespace_name> with the namespace where you created the scaled object.
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
keda-hpa-sample-app-scaler Deployment/sample-http-app <unknown>/5 (avg) 1 10 0 117s
The target rate is unknown because the deployment has no replicas.
Generate traffic to observe autoscaling behavior, for example by running the following script:
Run the following example script to generate HTTP traffic to your cluster. This creates concurrent requests that can be monitored in your metrics dashboard:
for i in $(seq 1 30); do
for j in $(seq 1 20); do
curl -sk "https://<host_name>/" > /dev/null &
done
wait
done
The curl command in this example sends one HTTP request to the route and prints the response, generating 600 total HTTP requests, in 30 batches with 20 concurrent requests per batch, to populate performance metrics.
Watch the deployment scale up in response to the traffic by running the following command:
$ oc get deployment <deployment_name> -n <namespace_name>
Replace <deployment_name> with the name of the deployment and <namespace_name> with the namespace where you created the deployment.
NAME READY UP-TO-DATE AVAILABLE AGE
sample-http-app 6/6 6 6 26m
You should see the READY and AVAILABLE fields increase. In this example, the deployment scaled up to 6 replicas.
Check the HPA status to see the current metrics and scaling activity by running the following command:
$ oc get hpa -n <namespace>
Replace <namespace> with the namespace where you created the scaled object.
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
keda-hpa-sample-app-scaler Deployment/sample-http-app 6500m/5 (avg) 1 10 6 15m
In this example, the HPA target rate is 6500m/5, which represents 6.5 requests per second per pod against your target of 5 in the interceptor route, so the deployment scaled to 6 replicas.
After traffic stops, wait for the 30-second cooldown period, then check the scaled object status by running the following command:
$ oc get deployment <deployment_name> -n <namespace_name>
Replace <deployment_name> with the name of the deployment and <namespace_name> with the namespace where you created the deployment.
NAME READY UP-TO-DATE AVAILABLE AGE
sample-http-app 0/0 0 0 27m
The HPA scales the replicas down to the minReplicaCount value in the scaled object. The Custom Metrics Autoscaler then scales the replicas down to 0.