Scaling Keycloak instances with Kubernetes Horizontal Pod Autoscaler
TypeScriptLet's create a Pulumi program in TypeScript to configure a Kubernetes Horizontal Pod Autoscaler (HPA) for a set of Keycloak instances. The HPA will automatically scale the number of Keycloak pods up or down based on the CPU utilization or custom metrics.
The
kubernetes.autoscaling.v2.HorizontalPodAutoscaler
resource is used to configure the HPA. This resource allows us to define metrics that will trigger the scaling, including CPU and memory utilization, and custom metrics provided by Kubernetes metrics APIs. We will focus on CPU utilization for this example.The important elements in the HPA resource are:
scaleTargetRef
: Specifies the target resource that this HPA will affect. It should be the Deployment or ReplicaSet managing the Keycloak pods.minReplicas
andmaxReplicas
: Define the minimum and maximum number of pods that the HPA can scale to.metrics
: A list of metrics to be used to calculate the desired replica count.
Here's a sample Pulumi program in TypeScript that sets up an HPA for Keycloak:
import * as k8s from "@pulumi/kubernetes"; const keycloakDeploymentName = "keycloak-deployment"; // The name of your Keycloak deployment const keycloakNamespace = "keycloak-namespace"; // The namespace where your Keycloak is deployed // Create a HorizontalPodAutoscaler to automatically scale Keycloak based on CPU utilization const keycloakHpa = new k8s.autoscaling.v2.HorizontalPodAutoscaler("keycloak-hpa", { metadata: { namespace: keycloakNamespace, }, spec: { scaleTargetRef: { apiVersion: "apps/v1", kind: "Deployment", name: keycloakDeploymentName, }, minReplicas: 1, maxReplicas: 10, metrics: [{ type: "Resource", resource: { name: "cpu", target: { type: "Utilization", averageUtilization: 50, // Target CPU utilization percentage that triggers scaling }, }, }], }, }, { provider: /* your Kubernetes provider here */ }); // Export the name of the HPA export const hpaName = keycloakHpa.metadata.name;
Before applying this code with Pulumi, you should replace placeholders with actual values:
keycloakDeploymentName
with the name of your Keycloak deployment resource,keycloakNamespace
with the namespace where your Keycloak instances are running,- Optionally, provide your Kubernetes provider if you're using a Pulumi Kubernetes provider configuration other than the default.
The
averageUtilization: 50
sets the target average CPU utilization at which the HPA will start creating new pods. If the CPU utilization goes above 50%, the scaler will add more replicas, and if it goes below, it will remove replicas, respecting the limits ofminReplicas
andmaxReplicas
.After creating and applying the HPA, Kubernetes will monitor the CPU utilization of your Keycloak pods and automatically adjust the number of replicas to maintain the target utilization rate.
To apply this Pulumi program:
- Save the code to a file with a
.ts
extension, for example,keycloakHpa.ts
. - Run
pulumi up
in the command line within the same directory as your file.
The Pulumi CLI will then execute the script and apply the changes to your Kubernetes cluster, setting up the HPA for your Keycloak instances.