# nitesh8011/hpa-keda architecture

> Architecture diagram of the GitHub repository nitesh8011/hpa-keda, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nitesh8011/hpa-keda
- Repository: https://github.com/nitesh8011/hpa-keda

## Overview

##### Project type and purpose
- This is a Kubernetes‐based event‐driven autoscaling demo that wires up a Spring Boot application (packaged as a JAR and containerized via the Dockerfile) with KEDA (Kubernetes Event‐Driven Autoscaling).
- Its core aim is to show how external metrics (HTTP load, Prometheus metrics, etc.) drive pod scaling through KEDA’s ScaledObject/ScaledJob CRDs.

##### File structure
- 001_operator/ – Kustomize overlays to install the KEDA Operator (OperatorGroup, Subscription, namespace, CRDs).
- 002_metrics/ – Kustomize overlays for the demo metrics setup:
– ScaledObject.yaml (defines how the Spring Boot Deployment is scaled)
– TriggerAuthentication.yaml (credentials for external metrics sources)
– ServiceMonitor.yaml (Prometheus scrape config)
– RBAC, Secret, namespace, etc.
- Dockerfile & spring-boot-prometheus-grafana-0.0.1-SNAPSHOT.jar – the application image containing Prometheus instrumentation.
- load.yaml – a load generator (e.g., a Job or Pod that pumps HTTP requests) to trigger scaling.
- METRICS.md – documentation on which Prometheus metrics the app exposes.

##### README for additional insights
- KEDA Operator watches ScaledObject/ScaledJob CRDs and feeds custom metrics to Kubernetes’ HPA.
- The Metrics Server component implements the external metrics API for HPA.
- Scalers (plugins) poll external systems (queue, HTTP endpoint, database).
- When thresholds are met, HPA adjusts replica counts; KEDA can scale to zero.

a. Main components:
– Kubernetes Cluster
– Namespaces: “keda-operator” & “demo-metrics”
– KEDA Operator (Controller)
– KEDA Metrics Server (External Metrics API)
– Scaler plugin(s) (e.g., HTTP request rate scaler)
– Spring Boot Application Deployment (Pods) with Prometheus exporter
– Prometheus Server & ServiceMonitor
– Grafana (optional, for dashboards)
– Horizontal Pod Autoscaler (HPA)
– Load generator (Pod/Job from load.yaml)
– Container image registry (for Dockerfile image)

b. Relationships and data flows:
1. Load generator → sends HTTP requests to Spring Boot Service.
2. Spring Boot pods → expose /metrics; ServiceMonitor → Prometheus scrapes metrics.
3. Scaler plugin queries Prometheus (via TriggerAuthentication Secret).
4. Scaler → provides custom metric to KEDA Metrics Server.
5. KEDA Metrics Server → surface metric through Kubernetes External Metrics API.
6. HPA → fetches metric, adjusts replicas on the Deployment.
7. KEDA Operator → reconciles ScaledObject CRD, ensures HPA is configured correctly and watching the right metric.

c. Architectural patterns/design principles:
– Control loop (Kubernetes Operator pattern)
– Custom Resource Definitions extend Kubernetes API (ScaledObject, TriggerAuthentication)
– Separation of concerns: Operator vs. Metrics Server vs. Scaler vs. HPA vs. Application
– Namespace isolation for operator vs. workloads

d. Key technologies/frameworks:
– Kubernetes CRDs & Operators (Operator SDK)
– Kustomize overlays for environment‐specific configs
– Horizontal Pod Autoscaler with custom/external metrics
– Prometheus & ServiceMonitor (Prometheus Operator)
– Spring Boot with Micrometer/Prometheus exporter
– Docker for container image
– Grafana for visualization

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Kubernetes Cluster"
        subgraph "001_operator Overlay"
            kustom001["kustomization.yaml"]:::config
            namespace001["namespace.yaml"]:::config
            opergroup["operatorgroup.yaml"]:::config
            subscript["subscription.yaml"]:::config
            kedacont["kedacontroller.yaml"]:::config
        end

        subgraph "002_metrics Overlay"
            kustom002["kustomization.yaml"]:::config
            namespace002["namespace.yaml"]:::config
            appcfg["application.yaml"]:::config
            socfg["scaledobject.yaml"]:::config
            tacfg["triggerauthentication.yaml"]:::config
            smcfg["servicemonitor.yaml"]:::config
            rbaccfg["rbac.yaml"]:::config
            secretcfg["secret.yaml"]:::config
        end

        subgraph "Container Image Build"
            dockerf["Dockerfile"]:::config
            jar["spring-boot-prometheus-grafana-0.0.1-SNAPSHOT.jar"]:::config
        end

        loadcfg["load.yaml"]:::config
        docs["README.md & METRICS.md"]:::config

        namespace001 --> KO[KEDA Operator]:::operator
        opergroup --> KO
        subscript --> KO
        kedacont --> KO
        kustom001 --> namespace001

        KO --> KM[KEDA Metrics Server]:::operator
        KM --> SOCRD[ScaledObject CRD]:::crd
        KM --> TACRD[TriggerAuthentication CRD]:::crd

        namespace002 --> SB[Spring Boot Deployment]:::workload
        appcfg --> SB
        dockerf --> SB
        jar --> SB

        namespace002 --> MON[Prometheus Server]:::external
        smcfg --> MON
        MON -->|"scrapes /metrics"| SB

        loadcfg --> LG[Load Generator Pod]:::external
        LG -->|"HTTP requests"| SB

        socfg --> SP[Scaler Plugin]:::operator
        tacfg --> SP
        SP -->|"queries metrics"| MON
        SP --> KM

        socfg --> HPA[Horizontal Pod Autoscaler]:::crd
        KO --> HPA
        KM --> HPA

        MON --> Graf[Grafana]:::external

        docs --> MON

    end

    click kustom001 "https://github.com/nitesh8011/hpa-keda/blob/main/001_operator/kustomization.yaml"
    click namespace001 "https://github.com/nitesh8011/hpa-keda/blob/main/001_operator/namespace.yaml"
    click opergroup "https://github.com/nitesh8011/hpa-keda/blob/main/001_operator/operatorgroup.yaml"
    click subscript "https://github.com/nitesh8011/hpa-keda/blob/main/001_operator/subscription.yaml"
    click kedacont "https://github.com/nitesh8011/hpa-keda/blob/main/001_operator/kedacontroller.yaml"
    click kustom002 "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/kustomization.yaml"
    click namespace002 "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/namespace.yaml"
    click appcfg "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/application.yaml"
    click socfg "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/scaledobject.yaml"
    click tacfg "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/triggerauthentication.yaml"
    click smcfg "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/servicemonitor.yaml"
    click rbaccfg "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/rbac.yaml"
    click secretcfg "https://github.com/nitesh8011/hpa-keda/blob/main/002_metrics/secret.yaml"
    click dockerf "https://github.com/nitesh8011/hpa-keda/tree/main/Dockerfile"
    click jar "https://github.com/nitesh8011/hpa-keda/blob/main/spring-boot-prometheus-grafana-0.0.1-SNAPSHOT.jar"
    click loadcfg "https://github.com/nitesh8011/hpa-keda/blob/main/load.yaml"
    click docs "https://github.com/nitesh8011/hpa-keda/blob/main/README.md"

    classDef operator fill:#D8BFD8,stroke:#000,stroke-width:1px
    classDef workload fill:#98FB98,stroke:#000,stroke-width:1px
    classDef external fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef crd fill:#FFFFE0,stroke:#000,stroke-width:1px
    classDef config fill:#F0F0F0,stroke:#000,stroke-width:1px
```
