Wednesday, 5 August 2026

kube-state-metrics

 


kube-state-metrics (KSM) is an official Kubernetes add-on agent that listens to the Kubernetes API server and generates Prometheus-format metrics about the state of Kubernetes objects (such as Pods, Deployments, Nodes, StatefulSets, and CronJobs).

Unlike agent metrics collectors like cAdvisor or node_exporter, KSM does not measure resource usage (like CPU usage, memory consumption, or disk I/O). Instead, it translates the raw state of objects in the Kubernetes API into structured metrics.

Core Concept: Usage vs. State


cAdvisor / Node Exporter (Usage): "How much CPU is Pod X consuming right now?"
kube-state-metrics (State): "How many replicas are ready in Deployment Y?", "When did CronJob Z last succeed?", "What phase is Pod A in?"


How kube-state-metrics Exposes Metrics


┌─────────────────────────┐
│ Kubernetes API Server   │
└────────────┬────────────┘
             │ Watch / List (Informer Cache)
             ▼
┌─────────────────────────┐
│   kube-state-metrics    │ ─── (Generates OpenMetrics in-memory)
└────────────┬────────────┘
             │ HTTP GET /metrics (Port 8080)
             ▼
┌─────────────────────────┐
│    Prometheus Server    │
└─────────────────────────┘

1. Consuming the API via Kubernetes Informers

KSM connects directly to the Kubernetes API server using standard Client-GO Informers.
  • Instead of constantly polling the API server with heavy requests, KSM maintains an in-memory cache updated in real-time via long-polling watch streams.
  • When a resource changes (e.g., a Deployment scales down or a Pod enters CrashLoopBackOff), the local cache updates instantly.

2. Generating Prometheus OpenMetrics In-Memory

KSM parses the fields of the Kubernetes object specs and status subresources (such as .status.phase, .spec.replicas, .status.conditions) and converts them directly into Prometheus gauge/counter metrics.

3. Serving via HTTP Endpoint

KSM hosts a lightweight HTTP server (default port :8080 at path /metrics).

It does not store metrics over time, nor does it push metrics out.

When a Prometheus server scrapes KSM's /metrics endpoint, KSM reads its in-memory snapshot, formats the raw text payload, and streams it back.

Key Technical Properties


Property
Details

Port & Path
Port 8080 (/metrics for cluster state), Port 8081 (/metrics for KSM internal performance telemetry)

Data Format
Prometheus text format / OpenMetrics standard

Metric Types
Predominantly Gauge metrics representing instantaneous state (1 or 0 state representations)

Scaling
Supports Horizontal Sharding and resource filtering (--resources, --namespaces) for large clusters


Example Metric Output


A scrape request to kube-state-metrics for a Deployment produces plain-text metrics like this:

# HELP kube_deployment_spec_replicas The desired number of pods declared in the deployment spec.
# TYPE kube_deployment_spec_replicas gauge
kube_deployment_spec_replicas{namespace="default",deployment="api-server"} 3

# HELP kube_deployment_status_replicas_available The number of available pods created by the deployment.
# TYPE kube_deployment_status_replicas_available gauge
kube_deployment_status_replicas_available{namespace="default",deployment="api-server"} 2


Do we need kube-state-metrics if we have Prometheus deployed in the cluster?


Yes, you still need kube-state-metrics (KSM) even if Prometheus is deployed in your cluster.

Prometheus is the engine that collects, stores, and queries time-series data, while kube-state-metrics is the agent that generates metrics about the state of Kubernetes API objects.

Prometheus does not natively inspect Kubernetes API objects on its own—it relies on exporters like KSM to expose that data.

What Prometheus collects out-of-the-box vs. with KSM?


Metric Source
Responsible Agent
Examples of Metrics Provided

Node / OS Metrics
node_exporter
Node CPU utilization, RAM usage, disk space, network traffic.

Container Usage
cAdvisor (built into kubelet)
Container CPU throttling, memory usage (RSS), network bytes per container.

Control Plane
kube-apiserver, etcd, coredns
API latency, request counts, etcd commit durations.

Kubernetes State
kube-state-metrics
Deployment replica counts, CronJob last success times, Pod restart counts, pending PVCs, ingress statuses, TLS certificate secret expiration.


What happens without kube-state-metrics?


If you run Prometheus without KSM, you lose visibility into the high-level health and configuration of your cluster resources. You will not be able to:

  1. Alert on Deployment Health: You won't know if spec.replicas (desired) doesn't match status.available_replicas.
  2. Track Job / CronJob Success: Metrics like kube_cronjob_status_last_successful_time or kube_job_status_failed won't exist.
  3. Monitor Pod Lifecycle States: You won't have metrics for Pods stuck in Pending, CrashLoopBackOff status codes, or ImagePullBackOff.
  4. Track Resource Requests vs. Limits: You won't be able to compare requested CPU/memory (kube_pod_container_resource_requests) against node capacity to measure cluster overcommit.

How They Work Together


┌─────────────────────────────────────────────────────────┐
│                    Kubernetes API                       │
└────────────────────────────┬────────────────────────────┘
                             │ Watch API Objects
                             ▼
                 ┌───────────────────────┐
                 │  kube-state-metrics   │
                 └───────────┬───────────┘
                             │ Exposes /metrics
                             ▼
                 ┌───────────────────────┐
                 │   Prometheus Server   │ ◄── (Scrapes KSM)
                 └───────────────────────┘


  1. KSM listens to the API server and generates metrics representing resource states.
  2. Prometheus scrapes KSM's /metrics endpoint along with cAdvisor, node-exporter, and your application endpoints.
  3. Prometheus evaluates Alertmanager rules and stores the metrics for Grafana dashboards.

Standard Stack Setup

In standard Kubernetes monitoring deployments (such as the kube-prometheus-stack Helm chart or Prometheus Operator), kube-state-metrics is included by default as a core component alongside Prometheus and Alertmanager.


How to install kube-state-metrics?



kube-state-metrics (KSM) is typically deployed using one of four common approaches:

1. As Part of the kube-prometheus-stack (Most Common)


If you are setting up cluster monitoring with Prometheus and Grafana, kube-state-metrics is usually installed automatically as a bundled sub-chart.

Using the kube-prometheus-stack Helm chart:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus-stack prometheus-community/kube-prometheus-stack


kube-state-metrics runs out of the box with ServiceMonitors already configured.

2. Standalone Helm Chart


If you already have a Prometheus instance or another monitoring agent (like Datadog, New Relic, or Grafana Alloy) and just need KSM running, you can deploy the official standalone Helm chart:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install kube-state-metrics prometheus-community/kube-state-metrics --namespace kube-system


3. Native Kustomize / Standard Manifests (kubectl apply)


For gitops workflows or lightweight clusters where Helm isn't used, you can apply the official manifests maintained directly in the upstream Git repository using Kustomize:

git clone https://github.com/kubernetes/kube-state-metrics.git
cd kube-state-metrics
kubectl apply -k examples/standard

Note: This creates the necessary ServiceAccount, ClusterRole, ClusterRoleBinding, Deployment, and Service in the kube-system namespace.


4. Cloud Managed Kubernetes Add-ons


On managed platforms (like GKE, EKS, or AKS), kube-state-metrics is often enabled as a one-click add-on or managed component integrated into cloud-native observability services (e.g., Google Cloud Observability, AWS CloudWatch Container Insights, or Azure Monitor).


What Gets Created in the Cluster?


Regardless of how you deploy it, KSM will create:
  • ServiceAccount & ClusterRole/Binding: Grants read-only access (list, watch, get) to cluster API resources.
  • Deployment: Runs the kube-state-metrics container.  
  • Service: Exposes the HTTP endpoint (usually on port 8080 at /metrics).  


How to inspect which default metrics KSM exposes or how to disable unused ones?


Understanding which metrics kube-state-metrics (KSM) emits and how to prune unwanted metrics is key to controlling Prometheus ingestion volume and storage overhead.

Part 1: Inspecting Exposed Metrics


You can inspect the metrics exposed by KSM using three different methods:

1. Official Documentation Reference


The KSM GitHub repository contains generated documentation for every supported Kubernetes API group.
  • Standard Docs: kube-state-metrics/docs contains dedicated files for each resource (e.g., pod-metrics.md, cronjob-metrics.md).
  • Resource Status: The docs categorize metrics as STABLE (default), EXPERIMENTAL (alpha/beta features), or DEPRECATED.  

2. Direct In-Cluster Scraping (curl / port-forward)


To see the exact payload KSM generates in your active cluster, port-forward to the KSM service and fetch /metrics:

# Port-forward the KSM service locally
kubectl port-forward svc/kube-state-metrics -n monitoring 8080:8080

# In a separate terminal, curl the metrics endpoint
curl http://localhost:8080/metrics


Filter for specific resource types using grep:

# View all Pod-related metrics exposed by KSM
curl -s http://localhost:8080/metrics | grep "^kube_pod_"

# View unique metric family names
curl -s http://localhost:8080/metrics | grep -v "^#" | cut -d'{' -f1 | sort -u


3. Prometheus Metric Explorer


In Prometheus, run the following query in the Expression Browser to list all distinct metric names harvested from the KSM job:

count by (__name__) ({job="kube-state-metrics"})


Part 2: Disabling or Filtering Unused Metrics


There are two primary ways to reduce metric volume: at the source (KSM Flags) or at the scraper (Prometheus Relabeling).

Method 1: Filtering at Source via KSM Flags (Recommended)


You can instruct KSM to only collect or expose specific resources or metric families using command-line arguments. This reduces both KSM memory utilization and network output.

--resources: Restricts KSM to watch specific Kubernetes objects only (e.g., ignore configmaps or secrets).
--metric-allowlist: Explicitly lists metric families to expose (all others are dropped).
--metric-denylist: Drops specific metric families while keeping the rest.

Setting via Helm (values.yaml):

# Helm configuration for prometheus-community/kube-state-metrics
resources:
  # Watch only specific K8s API resources
  resources:
    - pods
    - deployments
    - statefulsets
    - cronjobs
    - nodes

# Disable specific metric families across enabled resources
extraArgs:
  - --metric-denylist=kube_configmap_info,kube_secret_info,kube_pod_labels
Setting via Argo CD Application Manifest:YAMLapiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: kube-state-metrics
  namespace: argocd
spec:
  # ... (repo configuration omitted)
  source:
    helm:
      extraArgs:
        - --resources=pods,deployments,cronjobs,nodes
        - --metric-denylist=kube_pod_created,kube_pod_completion_time


Method 2: Dropping Metrics at Prometheus Ingestion


If you do not manage KSM CLI flags directly, configure Prometheus or Grafana Agent/Alloy to drop metrics during the scrape phase using metric_relabel_configs:

Prometheus CustomResource (Prometheus / ServiceMonitor):


apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: kube-state-metrics
  namespace: monitoring
spec:
  endpoints:
    - port: http-metrics
      metricRelabelings:
        # Drop entire metric families matching a regex pattern
        - action: drop
          sourceLabels: [__name__]
          regex: "(kube_configmap_.*|kube_secret_.*|kube_lease_.*)"
        
        # Drop high-cardinality container state metrics if unused
        - action: drop
          sourceLabels: [__name__]
          regex: "kube_pod_container_state_started"


kube_job_status_failed Source Filtering vs. Scraper DroppingFeatureKSM CLI Flags (--resources, --metric-denylist)Prometheus metric_relabel_configsKSM Memory UsageLower (KSM does not store or process ignored objects in memory)Unchanged (KSM processes objects regardless)Network TrafficLower (HTTP payload size is smaller)Unchanged (KSM transmits full payload; Prometheus discards post-fetch)Prometheus DB StorageLowerLowerConfig PlacementKSM Deployment manifestPrometheus scrape configuration



Prometheus Metrics Exposed by kube-state-metrics


kube_job_status_succeeded


kube_job_status_succeeded is a Prometheus metric exposed by kube-state-metrics that tracks the status of Kubernetes Jobs.

It returns a gauge value representing whether a Job execution succeeded, partitioned by label selectors.

Metric Breakdown


  • Metric Name: kube_job_status_succeeded
  • Type: Gauge
  • Value:
    • 1: The Job has completed successfully.
    • 0: The Job is in progress, failed, or has not succeeded.

Key Labels


Label                  Description
====                  =========
job_name        The name of the Kubernetes Job resource
namespace      The namespace where the Job resides
condition      Condition status (typically true when checking successful completion)


Common PromQL Queries


1. List All Currently Succeeded Jobs


kube_job_status_succeeded{condition="true"} == 1

2. Detect Jobs That Failed or Did Not Succeed


kube_job_status_succeeded{condition="true"} == 0

3. Alerting Rule: Job Failure


Alert when a Job has completed its execution attempt without succeeding:

- alert: KubernetesJobFailed
  expr: kube_job_status_failed{condition="true"} == 1
  for: 5m
  labels:
    severity: warning
  annotations:
    summary: "Kubernetes Job {{ $labels.namespace }}/{{ $labels.job_name }} failed"


4. Track Job Completion Rate Over Time


Calculate the rate of successful Job completions across a cluster:

sum(increase(kube_job_status_succeeded{condition="true"}[1h]))


kube_job_status_failed


kube_job_status_failed is a Prometheus metric exposed by kube-state-metrics that tracks whether a Kubernetes Job has failed to execute successfully.

It returns a gauge value representing the failure status of a Job, partitioned by label selectors.

Metric Breakdown


  • Metric Name: kube_job_status_failed
  • Type: Gauge
  • Value:
    • 1: The Job reached its failure condition (e.g., exceeded backoffLimit or failed execution).
    • 0: The Job has not failed (it is currently running, pending, or succeeded).

Key Labels


Label                Description
====                =========
job_name      The name of the Kubernetes Job resource
namespace    The namespace where the Job resides
condition    Condition status (typically true when evaluating an active failure state)
reason           The reason for failure if populated (e.g., BackoffLimitExceeded, DeadlineExceeded)


Common PromQL Queries



1. List All Currently Failed Jobs


kube_job_status_failed{condition="true"} == 1

2. Count Failed Jobs by Namespace


sum by (namespace) (kube_job_status_failed{condition="true"} == 1)

3. Prometheus Alerting Rule for Job Failures


Alert when a Job has reached a failed condition and remained failed for 5 minutes:

- alert: KubernetesJobFailed
  expr: kube_job_status_failed{condition="true"} == 1
  for: 5m
  labels:
    severity: warning
  annotations:
    summary: "Kubernetes Job {{ $labels.namespace }}/{{ $labels.job_name }} failed"
    description: "Job {{ $labels.job_name }} in namespace {{ $labels.namespace }} failed to complete."


4. Filter CronJobs / Generated Jobs by Prefix


If Jobs are generated dynamically by CronJobs, group or filter using regular expressions:

kube_job_status_failed{condition="true", job_name=~"nightly-backup-.*"} == 1



kube_cronjob_status_last_successful_time



kube_cronjob_status_last_successful_time is a gauge metric exposed by kube-state-metrics.

It records the Unix timestamp (in seconds) of when a Kubernetes CronJob last completed execution successfully.

Metric Details


Attribute:Value

  • Exporter: kube-state-metrics
  • Metric Type:Gauge
  • Value: Unix timestamp (seconds) or omitted if the CronJob has never completed
  • Labels: cronjob, namespace

Common PromQL Use Cases


1. Time Since Last Successful Run


Calculates how many seconds have elapsed since the CronJob last succeeded:

time() - kube_cronjob_status_last_successful_time

2. Alert on CronJob Failure or Missed Schedule


Alerts if the time since the last successful execution exceeds a target threshold (e.g., 1 day / 86,400 seconds):

(time() - kube_cronjob_status_last_successful_time) > 86400

3. Alerting when Schedule Run Failed


Compares the last scheduled time (kube_cronjob_status_last_schedule_time) against the last successful time. If last_schedule_time is greater than last_successful_time, the most recent run failed or is taking abnormally long:

kube_cronjob_status_last_schedule_time - kube_cronjob_status_last_successful_time > 0

4. Filter out Suspended CronJobs


Combine with kube_cronjob_spec_suspend to suppress false alerts on intentionally paused jobs:

(
  (time() - kube_cronjob_status_last_successful_time) > 86400
) and on (cronjob, namespace) (
  kube_cronjob_spec_suspend == 0
)



Tuesday, 4 August 2026

Monitoring term: Dead-man's switch


It's monitoring inverted: instead of alerting when you observe something bad, you alert when you stop observing something good.

Example: ping on a successful run. If run is unsuccessful, monitoring catches missing ping and triggers alert.

The name comes from industrial safety — the lever on a train's throttle or a chainsaw that has to be actively held down. If the operator dies or lets go, the machine stops. Safety is the default state; it takes continuous positive action to keep running.

Normal alerting is presence-based. Something goes wrong, it emits a signal, you alert on the signal: error rate spikes, latency crosses a threshold, a pod enters CrashLoopBackOff. It works well when failures are noisy.

A dead-man's switch is absence-based. The healthy system periodically says "still fine." You alert when that message doesn't arrive on time.

Kubernetes Debugging Scenario: Node.JS CronJob dies with a V8 JavaScript heap OOM

Problem Scenario


A Node.js batch job running as a Kubernetes CronJob aborts with FATAL ERROR: Ineffective mark-compacts near heap limit at ~4 GB. No NODE_OPTIONS, no resources block. Each scheduled run leaves several failed pods behind.


Knowledge required to fix the problem (Q&A)


Detailed Q&A


1. Node.js / V8 memory model

Q: What does --max-old-space-size actually control, and what does it not control? 

It caps V8's old space — the long-lived generation of the JS heap. It does not cap new space (--max-semi-space-size), code space, large object space, or external/off-heap memory such as Buffer and ArrayBuffer allocations, native addon memory, thread-pool stacks, or glibc malloc arenas. So a process with a 6 GB old-space ceiling can easily have an RSS well above 6 GB.

V8 is Google's open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others.

--max-old-space-size sets the maximum memory limit (in megabytes) allocated to the Old Generation heap space inside V8, the JavaScript engine powering Node.js.

When V8 allocates memory for your application, it divides the JavaScript heap into distinct regions based on object lifecycle. This flag configures the largest region where long-lived objects reside.

What It Measures & Controls

--max-old-space-size explicitly caps memory allocated for:

  • Old Generation JavaScript Objects: Objects, arrays, functions, closures, and strings that have survived initial garbage collection cycles in the Young Generation space and were promoted to the Old Generation.
  • Old Pointer Space & Old Data Space: Regions holding objects that contain pointers to other objects and raw data (like numbers or unboxed scalars).

What It Does NOT Control

  • A common misconception is that --max-old-space-size caps the entire Resident Set Size (RSS) or system memory footprint of your Node.js process. It does not limit:
  • Node.js Buffers (ArrayBuffers): Since Node.js v8.0+, binary Buffer allocations use off-heap C++ memory backing stores (ArrayBuffer). While the JavaScript wrapper object lives on the V8 heap, the underlying raw bytes do not count toward the old space limit.
  • Native C++ Allocations: Memory used by native C++ add-ons, libuv threads, or external libraries compiled into Node.
  • Other V8 Heap Spaces:
    • New Space (Nursery/Young Generation): Where new allocations land (--max-semi-space-size).
    • Code Space: JIT-compiled bytecode and machine code.
    • Map/Cell Spaces: V8 internal hidden classes and metadata.
  • Call Stack Memory: Memory used by execution contexts and local variables on the stack.

Because of off-heap memory, a Node.js process with --max-old-space-size=2048 (2 GB) can easily consume 3 GB or more of total system RAM (RSS).

What Happens When the Limit Is Reached

  1. Aggressive Garbage Collection: As old space usage approaches the limit, V8 triggers blocking, high-overhead Mark-Sweep-Compact garbage collection cycles to reclaim dead objects.
  2. Process Crash: If V8 cannot free enough memory to fit the next allocation below the configured threshold, Node.js crashes with a fatal error:

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Default Values & Usage

Default Behavior: In modern Node.js versions, V8 dynamically sets the limit based on total available system RAM—typically around 2 GB to 4 GB on 64-bit systems if unspecified.

Command Line Flag:

node --max-old-space-size=4096 app.js

Environment Variable:

export NODE_OPTIONS="--max-old-space-size=4096"

 

Q: Why did the process die at ~4064 MB when nobody configured a heap limit? 

V8 picks a default heap ceiling from the memory it believes is available, and on 64-bit builds that lands at roughly 4 GB. The Mark-Compact 4064.3 MB line in the log is the giveaway that it hit that default ceiling rather than any limit you set.


Q: Why doesn't Node just size its heap to the container's memory limit? 

Historically V8 read host RAM, not the cgroup limit, so a Node process in a 512 Mi container would happily set a multi-gigabyte heap and get OOM-killed. Newer Node versions do consult cgroup constraints, but the behaviour varies by version — which is why the defensive answer is always to set the flag explicitly rather than rely on auto-detection.


Q: The workload starts with npm run start. Does setting NODE_OPTIONS in the container env actually reach the Node process? 

Yes — NODE_OPTIONS is an environment variable, so it's inherited by every child process npm spawns. Two caveats worth naming: it also applies to the npm wrapper process itself (harmless, just an extra reservation), and not every V8 flag is permitted inside NODE_OPTIONS. The alternative is passing the flag in the npm script itself, which is more surgical but easier to lose.



2. Diagnosis: which kind of OOM is this?

Q: How do you tell a V8 heap OOM from a kernel OOMKill from a kubelet eviction?

Signal V8 heap OOM OOMKilled Evicted
Log line FATAL ERROR: Ineffective mark-compacts near heap limit none from the app — killed mid-flight none from the app
Signal / exit SIGABRT, exit 134 SIGKILL, exit 137 pod deleted
Pod status Error OOMKilled in lastState.terminated.reason Failed, reason Evicted
Fix direction raise heap ceiling or reduce allocation raise container limit set requests so you aren't the first target

The ticket's evidence — the mark-compact message plus signal SIGABRT — puts it firmly in column one. That matters because raising the container limit alone would have changed nothing: V8 would still have aborted at 4 GB.


Q: How would you size the flag rather than guessing? 

Instrument before you tune. --trace-gc shows the heap trajectory over the run; process.memoryUsage() sampled periodically distinguishes heapUsed from external; --heapsnapshot-near-heap-limit=1 writes a snapshot right before the abort that you can open in Chrome DevTools to find the retaining structure. That tells you whether the working set is genuinely ~6 GB or whether one unbounded array is the whole problem.

Q: Is raising the heap the right fix at all? 

Usually it's a mitigation, not a fix. A benchmark job whose memory scales with input size will hit any ceiling you pick — the durable fix is streaming, batching, or paginating so peak memory is bounded by chunk size rather than dataset size. Raising the flag is defensible as a stopgap; the honest version says so in the ticket and files the follow-up. Note that in this case the real numbers came out at 10 GB heap with a 5-hour runtime, which is a fairly loud hint that the algorithm is the underlying issue.


3. Kubernetes resource management

Q: What's the difference between a memory request and a memory limit? 

The request is what the scheduler reserves — it decides which node the pod fits on and is the baseline the kubelet uses when deciding who to evict. The limit is enforced at runtime by the cgroup; exceed it and the kernel OOM-kills the container. Memory, unlike CPU, is incompressible: there's no throttling, only killing.

Q: What QoS class does a pod with no resources block get, and why does that matter here? 

BestEffort — the first thing evicted under node memory pressure, and it contributes nothing to the scheduler's accounting so the node can be oversubscribed into pressure in the first place. Setting requests equal to limits gives Guaranteed; requests below limits gives Burstable.

Q: How do you choose the relationship between the heap flag and the container limit? 

Limit strictly above heap ceiling, with headroom for everything --max-old-space-size doesn't cover — off-heap buffers, native memory, the npm and node process overhead, plus GC working room. The ticket proposed 6 GB heap under a 7 Gi limit; what actually shipped was 10 GB heap under a 12 Gi limit. Too tight and you convert a clean SIGABRT into a much harder-to-debug OOMKill.

Q: What's the risk of setting limits.memory well above requests.memory? 

You're overcommitting the node. It schedules against the request but can consume up to the limit, so several such pods on one node can drive it into memory pressure and trigger evictions of unrelated workloads. Matching them costs you scheduling flexibility but makes the blast radius predictable.

Q: You set requests.memory: 8Gi and the pod never starts. What's your first check? 

Whether any node has 8 Gi of allocatable memory free — allocatable is capacity minus kube-reserved, system-reserved, and eviction thresholds. The pod sits Pending with an Insufficient memory scheduling event. Big-request batch jobs are a classic case for a dedicated or autoscaling node pool.


4. CronJobs and Jobs

Q: What produced seven Error pods from one scheduled run? 

backoffLimit retries on failure, and a deterministic OOM fails identically every time — so the Job burned through its retries producing one dead pod each. The fix applied was a podFailurePolicy that fails the Job on the application's exit code instead of retrying, plus ttlSecondsAfterFinished so finished Jobs get garbage-collected rather than accumulating.

Q: What does podFailurePolicy require to work? 

restartPolicy: Never on the pod template, and rules matching on either container exit codes (onExitCodes) or pod conditions (onPodConditions, e.g. DisruptionTarget). Actions are FailJob, Ignore, Count, and FailIndex. The point is distinguishing retryable infrastructure failures from deterministic application failures — retrying a heap OOM six times is pure waste.

Q: Which CronJob fields govern history and overlap? 

successfulJobsHistoryLimit / failedJobsHistoryLimit for retained Job objects, ttlSecondsAfterFinished on the Job for automatic cleanup, concurrencyPolicy (Allow / Forbid / Replace) for overlapping runs, startingDeadlineSeconds for missed schedules, and activeDeadlineSeconds as a wall-clock kill switch. For a job that runs five hours, concurrencyPolicy: Forbid deserves a hard look.

Q: The schedule is 30 10 */14 * *. Does that run every 14 days? 

No — and this is the trap. Step values in day-of-month are evaluated within each month, so it fires on the 1st, 15th, and 29th, then resets. The gap between the 29th and the following 1st is two or three days, not fourteen. Genuine "every N days" needs an external scheduler or a daily run that no-ops based on a stored timestamp.


5. Container memory accounting

Q: When you read a container's memory usage, what are you actually seeing? 

Under cgroup v2 the kubelet reports working set derived from memory.current minus inactive file cache; memory.max is the hard limit. Crucially memory.current includes page cache, so a process doing heavy file I/O can look alarming without any anonymous-memory problem. RSS is anonymous plus mapped pages for the process specifically, and glibc often doesn't return freed memory to the OS — so RSS is sticky and lags real usage downward.

Q: Why is container_memory_rss a poor alerting signal for some workloads? 

Because it only captures what lives in RSS. For a JVM or Node process the heap is anonymous memory and RSS tracks it reasonably; for something like Percona MongoDB, where WiredTiger's cache sits in the OS page cache rather than RSS, the metric is structurally blind to the thing you care about — you want cache fill percentage instead. Matching the metric to the workload's memory architecture is the actual skill.


6. Verification

Q: How do you prove the fix worked? 

Trigger a manual run (kubectl create job --from=cronjob/experience-benchmarks) and confirm the Job reaches Complete with no SIGABRT and no Error pods. Then compare peak usage against the limit — completing at 95% of the ceiling is luck, not a fix. Verification model: live CronJob spec matches main, last three runs all Complete, runtimes recorded, zero Error pods.

Q: How do you confirm what's actually running in prod matches what's in the repo? 

Diff the live object against the manifest — kubectl get cronjob experience-benchmarks -o yaml against deploy/prod.yml. Drift between a merged PR and the running cluster is exactly the kind of gap that lets a "fixed" ticket keep failing, and it's the check that would have surfaced the tickets overlap before any code was written.

Q: What should you have checked before writing a single line for this ticket? 

Whether the problem still existed. The ticket sat in Backlog for four days, a ticket shipped a superset of the fix during that window, and the work that followed would have lowered the heap from 10 GB to 6 GB — reintroducing the OOM. Reading main and the live spec before implementing is the cheapest step in the whole process and the one that was skipped.


Brief Q&A


V8 / Node

Q: What does --max-old-space-size cap? Only V8's old space. Not new space, code space, or off-heap memory (Buffer, ArrayBuffer, native addons). RSS can exceed it substantially.

Q: Why die at ~4 GB with no flag set? That's V8's default ceiling on 64-bit. Node has historically sized it from host RAM, not the cgroup limit — so always set it explicitly.

Q: Does NODE_OPTIONS reach a process started via npm run start? Yes, it's inherited by child processes. It also applies to the npm wrapper itself.

Diagnosis

Q: Distinguish the three OOM flavours. V8 heap OOM → mark-compact message, SIGABRT, exit 134, pod Error. Kernel kill → no app log, SIGKILL, exit 137, OOMKilled. Eviction → pod Failed, reason Evicted. Only the first is fixed by the heap flag.

Q: How do you size the flag instead of guessing? --trace-gc for the trajectory, process.memoryUsage() for heap vs. external, --heapsnapshot-near-heap-limit=1 for a snapshot at the abort.

Q: Is raising the heap the real fix? Usually a stopgap. If memory scales with input size, any ceiling eventually fails — stream or batch so peak is bounded by chunk size.

Kubernetes resources

Q: Request vs. limit? Request drives scheduling and eviction ranking; limit is cgroup-enforced. Memory is incompressible — no throttling, only killing.

Q: No resources block means what QoS? BestEffort — first evicted under node pressure, and invisible to scheduler accounting. Requests == limits gives Guaranteed.

Q: How do heap ceiling and container limit relate? Limit strictly above the heap, with headroom for off-heap and process overhead. Too tight converts a clean SIGABRT into a harder-to-debug OOMKill.

Q: Request set high and the pod won't schedule? Check node allocatable (capacity minus reserved and eviction thresholds). Expect Pending with Insufficient memory.

Jobs / CronJobs

Q: Why several failed pods per run? backoffLimit retries, and a deterministic OOM fails identically each time. Use podFailurePolicy with onExitCodes (requires restartPolicy: Never) to fail fast, plus ttlSecondsAfterFinished for cleanup.

Q: Does 30 10 */14 * * run every 14 days? No. Day-of-month steps reset monthly → the 1st, 15th, and 29th. True "every N days" needs external scheduling.

Verification

Q: How do you prove it's fixed? Trigger a manual run from the CronJob, confirm Complete with no failed pods, and compare peak usage to the limit — finishing at 95% of the ceiling is luck.

Q: What do you check before writing any code? That the problem still exists. Diff the live object against the repo manifest; a stale ticket can lead you to lower limits that a since-merged fix raised.

Monday, 3 August 2026

elasticsearch-exporter



Elasticsearch doesn't speak Prometheus. It exposes its internals over its own REST API — GET /_nodes/stats, /_cluster/health, /_cat/indices — as JSON, in Elastic's own shape. Prometheus can't scrape that.

The elasticsearch-exporter is a small sidecar-or-Deployment-shaped translator that sits between the two:
  • it polls those ES REST endpoints on an interval,
  • flattens the JSON into Prometheus text-format metrics,
  • and serves them on /metrics (conventionally :9114) for Prometheus to scrape.
The canonical implementation is prometheus-community/elasticsearch_exporter (formerly justwatchcom/elasticsearch_exporter), packaged as the prometheus-elasticsearch-exporter Helm chart. Elastic also ships a first-party alternative path — Metricbeat's elasticsearch module, or the newer Elastic Agent integration — but those ship into Elasticsearch/Kibana's own monitoring cluster, not into Prometheus, so they don't help a Grafana-alerting-on-Prometheus setup.

The metrics it produces are for example:

  • metric
    • what it gives you 
  • search_jvm_memory_used_bytes{area="heap"}
    • actual JVM heap in use — the thing that actually predicts an ES OOM
  • elasticsearch_jvm_memory_max_bytes{area="heap"}
    • configured heap ceiling (-Xmx), so you can take a real ratio
  • elasticsearch_jvm_gc_collection_seconds_*
    • GC pressure; sustained old-gen GC is the pre-OOM tell
  • elasticsearch_breakers_tripped
    • circuit breakers firing — ES rejecting work to avoid OOM
  • elasticsearch_cluster_health_status
    • green/yellow/red, unassigned shards                                 │

The exporter is a separate deployable — ECK does not install it.

Prometheus Metrics



The 4 Core Prometheus Metric Types


Prometheus defines four primary metric types. Choosing the right type depends on how the value behaves over time.


                              ┌───────────────────────────┐
                              │ Prometheus Metric Types   │
                              └─────────────┬─────────────┘
                                            │
         ┌───────────────────┬──────────────┴───────────────┬────────────────────┐
         ▼                   ▼                              ▼                    ▼
  ┌──────────────┐    ┌──────────────┐              ┌──────────────┐     ┌──────────────┐
  │    Gauge     │    │   Counter    │              │  Histogram   │     │   Summary    │
  ├──────────────┤    ├──────────────┤              ├──────────────┤     ├──────────────┤
  │ Value goes   │    │ Value ONLY   │              │ Groups values│     │ Calculates   │
  │ UP and DOWN  │    │ increases    │              │ into buckets │     │ quantiles    │
  │ (Snapshot)   │    │ (Cumulative) │              │ (Client-side)│     │ (Client-side)│
  └──────────────┘    └──────────────┘              └──────────────┘     └──────────────┘

1. Gauge


  • Behavior: Can increase, decrease, or stay the same.
  • Use Case: Current state, temperatures, memory usage, concurrent connections, replica counts.
  • PromQL Functions: avg_over_time(), max_over_time(), direct evaluation.

In Prometheus (and OpenMetrics standards), a Gauge is a metric that represents a single numerical value that can arbitrarily go up and down.

It acts like a digital display or a vehicle's speedometer—it gives you a point-in-time snapshot of a current state.

Understanding the Gauge Type


Because a Gauge can fluctuate freely, it is used to measure snapshot values, current state levels, and instantaneous statuses.

  • Example Real-World Analogy: A car's speedometer (can go from 0 to 70 mph, back to 30 mph), ambient temperature, or fuel tank level.
  • Kubernetes/KSM Examples:
    • kube_cronjob_status_last_successful_time: Holds a Unix timestamp (moves forward or stays fixed).
    • kube_deployment_status_replicas_available: Number of ready pods (e.g., 3 → 5 → 2).
    • node_memory_MemAvailable_bytes: Available system memory in bytes.

Key Feature: Instantaneous Operations


Because gauges fluctuate, you do not run rate or increase functions like rate() or increase() on them in PromQL. Instead, you query their current value directly or perform operations like running averages (avg_over_time()), min/max checks, or scalar math (e.g., time() - gauge).



2. Counter


  • Behavior: A cumulative metric that only increases (or resets to 0 upon process restart). It never decreases naturally.
  • Use Case: Counting total occurrences of events (e.g., total HTTP requests served, total pipeline errors, network bytes transmitted).
  • PromQL Functions: rate(), irate(), increase().

Why use a Counter instead of a Gauge? A counter allows PromQL's rate() function to accurately calculate "per-second rates" while automatically handling process restarts (resets).

3. Histogram


  • Behavior: Samples observations (usually things like request durations or payload sizes) and counts them in configurable buckets. It also provides a sum of all observed values.
  • Exposed Data:
    • <basename>_bucket{le="<upper_bound>"}: Counter of observations with value <= upper bound.
    • <basename>_count: Total number of observations (Counter).
    • <basename>_sum: Sum of all observed values (Counter).
  • Use Case: Measuring latencies, response times, or request sizes where you want to calculate percentiles (e.g., p95, p99) on the server side using PromQL (histogram_quantile()).

4. Summary


  • Behavior: Similar to a histogram, but calculates configurable quantiles (e.g., 0.50, 0.90, 0.99) directly on the client application side over a sliding time window.
  • Exposed Data:
    • <basename>{quantile="0.95"}: The 95th percentile value.
    • <basename>_count: Total observations.
    • <basename>_sum: Sum of observations.
  • Use Case: When client-side percentile calculation is required and you don't need to aggregate quantiles across multiple instances in Prometheus.


Summary Comparison Matrix


Metric Type
  • Can Decrease?
  • Typical PromQL Functions
  • Best Used For

Gauge
  • Yes
  • Direct value, avg_over_time()
  • Current state, memory usage, counts of current objects

Counter
  • No (only on restart)
  • rate(), increase()
  • Total count of events over time, request volume

Histogram
  • No (bucket counts increase)
  • histogram_quantile(), rate()
  • Request latencies, payload sizes (aggregatable)

Summary
  • No (counts/sums increase)
  • Direct quantile query
  • Request latencies (pre-calculated on client)




Container Metrics


container_memory_rss



container_memory_rss is a Prometheus metric (exposed by cAdvisor) that measures a container's Resident Set Size—specifically, the amount of physical RAM allocated to non-reclaimable, non-file-backed memory.

Key Technical Breakdown


Under the hood in Linux cgroups, container_memory_rss tracks:
  • Anonymous Memory: Process heap allocations, execution stack, and memory allocated via malloc or mmap(MAP_ANONYMOUS).  
  • Swap Cache: Memory swapped out to disk that is being brought back into RAM.

What it EXCLUDES:

Unlike standard Linux host RSS metrics, container_memory_rss in cAdvisor excludes file-backed page caches (memory used to cache files read from disk). Because of this, it only measures memory that cannot be automatically freed by the Linux kernel under memory pressure.  


container_memory_rss vs. Other Container Metrics


To understand its role, it helps to see how it fits into cAdvisor's other core memory metrics:

Metric
  • Includes
  • Purpose / Key Characteristic

container_memory_rss
  • Heap + Stack (Anonymous memory)
  • Stable indicator of process memory footprint. Does not fluctuate with disk I/O.

container_memory_working_set_bytes
  • Heap + Stack + Active Page Cache
  • What Kubernetes actually monitors. Fluctuates with file reads/writes.

container_memory_usage_bytes
  • Heap + Stack + Active Cache + Inactive Cache
  • Raw total RAM usage. Can be misleading because inactive cache is easily reclaimed by the OS.

Why is container_memory_rss Important?

  • Memory Leak Detection: Because it excludes cached file reads, container_memory_rss provides a much cleaner signal for application-level memory leaks. If this metric steadily climbs over time without dropping, your application (e.g., Go heap, JVM heap, Node process) is holding onto unmanaged memory. 
  • Debugging OOM Kills: While Kubernetes triggers OOMKills based on container_memory_working_set_bytes hitting resource limits, container_memory_rss helps you determine why it happened:  
    • High RSS + High Working Set --> Application memory leak or underestimated heap limit.
    • Low RSS + High Working Set --> Heavy disk I/O / file caching (e.g., reading massive log files or database indexes into memory).  

When container_memory_rss is NOT a relevant metric?


Example:

WiredTiger is the default storage engine that MongoDB (here, the Percona Server for MongoDB / PSMDB cluster) uses to actually read and write data to disk. In <ticketID> it matters specifically because of how it uses memory, which is what breaks those four Grafana alert rules.

The relevant behavior:

WiredTiger keeps a large in-memory cache. It maintains its own cache of frequently-accessed data and indexes, and it deliberately sizes that cache to roughly half the container's memory limit (the issue cites ~10 GiB against a 21Gi limit on rs0/rs2, and it's tunable per <ticketID>). WiredTiger runs its own eviction, targeting about 80% cache fill under normal operation and triggering aggressive eviction around 95%.

That cached data shows up as page cache, not RSS. This is the crux of the ticket. container_memory_rss counts anonymous/resident process memory but excludes the OS page cache — and for a WiredTiger workload the file-backed pages (the cache) are the bulk of the real footprint. So rss / limit sits structurally pinned around 50-58% no matter how much memory pressure the pod is actually under. An alert keyed on container_memory_rss > 0.90 can therefore never fire for a WiredTiger container. It's dead. (alert is defined as: container_memory_rss{container="mongod"} / limit > 0.90)

But you can't just switch to working-set either. container_memory_working_set_bytes does include those active file pages, so on the busy replicas it reads 95-96%. That looks alarming but is expected and healthy: it's the WiredTiger cache sitting at its designed ~half-of-limit size and WT's own eviction watermark. The kernel reclaims those pages under real pressure, and the evidence is that nothing in the mongodb namespace has ever been OOMKilled. Flipping the metric would just move the rule from never-firing to always-firing.

The signal that actually matters is WiredTiger cache fill. Because WT stalls user operations when its cache can't evict fast enough, the meaningful early-warning metric is cache utilization (the existing MongoDB WiredTiger cache fill — above 95% for 30m rule from <ticketID>), not container RSS or working set. That's why the ticket argues the RSS rules are redundant, not just broken, and leans toward deleting them.

So in this issue's context, "WiredTiger" is essentially shorthand for "a workload whose memory lives mostly in a large, self-managed, page-cache-backed database cache" — which is exactly the profile that makes RSS-based memory alerting meaningless and makes cache-fill the correct signal instead. (The same logic applies to the Elasticsearch rules, where the JVM heap plays the analogous role.)


Example PromQL Query


To track application heap growth without the noise of filesystem caching, query RSS like this:

# Rate of container RSS growth over 5-minute intervals
rate(container_memory_rss{namespace="production", container!=""}[5m])


container_memory_working_set_bytes


...

container_memory_usage_bytes


...

Introduction to cAdvisor (Container Advisor)




cAdvisor (short for Container Advisor) is an open-source tool created by Google to collect, aggregate, process, and export resource usage and performance metrics for running containers.

It acts as a daemon that monitors resource isolation parameters, historical resource usage, and network statistics directly from the host node.



How It Works


cAdvisor doesn't require instrumenting code inside containers. Instead, it inspects the node environment where containers run:
  • Queries Linux Kernel Structures: It pulls raw performance data directly from kernel mechanisms—primarily cgroups (control groups) for CPU, memory, and disk utilization, and network interfaces for throughput metrics.
  • Discovers Running Containers: It automatically detects running containers across multiple runtimes (Docker, containerd, CRI-O, systemd containers).
  • Exposes Metrics: It formats gathered data and exposes it over a /metrics HTTP endpoint (primarily in Prometheus format) for scrapers to ingest.


Role in Kubernetes


In Kubernetes, cAdvisor is not deployed as a standalone pod. Instead, it is built directly into the kubelet binary that runs on every node.
  • The kubelet uses cAdvisor internally to monitor local container resource usage.
  • It exposes cAdvisor metrics under the /metrics/cadvisor endpoint on the kubelet API port (typically 10250).
  • Tools like Prometheus scrape this endpoint to collect system-wide container metrics (container_cpu_usage_seconds_total, container_memory_rss, container_network_transmit_bytes_total, etc.).

Core Capabilities


  • Resource Usage Monitoring: Tracks real-time CPU utilization, memory breakdown (RSS, cache, swap), network I/O, and disk space usage per container.
  • Historical Trend Aggregation: Keeps a small buffer of historical telemetry in memory for local inspection.
  • Multi-Runtime Support: Works out of the box with containerd, Docker, and CRI-compliant container engines.
  • Built-in Web UI: When run as a standalone binary or Docker container outside Kubernetes, it provides a lightweight built-in dashboard for quick visual inspection of container stats.


Summary of Metric Flow


Linux Kernel / cgroups --> cAdvisor --> Prometheus --> Grafana

cAdvisor sits right at the boundary between the underlying host/kernel layer and the monitoring stack, turning low-level kernel counters into structured metrics for alerting and visualization.


Kubernetes Job object

 

A Kubernetes Job is a controller object designed to run a batch task to completion.

Unlike Deployments or ReplicaSets (which keep applications running indefinitely) or CronJobs (which trigger tasks on a schedule), a Job creates one or more Pods, executes the workload, and ensures they terminate cleanly. Once the specified number of Pods complete successfully, the Job itself is marked as complete and stops.

Standard Job Manifest


apiVersion: batch/v1
kind: Job
metadata:
  name: data-migration-job
spec:
  backoffLimit: 4             # Number of retries before marking the job failed
  completions: 1              # Number of successful pod completions required
  parallelism: 1              # How many pods run concurrently
  ttlSecondsAfterFinished: 600 # Clean up job & pods 10 minutes after completion
  template:
    spec:
      containers:
      - name: migration-task
        image: python:3.11-slim
        command: ["python", "-c", "print('Running database migration...'); import time; time.sleep(10); print('Done!')"]
      restartPolicy: OnFailure # Required: OnFailure or Never (Always is invalid)


Core Execution Patterns


Kubernetes Jobs support three primary workload execution models:

  • 1. Non-Parallel Jobs
    • Behavior: Starts a single Pod and waits for it to complete successfully.
    • Use Case: One-off database schema migrations, report generation, or administrative scripts.
  • 2. Parallel Jobs with Fixed Completions
    • Behavior: Runs multiple Pods in parallel until a total number of successful completions (spec.completions) is reached.
    • Use Case: Batch processing where $N$ independent tasks need to be completed.
  • 3. Parallel Jobs with Work Queue
    • Behavior: Pods coordinate via an external message queue (e.g., RabbitMQ, Redis, SQS). Each Pod pulls work until the queue is empty, then exits.
    • Use Case: High-throughput task processing, media transcoding, or distributed data transformation.


Key Configuration Fields


Field
  • Default
  • Description

restartPolicy
  • Required
  • Must be OnFailure (restarts container inside same Pod) or Never (spawns a new Pod on failure).

backoffLimit
  • 6
  • Maximum number of retries before marking the Job as failed.

completions
  • 1
  • Total number of successful Pod terminations needed for Job completion.

parallelism
  • 1
  • Max number of Pods allowed to run concurrently at any given moment.

activeDeadlineSeconds
  • Unlimited
  • Max time allowed for the entire Job (including retries) before terminating all running Pods.

completionMode
  • NonIndexed
  • Set to Indexed to assign each Pod a unique completion index ($0$ to $\text{completions}-1$) via environment variables.

ttlSecondsAfterFinished
  • Disabled
  • Automatically deletes the Job and its underlying Pods after $N$ seconds of finishing.

Essential kubectl Commands


Operation                                 Command
==================         ===========
Create a Job imperatively         kubectl create job my-job --image=busybox -- echo "Hello World"
Get Job status                            kubectl get jobs
Inspect job details                     kubectl describe job my-job
List Pods associated with Job   kubectl get pods --selector=batch.kubernetes.io/job-name=my-job
View logs of Job Pods              kubectl logs job/my-job
Delete Job and its Pods            kubectl delete job my-job


Jobs vs Deployments vs CronJobs        



                    ┌─────────────────────────┐
                    │      Workload Type      │
                    └────────────┬────────────┘
                                 │
           ┌─────────────────────┴─────────────────────┐
           ▼                                           ▼
   Long-Running Services                     Batch Tasks / One-Off
(Deployments, StatefulSets)                      (Jobs & CronJobs)
           │                                           │
  Maintains target Pod                       Executes task, then
  count indefinitely.                        terminates cleanly.
                                                       │
                                      ┌────────────────┴────────────────┐
                                      ▼                                 ▼
                                Single Run                         Scheduled Run
                                  (Job)                              (CronJob)