当前位置: 首页 > 软件库 > 云计算 > 云原生 >

kube-state-metrics

授权协议 Apache-2.0 License
开发语言 Google Go
所属分类 云计算、 云原生
软件类型 开源软件
地区 不详
投 递 者 澹台正真
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Overview

Build Status

kube-state-metrics (KSM) is a simple service that listens to the Kubernetes APIserver and generates metrics about the state of the objects. (See examples inthe Metrics section below.) It is not focused on the health of the individualKubernetes components, but rather on the health of the various objects inside,such as deployments, nodes and pods.

kube-state-metrics is about generating metrics from Kubernetes API objectswithout modification. This ensures that features provided by kube-state-metricshave the same grade of stability as the Kubernetes API objects themselves. Inturn, this means that kube-state-metrics in certain situations may not show theexact same values as kubectl, as kubectl applies certain heuristics to displaycomprehensible messages. kube-state-metrics exposes raw data unmodified from theKubernetes API, this way users have all the data they require and performheuristics as they see fit.

The metrics are exported on the HTTP endpoint /metrics on the listening port(default 8080). They are served as plaintext. They are designed to be consumedeither by Prometheus itself or by a scraper that is compatible with scraping aPrometheus client endpoint. You can also open /metrics in a browser to seethe raw metrics. Note that the metrics exposed on the /metrics endpointreflect the current state of the Kubernetes cluster. When Kubernetes objectsare deleted they are no longer visible on the /metrics endpoint.

Table of Contents

Kubernetes Version

kube-state-metrics uses client-go to talk withKubernetes clusters. The supported Kubernetes cluster version is determined by client-go.The compatibility matrix for client-go and Kubernetes cluster can be foundhere.All additional compatibility is only best effort, or happens to still/already be supported.

Compatibility matrix

At most, 5 kube-state-metrics and 5 kubernetes releases will be recorded below.

kube-state-metrics Kubernetes 1.18 Kubernetes 1.19 Kubernetes 1.20 Kubernetes 1.21 Kubernetes 1.22
v1.9.8 - - - - -
v2.0.0 -/✓ -/✓ -/✓
v2.1.1 -/✓ -/✓
v2.2.1 -/✓
master -/✓
  • Fully supported version range.
  • - The Kubernetes cluster has features the client-go library can't use (additional API objects, deprecated APIs, etc).

Note: The current kube-state-metrics v2.0.0 + releases work on Kubernetes v1.17 & v1.18 excluding Ingress or CertificateSigningRequest resource metrics. If you require those metrics on an older Kubernetes version, use kube-state-metrics v1.9.8.

Resource group version compatibility

Resources in Kubernetes can evolve, i.e., the group version for a resource may change from alpha to beta and finally GAin different Kubernetes versions. For now, kube-state-metrics will only use the oldest API available in the latestrelease.

Container Image

The latest container image can be found at:

  • k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.2.1 (arch: amd64, arm, arm64, ppc64le and s390x)

Metrics Documentation

Any resources and metrics based on alpha Kubernetes APIs are excluded from any stability guarantee,which may be changed at any given release.

See the docs directory for more information on the exposed metrics.

Conflict resolution in label names

The *_labels family of metrics exposes Kubernetes labels as Prometheus labels.As Kubernetesis more liberal thanPrometheusin terms of allowed characters in label names,we automatically convert unsupported characters to underscores.For example, app.kubernetes.io/name becomes label_app_kubernetes_io_name.

This conversion can create conflicts when multiple Kubernetes labels likefoo-bar and foo_bar would be converted to the same Prometheus label label_foo_bar.

Kube-state-metrics automatically adds a suffix _conflictN to resolve this conflict,so it converts the above labels tolabel_foo_bar_conflict1 and label_foo_bar_conflict2.

If you'd like to have more control over how this conflict is resolved,you might want to consider addressing this issue on a different level of the stack,e.g. by standardizing Kubernetes labels using anAdmission Webhookthat ensures that there are no possible conflicts.

Enabling VerticalPodAutoscalers

Please note that the collector for verticalpodautoscalers is disabled by default; Vertical Pod Autoscaler metrics will not be collected until the collector is enabled. This is because Vertical Pod Autoscalers are managed as custom resources.

If you want to enable this collector,the instructions are located in the Vertical Pod Autoscaler Metrics documentation.

Kube-state-metrics self metrics

kube-state-metrics exposes its own general process metrics under --telemetry-host and --telemetry-port (default 8081).

kube-state-metrics also exposes list and watch success and error metrics. These can be used to calculate the error rate of list or watch resources.If you encounter those errors in the metrics, it is most likely a configuration or permission issue, and the next thing to investigate would be lookingat the logs of kube-state-metrics.

Example of the above mentioned metrics:

kube_state_metrics_list_total{resource="*v1.Node",result="success"} 1
kube_state_metrics_list_total{resource="*v1.Node",result="error"} 52
kube_state_metrics_watch_total{resource="*v1beta1.Ingress",result="success"} 1

kube-state-metrics also exposes some http request metrics, examples of those are:

http_request_duration_seconds_bucket{handler="metrics",method="get",le="2.5"} 30
http_request_duration_seconds_bucket{handler="metrics",method="get",le="5"} 30
http_request_duration_seconds_bucket{handler="metrics",method="get",le="10"} 30
http_request_duration_seconds_bucket{handler="metrics",method="get",le="+Inf"} 30
http_request_duration_seconds_sum{handler="metrics",method="get"} 0.021113919999999998
http_request_duration_seconds_count{handler="metrics",method="get"} 30

kube-state-metrics also exposes build and configuration metrics:

kube_state_metrics_build_info{branch="master",goversion="go1.15.3",revision="6c9d775d",version="v2.0.0-beta"} 1
kube_state_metrics_shard_ordinal{shard_ordinal="0"} 0
kube_state_metrics_total_shards 1

kube_state_metrics_build_info is used to expose version and other build information. For more usage about the info pattern,please check the blog post here.Sharding metrics expose --shard and --total-shards flags and can be used to validaterun-time configuration, see /examples/prometheus-alerting-rules.

Scaling kube-state-metrics

Resource recommendation

Resource usage for kube-state-metrics changes with the Kubernetes objects (Pods/Nodes/Deployments/Secrets etc.) size of the cluster.To some extent, the Kubernetes objects in a cluster are in direct proportion to the node number of the cluster.

As a general rule, you should allocate:

  • 250MiB memory
  • 0.1 cores

Note that if CPU limits are set too low, kube-state-metrics' internal queues will not be able to be worked off quickly enough, resulting in increased memory consumption as the queue length grows. If you experience problems resulting from high memory allocation or CPU throttling, try increasing the CPU limits.

Latency

In a 100 node cluster scaling test the latency numbers were as follows:

"Perc50": 259615384 ns,
"Perc90": 475000000 ns,
"Perc99": 906666666 ns.

A note on costing

By default, kube-state-metrics exposes several metrics for events across your cluster. If you have a large number of frequently-updating resources on your cluster, you may find that a lot of data is ingested into these metrics. This can incur high costs on some cloud providers. Please take a moment to configure what metrics you'd like to expose, as well as consult the documentation for your Kubernetes environment in order to avoid unexpectedly high costs.

kube-state-metrics vs. metrics-server

The metrics-serveris a project that has been inspired byHeapster and is implementedto serve the goals of core metrics pipelines in Kubernetes monitoringarchitecture.It is a cluster level component which periodically scrapes metrics from allKubernetes nodes served by Kubelet through Summary API. The metrics areaggregated, stored in memory and served in Metrics APIformat. Themetrics-server stores the latest values only and is not responsible forforwarding metrics to third-party destinations.

kube-state-metrics is focused on generating completely new metrics fromKubernetes' object state (e.g. metrics based on deployments, replica sets,etc.). It holds an entire snapshot of Kubernetes state in memory andcontinuously generates new metrics based off of it. And just like themetrics-server it too is not responsible for exporting its metrics anywhere.

Having kube-state-metrics as a separate project also enables access to thesemetrics from monitoring systems such as Prometheus.

Horizontal sharding

In order to shard kube-state-metrics horizontally, some automated sharding capabilities have been implemented. It is configured with the following flags:

  • --shard (zero indexed)
  • --total-shards

Sharding is done by taking an md5 sum of the Kubernetes Object's UID and performing a modulo operation on it with the total number of shards. Each shard decides whether the object is handled by the respective instance of kube-state-metrics or not. Note that this means all instances of kube-state-metrics, even if sharded, will have the network traffic and the resource consumption for unmarshaling objects for all objects, not just the ones they are responsible for. To optimize this further, the Kubernetes API would need to support sharded list/watch capabilities. In the optimal case, memory consumption for each shard will be 1/n compared to an unsharded setup. Typically, kube-state-metrics needs to be memory and latency optimized in order for it to return its metrics rather quickly to Prometheus. One way to reduce the latency between kube-state-metrics and the kube-apiserver is to run KSM with the --use-apiserver-cache flag. In addition to reducing the latency, this option will also lead to a reduction in the load on etcd.

Sharding should be used carefully and additional monitoring should be set up in order to ensure that sharding is set up and functioning as expected (eg. instances for each shard out of the total shards are configured).

Automated sharding

Automatic sharding allows each shard to discover its nominal position when deployed in a StatefulSet which is useful for automatically configuring sharding. This is an experimental feature and may be broken or removed without notice.

To enable automated sharding, kube-state-metrics must be run by a StatefulSet and the pod name and namespace must be handed to the kube-state-metrics process via the --pod and --pod-namespace flags. Example manifests demonstrating the autosharding functionality can be found in /examples/autosharding.

This way of deploying shards is useful when you want to manage KSM shards through a single Kubernetes resource (a single StatefulSet in this case) instead of having one Deployment per shard. The advantage can be especially significant when deploying a high number of shards.

The downside of using an auto-sharded setup comes from the rollout strategy supported by StatefulSets. When managed by a StatefulSet, pods are replaced one at a time with each pod first getting terminated and then recreated. Besides such rollouts being slower, they will also lead to short downtime for each shard. If a Prometheus scrape happens during a rollout, it can miss some of the metrics exported by kube-state-metrics.

Setup

Install this project to your $GOPATH using go get:

go get k8s.io/kube-state-metrics

Building the Docker container

Simply run the following command in this root folder, which will create aself-contained, statically-linked binary and build a Docker image:

make container

Usage

Simply build and run kube-state-metrics inside a Kubernetes pod which has aservice account token that has read-only access to the Kubernetes cluster.

For users of prometheus-operator/kube-prometheus stack

The (kube-prometheus) stack installs kube-state-metrics as one of its components; you do not need to install kube-state-metrics if you're using the kube-prometheus stack.

If you want to revise the default configuration for kube-prometheus, for example to enable non-default metrics, have a look at Customizing Kube-Prometheus.

Kubernetes Deployment

To deploy this project, you can simply run kubectl apply -f examples/standard and aKubernetes service and deployment will be created. (Note: Adjust the apiVersion of some resource if your kubernetes cluster's version is not 1.8+, check the yaml file for more information).

To have Prometheus discover kube-state-metrics instances it is advised to create a specific Prometheus scrape config for kube-state-metrics that picks up both metrics endpoints. Annotation based discovery is discouraged as only one of the endpoints would be able to be selected, plus kube-state-metrics in most cases has special authentication and authorization requirements as it essentially grants read access through the metrics endpoint to most information available to it.

Note: Google Kubernetes Engine (GKE) Users - GKE has strict role permissions that will prevent the kube-state-metrics roles and role bindings from being created. To work around this, you can give your GCP identity the cluster-admin role by running the following one-liner:

kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin --user=$(gcloud info --format='value(config.account)')

Note that your GCP identity is case sensitive but gcloud info as of Google Cloud SDK 221.0.0 is not. This means that if your IAM member contains capital letters, the above one-liner may not work for you. If you have 403 forbidden responses after running the above command and kubectl apply -f examples/standard, check the IAM member associated with your account at https://console.cloud.google.com/iam-admin/iam?project=PROJECT_ID. If it contains capital letters, you may need to set the --user flag in the command above to the case-sensitive role listed at https://console.cloud.google.com/iam-admin/iam?project=PROJECT_ID.

After running the above, if you see Clusterrolebinding "cluster-admin-binding" created, then you are able to continue with the setup of this service.

Limited privileges environment

If you want to run kube-state-metrics in an environment where you don't have cluster-reader role, you can:

  • create a serviceaccount
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kube-state-metrics
  namespace: your-namespace-where-kube-state-metrics-will-deployed
  • give it view privileges on specific namespaces (using roleBinding) (note: you can add this roleBinding to all the NS you want your serviceaccount to access)
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: kube-state-metrics
  namespace: project1
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: view
subjects:
  - kind: ServiceAccount
    name: kube-state-metrics
    namespace: your-namespace-where-kube-state-metrics-will-deployed
  • then specify a set of namespaces (using the --namespaces option) and a set of kubernetes objects (using the --resources) that your serviceaccount has access to in the kube-state-metrics deployment configuration
spec:
  template:
    spec:
      containers:
      - name: kube-state-metrics
        args:
          - '--resources=pods'
          - '--namespaces=project1'

For the full list of arguments available, see the documentation in docs/cli-arguments.md

Helm Chart

Starting from the kube-state-metrics chart v2.13.3 (kube-state-metrics image v1.9.8), the official Helm chart is maintained in prometheus-community/helm-charts. Starting from kube-state-metrics chart v3.0.0 only kube-state-metrics images of v2.0.0 + are supported.

Development

When developing, test a metric dump against your local Kubernetes cluster byrunning:

Users can override the apiserver address in KUBE-CONFIG file with --apiserver command line.

go install
kube-state-metrics --port=8080 --telemetry-port=8081 --kubeconfig=<KUBE-CONFIG> --apiserver=<APISERVER>

Then curl the metrics endpoint

curl localhost:8080/metrics

To run the e2e tests locally see the documentation in tests/README.md.

Developer Contributions

When developing, there are certain code patterns to follow to better your contributing experience and likelihood of e2e and other ci tests to pass. To learn more about them, see the documentation in docs/developer/guide.md.

  • k8s部署kube-state-metrics 1. 需要的yaml文件 1.1 kube-state-metrics-rbac.yaml apiVersion: v1 kind: ServiceAccount metadata: name: kube-state-metrics namespace: monitoring --- apiVersion: rbac.authorizatio

  • kube-state-metrics 指标数据 1、kube-state-metrics说明 kube-state-metrics关注于获取k8s各种资源的最新状态,如deployment或者daemonset,之所以没有把kube-state-metrics纳入到metric-server的能力中,是因为他们的关注点本质上是不一样的。metric-server仅仅是获取、格式化现有数据,写入特定

  • 1、kube-state-metrics 当部署了metrics-server、cadvisor(集成在kubelet内)监控指标基本都已经能拿到,但是这些都是在应用内部,需要在应用提供or开启/metrics接口,或者部署exports来暴漏对应的指标,但是对于deployment,Pod、daemonset、cronjob等k8s资源对象并没有监控,比如:当前replace是多少?Pod当前状

 相关资料
  • kubernetes_sd_config(在prometheus中)是否提供了我可以使用metrics-server和kube-state-metrics获得的所有数据? kubernetes_sd_config是否足以进行监视? metrics-server只是为了向内部组件(如hpa控制器)提供数据(小于kubernetes_sd_config)吗? 是只针对对象(pod、deployment

  • Kube 足够的简单,足够小,具有很强的自适应能力,是个响应式的 CSS 框架。它拥有最新最炫的网格和漂亮的字体排版,没有任何样式绑定,给用户以绝对的自由。 支持的浏览器包括: Latest Chrome Latest Firefox Latest Safari Latest Opera IE 8+ 手机浏览器

  • Kube-OVN 将基于 OVN/OVS 的网络虚拟化方案带入 Kubernetes,提供了针对企业应用场景的高级容器网络编排功能。 主要功能: 基于Namespace的子网划分,以及网络控制 容器固定 IP IPv6支持 细粒度网络策略 动态 QoS 分布式和集中式网关 内嵌负载均衡器 支持集群内外网络直通 控制平面的灾备及高可用 丰富的监控和链路追踪工具 未来计划: 基于 XDP/DPDK/O

  • kube-eventer 是一个事件发射器,它将 Kubernetes 事件发送到接收器(例如,DingTalk、SLS、Kafka 等)。 监控是保障系统稳定性的重要组成部分,在 Kubernetes 开源生态中,资源类的监控工具与组件百花齐放,但是,只有资源类的监控是远远不够的,因为资源监控存在如下两个主要的缺欠: 监控的实时性与准确性不足 监控的场景覆盖范围不足 Kubernetes 的核心

  • kube-backup Quick 'n dirty kubernetes state backup script, designed to be ran as kubernetes Job. Think of it like RANCID for kubernetes. Props to @gianrubio for coming up with the idea. Setup Use the

  • kube-ps1: Kubernetes prompt for bash and zsh A script that lets you add the current Kubernetes context and namespaceconfigured on kubectl to your Bash/Zsh prompt strings (i.e. the $PS1). Inspired by s