I&B Monitoring Platform documentation

Kube state metrics

To enable metrics expose from kubernetes with kube state metrics you need to deploy it in kubernetes with helm and set value

Bash
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 monitoring

after this deploy your kubernetes expose metrics in kubernetes on http://kube-state-metrics.monitoring.svc.cluster.local:8080/metrics

For access with basic auth and encryption from outside the kubernetes you need to deploy kubernetes ingress object with basic autentification and tls encryption. For this do.

  1. Create a Secret for Basic Authentication

First, generate a .htpasswd file containing the username and a hashed password. For example, to create a user named prometheus with a password, use the htpasswd utility:

htpasswd -c auth prometheus

This command will prompt for a password and create a file named auth in the current directory. Next, create a Kubernetes Secret from this file:

kubectl create secret generic basic-auth --from-file=auth

This creates a Secret named basic-auth in your current namespace, containing the auth file as a key in its data.

  1. Create self signed certificate as its created in section Authentication

  2. Create kubernetes secret from this certificate

kubectl create secret tls kube-state-local-tls \
  --cert=exporter.crt \
  --key=exporter.key

Create the yaml file of ingress with tls and basic auth

nano kube-state-metrics.yaml

and paste into opened file

Bash
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kube-state-metrics
  annotations:
    nginx.ingress.kubernetes.io/auth-type: basic  #basic auth annotation
    nginx.ingress.kubernetes.io/auth-secret: basic-auth   #basic auth annotation
    nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - user'   #basic auth annotation
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - kube-state-metrics.com
      secretName: kube-state-local-tls
  rules:
  - host: kube-state-metrics.com
    http:
      paths:
      - path: /metrics
        pathType: Prefix
        backend:
          service:
            name: kube-state-metrics
            port:
              number: 8080

Save and exit from file. And create ingress object. For this run

kubectl apply -f kube-state-metrics.yaml

After deploy ingress, metrics of your kubernetes will exposed on url: https://kube-state-metrics.com/metrics

after login to this URL you will see some like this:

image-20251006-122900.png

Enjoy!)