1. Answers
  2. Auto-scaling Kubernetes pods based on Dynatrace performance metrics

How do I auto-scale Kubernetes pods based on Dynatrace performance metrics?

In this guide, we will set up auto-scaling for Kubernetes pods based on performance metrics provided by Dynatrace. We will use Pulumi to define a HorizontalPodAutoscaler resource that scales the number of pods in a deployment according to the metrics collected by Dynatrace.

Key Points:

  • We will create a Kubernetes Deployment.
  • We will set up a Horizontal Pod Autoscaler (HPA) that uses custom metrics from Dynatrace.
  • The HPA will scale the number of pods based on the performance metrics.

Detailed Steps:

  1. Create a Kubernetes Deployment: This deployment will run a sample application.
  2. Set up Horizontal Pod Autoscaler: The HPA will use Dynatrace metrics to determine when to scale the number of pods.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("dynatrace-namespace");

// Create a Kubernetes Deployment
const appLabels = { app: "my-app" };
const deployment = new k8s.apps.v1.Deployment("my-app-deployment", {
    metadata: {
        namespace: namespace.metadata.name,
    },
    spec: {
        selector: { matchLabels: appLabels },
        replicas: 1,
        template: {
            metadata: { labels: appLabels },
            spec: {
                containers: [{
                    name: "my-app",
                    image: "nginx",
                    ports: [{ containerPort: 80 }],
                }],
            },
        },
    },
});

// Create a Horizontal Pod Autoscaler
const hpa = new k8s.autoscaling.v2.HorizontalPodAutoscaler("my-app-hpa", {
    metadata: {
        namespace: namespace.metadata.name,
    },
    spec: {
        scaleTargetRef: {
            apiVersion: "apps/v1",
            kind: "Deployment",
            name: deployment.metadata.name,
        },
        minReplicas: 1,
        maxReplicas: 10,
        metrics: [{
            type: "External",
            external: {
                metric: {
                    name: "dynatrace_custom_metric",
                    selector: {
                        matchLabels: {
                            dynatrace: "custom-metric",
                        },
                    },
                },
                target: {
                    type: "Value",
                    value: "100",
                },
            },
        }],
    },
});

// Export the namespace and deployment names
export const namespaceName = namespace.metadata.name;
export const deploymentName = deployment.metadata.name;

Conclusion:

In this guide, we created a Kubernetes deployment and set up a Horizontal Pod Autoscaler using Dynatrace performance metrics. The HPA scales the number of pods in the deployment based on custom metrics collected by Dynatrace, ensuring optimal resource utilization and performance. This setup helps in maintaining the desired performance levels of your application by dynamically adjusting the number of running pods.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up