kube-state-metrics
Kube State Metrics exposes Kubernetes object state as Prometheus metrics
About this chart
Kube State Metrics Helm Chart
Kube State Metrics exposes Kubernetes object state as Prometheus metrics
Features
- Comprehensive Monitoring: Exposes state metrics for all Kubernetes resources
- Prometheus Integration: Native Prometheus metrics format
- ServiceMonitor: Prometheus Operator support
- Flexible Collection: Namespace and resource filtering
- Low Overhead: Minimal resource usage (read-only access)
- RBAC Enabled: ClusterRole with minimal permissions
- Operational Tools: 15+ Makefile commands
TL;DR
helm repo add scripton-charts https://scriptonbasestar-container.github.io/sb-helm-charts
helm repo update
# Install with default configuration (all namespaces, all resources)
helm install my-ksm scripton-charts/kube-state-metrics
# Install with namespace filtering
helm install my-ksm scripton-charts/kube-state-metrics \
--set 'collectors.namespaces={default,kube-system}'
# Install with production configuration
helm install my-ksm scripton-charts/kube-state-metrics \
-f values-small-prod.yaml
Prerequisites
- Kubernetes 1.19+
- Helm 3.0+
- (Optional) Prometheus Operator for ServiceMonitor
Configuration
Key Parameters
| Parameter | Description | Default |
|---|---|---|
replicaCount | Number of replicas | 1 |
collectors.namespaces | Limit to specific namespaces | [] (all) |
collectors.resources | Limit to specific resources | [] (all) |
serviceMonitor.enabled | Enable ServiceMonitor | false |
rbac.create | Create RBAC resources | true |
Available Resources
Pods, Nodes, Deployments, StatefulSets, DaemonSets, ReplicaSets, Jobs, CronJobs, Services, Endpoints, Ingresses, PersistentVolumes, PersistentVolumeClaims, ConfigMaps, Secrets, Namespaces, ResourceQuotas, LimitRanges, HorizontalPodAutoscalers, PodDisruptionBudgets, NetworkPolicies, StorageClasses, VolumeAttachments, CertificateSigningRequests, and more.
Operational Commands
Basic Operations
# View logs
make -f make/ops/kube-state-metrics.mk ksm-logs
# Open shell
make -f make/ops/kube-state-metrics.mk ksm-shell
# Restart
make -f make/ops/kube-state-metrics.mk ksm-restart
Health & Status
# Check status
make -f make/ops/kube-state-metrics.mk ksm-status
# Check version
make -f make/ops/kube-state-metrics.mk ksm-version
# Check health
make -f make/ops/kube-state-metrics.mk ksm-health
Metrics Queries
# Get all metrics
make -f make/ops/kube-state-metrics.mk ksm-metrics
# Get pod metrics
make -f make/ops/kube-state-metrics.mk ksm-pod-metrics
# Get deployment metrics
make -f make/ops/kube-state-metrics.mk ksm-deployment-metrics
# Get node metrics
make -f make/ops/kube-state-metrics.mk ksm-node-metrics
# Get service metrics
make -f make/ops/kube-state-metrics.mk ksm-service-metrics
# Get persistent volume metrics
make -f make/ops/kube-state-metrics.mk ksm-pv-metrics
# Get namespace metrics
make -f make/ops/kube-state-metrics.mk ksm-namespace-metrics
Resource Status
# Check pod status metrics
make -f make/ops/kube-state-metrics.mk ksm-pod-status
# Check deployment status metrics
make -f make/ops/kube-state-metrics.mk ksm-deployment-status
# Check node status metrics
make -f make/ops/kube-state-metrics.mk ksm-node-status
Port Forward
# Port forward metrics endpoint
make -f make/ops/kube-state-metrics.mk ksm-port-forward
# Port forward telemetry endpoint
make -f make/ops/kube-state-metrics.mk ksm-port-forward-telemetry
# Then visit http://localhost:8080/metrics
Configuration Examples
Monitor Specific Namespaces
collectors:
namespaces:
- default
- kube-system
- production
Monitor Specific Resources
collectors:
resources:
- pods
- deployments
- services
- nodes
- persistentvolumeclaims
Add Resource Labels to Metrics
extraArgs:
- --metric-labels-allowlist=pods=[*],deployments=[*],statefulsets=[*]
- --metric-annotations-allowlist=namespaces=[*]
Integration with Prometheus
Manual Scraping
Add to Prometheus configuration:
scrape_configs:
- job_name: 'kube-state-metrics'
static_configs:
- targets: ['kube-state-metrics.default.svc.cluster.local:8080']
Using ServiceMonitor
serviceMonitor:
enabled: true
interval: 30s
labels:
prometheus: kube-prometheus
Example Metrics
Pod Metrics
# Total pods by namespace
count(kube_pod_info) by (namespace)
# Pod status
kube_pod_status_phase{phase="Running"}
# Container restarts
rate(kube_pod_container_status_restarts_total[5m])
# Pod resource requests
kube_pod_container_resource_requests{resource="cpu"}
Deployment Metrics
# Deployment replicas
kube_deployment_status_replicas
# Available replicas
kube_deployment_status_replicas_available
# Unavailable replicas
kube_deployment_status_replicas_unavailable
Node Metrics
# Node status
kube_node_status_condition{condition="Ready",status="true"}
# Allocatable resources
kube_node_status_allocatable{resource="cpu"}
# Node info
kube_node_info
Persistent Volume Metrics
# PV capacity
kube_persistentvolume_capacity_bytes
# PVC requests
kube_persistentvolumeclaim_resource_requests_storage_bytes
# PV status
kube_persistentvolume_status_phase{phase="Bound"}
PromQL Query Examples
# Pods not in Running state
kube_pod_status_phase{phase!="Running"} == 1
# Deployments with insufficient replicas
kube_deployment_status_replicas_available < kube_deployment_spec_replicas
# Nodes not ready
kube_node_status_condition{condition="Ready",status="false"} == 1
# PVCs in pending state
kube_persistentvolumeclaim_status_phase{phase="Pending"} == 1
# Container CPU requests by namespace
sum(kube_pod_container_resource_requests{resource="cpu"}) by (namespace)
# Total storage requests by namespace
sum(kube_persistentvolumeclaim_resource_requests_storage_bytes) by (namespace)
Grafana Dashboards
Recommended Grafana dashboards for kube-state-metrics:
- Kubernetes Cluster Monitoring (ID: 7249)
- Kubernetes Cluster Overview (ID: 12117)
- Kubernetes Resource Requests (ID: 13332)
Import via: Grafana UI → Dashboards → Import → Enter ID
Production Setup
# values-prod.yaml
replicaCount: 1
rbac:
create: true
serviceAccount:
create: true
collectors:
namespaces: [] # All namespaces
resources: [] # All resources
extraArgs:
- --metric-labels-allowlist=pods=[*],deployments=[*],statefulsets=[*]
- --metric-annotations-allowlist=namespaces=[*]
serviceMonitor:
enabled: true
interval: 30s
labels:
prometheus: kube-prometheus
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
tolerations:
- effect: NoSchedule
operator: Exists
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- kube-state-metrics
topologyKey: kubernetes.io/hostname
priorityClassName: "system-cluster-critical"
Troubleshooting
Check Metrics Collection
make -f make/ops/kube-state-metrics.mk ksm-health
make -f make/ops/kube-state-metrics.mk ksm-metrics | head -20
RBAC Issues
# Check ClusterRole
kubectl get clusterrole kube-state-metrics
# Check ClusterRoleBinding
kubectl get clusterrolebinding kube-state-metrics
# Check ServiceAccount
kubectl get serviceaccount kube-state-metrics -n <namespace>
High Memory Usage
Reduce the number of monitored resources:
collectors:
resources:
- pods
- deployments
- nodes
Or limit to specific namespaces:
collectors:
namespaces:
- production
- staging
Missing Metrics
Check if resources are enabled:
# Get available resources
kubectl api-resources
Ensure RBAC has permissions for the resource type.
Sharding (Large Clusters)
For very large clusters (1000+ nodes), use sharding:
# Shard 0
replicaCount: 1
extraArgs:
- --shard=0
- --total-shards=3
# Shard 1
replicaCount: 1
extraArgs:
- --shard=1
- --total-shards=3
# Shard 2
replicaCount: 1
extraArgs:
- --shard=2
- --total-shards=3
License
- Chart: BSD 3-Clause License
- Kube State Metrics: Apache License 2.0
Additional Resources
Maintained by: ScriptonBasestar Chart Version: 0.3.0 Kube State Metrics Version: 2.10.1
Deploy kube-state-metrics with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has kube-state-metrics on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartkube-state-metrics is pinned at v0.3.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply