Using kubernetes http.keda.sh with gateway.envoyproxy.io
TypeScriptTo use Kubernetes with HTTP-triggered scaling (KEDA) and an Envoy proxy gateway, you would typically follow these steps:
- Deploy KEDA to your Kubernetes cluster to allow for event-driven autoscaling of your Pods.
- Set up an Envoy proxy within your Kubernetes cluster, which will handle incoming HTTP traffic and route it to your services.
- Define ScaledObjects that tell KEDA how to scale your deployment based on the HTTP traffic coming through the Envoy proxy.
The following TypeScript program for Pulumi will demonstrate how to set up a Kubernetes deployment that can be autoscaled with KEDA using HTTP traffic through an Envoy proxy. We'll be using the
http.keda.sh
CustomResourceDefinition (CRD) provided by KEDA to scale our application based on the rate of HTTP requests it receives.First, you need to have the KEDA and Envoy proxy installed on your Kubernetes cluster. We're going to assume these are already set up for the purpose of this demonstration.
Let's write the TypeScript Pulumi program:
import * as k8s from "@pulumi/kubernetes"; const appName = "http-autoscaled-app"; // Create a Kubernetes Deployment for our application const appLabels = { app: appName }; const appDeployment = new k8s.apps.v1.Deployment(appName, { metadata: { labels: appLabels, }, spec: { replicas: 1, selector: { matchLabels: appLabels }, template: { metadata: { labels: appLabels, }, spec: { containers: [ { name: appName, image: "nginx", // This image would be your application that you want to autoscale. ports: [{ containerPort: 80 }], }, ], }, }, }, }); // Create a Service to expose the Deployment const appService = new k8s.core.v1.Service(appName, { metadata: { labels: appLabels, }, spec: { ports: [{ port: 80, targetPort: 80 }], selector: appLabels, type: "ClusterIP", }, }); // Define a ScaledObject for KEDA to autoscale the app based on HTTP traffic const httpScaledObject = new k8s.apiextensions.CustomResource(appName, { apiVersion: "keda.sh/v1alpha1", kind: "ScaledObject", metadata: { name: appName, labels: appLabels, }, spec: { scaleTargetRef: { kind: "Deployment", name: appDeployment.metadata.name, }, // This is where you configure the trigger. // You need to setup an external metric server first, which can be done with // an Envoy proxy statsd sink and a Prometheus server which scrubs the statsd metrics triggers: [ { type: "external", metadata: { // The URL of the metric service exposing metrics for HPA (horizontal pod autoscaler) scalerAddress: "<YOUR_METRIC_SERVER_URL>", }, }, ], }, }); // Export the Service name and Cluster IP export const appName = appService.metadata.name; export const appClusterIP = appService.spec.clusterIP;
This program sets up a Deployment and Service for an Nginx server (which would be a placeholder for your actual application) and a ScaledObject to enable KEDA-based autoscaling. The
triggers
section of the ScaledObject is configured for an external metric (in this case, HTTP traffic) that is monitored by a metric server. ThescalerAddress
is a placeholder for the URL of the metric service, which you would need to replace with your actual metrics service URL.You would need to set up an Envoy proxy to collect HTTP metrics and a Prometheus server to serve these metrics in a way KEDA can use to make scaling decisions.
For more information on KEDA ScaledObjects, you can check the ScaledObjects in the Pulumi Registry. For Envoy, refer to the Envoy documentation for setup instructions.
Remember, before running this Pulumi program, install
@pulumi/kubernetes
package using npm or yarn, and set up a Kubernetes cluster with KEDA and an Envoy proxy.