Using kubernetes keda.sh with access
TypeScriptTo use Kubernetes-based Event-driven Autoscaling (KEDA) with access to particular resources, you would typically need to install the KEDA components on your Kubernetes cluster and configure Scalable Object (ScaledObject) resources which define how your applications should scale.
Here is a step-by-step program in TypeScript using Pulumi to deploy KEDA to your Kubernetes cluster along with a simple example of a ScaledObject.
Prerequisites
Before you begin, make sure that you have:
- Installed Pulumi CLI and set up your Pulumi account.
- Configured your preferred cloud provider CLI with Kubernetes access (e.g.,
kubectl
for generic Kubernetes,aws
for EKS,gcloud
for GKE, etc.). - A Kubernetes cluster already running.
Step 1: Import Required Pulumi Packages
To work with Kubernetes resources, we need to import the necessary Pulumi package for Kubernetes.
import * as k8s from "@pulumi/kubernetes";
Step 2: Set up KEDA Namespace
We'll create a separate namespace for KEDA components to keep things organized.
const kedaNamespace = new k8s.core.v1.Namespace("keda", { metadata: { name: "keda" }, });
Step 3: Deploy KEDA
Now, we deploy KEDA using its Helm chart. Helm charts simplify the deployment and management of applications on Kubernetes.
const kedaChart = new k8s.helm.v3.Chart("keda", { chart: "keda", version: "2.0.0", // Use the appropriate version namespace: kedaNamespace.metadata.name, fetchOpts: { repo: "https://kedacore.github.io/charts", }, }, { dependsOn: [kedaNamespace] });
Step 4: Define a Kubernetes Deployment
We will define a simple deployment that KEDA will scale. This is just a placeholder for your actual application.
const appLabels = { app: "hello-keda" }; const deployment = new k8s.apps.v1.Deployment("hello-keda-deployment", { metadata: { namespace: kedaNamespace.metadata.name, }, spec: { selector: { matchLabels: appLabels, }, replicas: 1, template: { metadata: { labels: appLabels, }, spec: { containers: [{ name: "hello-keda", image: "nginx", // Replace with your actual application container image }], }, }, }, }, { dependsOn: [kedaChart] });
Step 5: Define a ScaledObject
The
ScaledObject
resource is a custom resource defined by KEDA to configure autoscaling based on events. Here is a simple example of aScaledObject
that scales a deployment based on a hypothetical metric.const scaledObject = new k8s.apiextensions.CustomResource("hello-keda-scaledobject", { apiVersion: "keda.sh/v1alpha1", kind: "ScaledObject", metadata: { namespace: kedaNamespace.metadata.name, labels: appLabels, }, spec: { scaleTargetRef: { name: deployment.metadata.name, }, triggers: [ { type: "http", metadata: { // These are example values; they must be replaced with actual trigger specifications scalerAddress: "http://example-scaler-service", }, }, ], }, }, { dependsOn: [deployment] });
Maintain the
triggers
section consistent with the event source your application uses, such as a queue or a stream processor; refer to the KEDA documentation for specifics on how to configure triggers for each supported scaler.Complete Program
Putting it all together, the full Pulumi program in TypeScript would look like this:
import * as pulumi from "@pulumi/pulumi"; import * as k8s from "@pulumi/kubernetes"; // Step 1: Import Required Pulumi Packages is already done above. // Step 2: Set up KEDA Namespace code here // Step 3: Deploy KEDA code here // Step 4: Define a Kubernetes Deployment code here // Step 5: Define a ScaledObject code here
Save this file with a
.ts
extension, and then execute it with Pulumi by running the following commands:pulumi up
The
pulumi up
command will create or update resources in your Kubernetes cluster to match the desired state defined in the program. Once executed, your cluster will have a running instance of KEDA, ready to automatically scale your example deployment based on configured triggers.Please replace placeholders and example values with the appropriate details matching your use case and environment.