1. Answers
  2. Monitoring Kubernetes Cluster Performance with Elastic APM

How do I monitor Kubernetes cluster performance with Elastic APM?

In this guide, we will set up monitoring for a Kubernetes cluster using Elastic APM. Elastic APM is a powerful tool for monitoring the performance of applications and infrastructure. We’ll use Pulumi to automate the deployment and configuration of the necessary resources.

Key Points:

  • We will deploy a Kubernetes cluster.
  • We will set up Elastic APM to monitor the performance of the cluster.
  • We will configure the necessary resources and permissions.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a Kubernetes cluster
const cluster = new k8s.core.v1.Namespace("elastic-apm-namespace", {
    metadata: {
        name: "elastic-apm",
    },
});

// Deploy Elastic APM Server
const apmServer = new k8s.apps.v1.Deployment("apm-server", {
    metadata: {
        namespace: cluster.metadata.name,
    },
    spec: {
        replicas: 1,
        selector: {
            matchLabels: {
                app: "apm-server",
            },
        },
        template: {
            metadata: {
                labels: {
                    app: "apm-server",
                },
            },
            spec: {
                containers: [{
                    name: "apm-server",
                    image: "docker.elastic.co/apm/apm-server:7.14.0",
                    ports: [{
                        containerPort: 8200,
                    }],
                    env: [
                        {
                            name: "ELASTIC_APM_SERVER_URL",
                            value: "http://localhost:8200",
                        },
                        {
                            name: "ELASTIC_APM_SECRET_TOKEN",
                            value: "your_secret_token",
                        },
                    ],
                }],
            },
        },
    },
});

// Expose APM Server as a Service
const apmService = new k8s.core.v1.Service("apm-service", {
    metadata: {
        namespace: cluster.metadata.name,
        labels: apmServer.spec.template.metadata.labels,
    },
    spec: {
        ports: [{
            port: 8200,
            targetPort: 8200,
        }],
        selector: apmServer.spec.template.metadata.labels,
    },
});

// Deploy APM Agent to monitor the cluster
const apmAgent = new k8s.apps.v1.Deployment("apm-agent", {
    metadata: {
        namespace: cluster.metadata.name,
    },
    spec: {
        replicas: 1,
        selector: {
            matchLabels: {
                app: "apm-agent",
            },
        },
        template: {
            metadata: {
                labels: {
                    app: "apm-agent",
                },
            },
            spec: {
                containers: [{
                    name: "apm-agent",
                    image: "docker.elastic.co/apm/apm-agent:7.14.0",
                    env: [
                        {
                            name: "ELASTIC_APM_SERVER_URL",
                            value: "http://apm-service.elastic-apm.svc.cluster.local:8200",
                        },
                        {
                            name: "ELASTIC_APM_SECRET_TOKEN",
                            value: "your_secret_token",
                        },
                    ],
                }],
            },
        },
    },
});

// Export the APM Server URL
export const apmServerUrl = apmService.status.loadBalancer.ingress[0].hostname;

Summary

In this guide, we deployed a Kubernetes cluster and set up Elastic APM to monitor its performance. We created a namespace for Elastic APM, deployed the APM Server, exposed it as a service, and deployed an APM Agent to collect performance data. This setup helps in monitoring the health and performance of your Kubernetes cluster using Elastic APM.

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