使用 Go 客户端获取 Kubernetes 中 Pod 的当前资源使用情况 [英] Get current resource usage of a pod in Kubernetes with Go client
问题描述
kubernetes go 客户端有很多方法,我找不到如何获取当前 CPU 和特定(或所有 Pod)的 RAM 使用情况.
The kubernetes go client has tons of methods and I can't find how I can get the current CPU & RAM usage of a specific (or all pods).
有人能告诉我需要调用哪些方法来获取 pods 的当前使用情况吗?节点?
Can someone tell me what methods I need to call to get the current usage for pods & nodes?
我的节点列表:
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
Kubernetes Go 客户端:https://github.com/kubernetes/client-go
Kubernetes Go Client: https://github.com/kubernetes/client-go
指标包:https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/metrics
据我所知,指标服务器实现了 Kubernetes 指标包,以便从 pod 和节点获取资源使用情况,但我不知道在哪里 &他们是如何做到的:https://github.com/kubernetes-incubator/metrics-server
As far as I got the metrics server implements the Kubernetes metrics package in order to fetch the resource usage from pods and nodes, but I couldn't figure out where & how they do it: https://github.com/kubernetes-incubator/metrics-server
推荐答案
go-client 不支持 metrics 类型是正确的,但是在 metrics 包中有一个预先生成的 client 可用于获取指标对象和立即将它们分配给适当的结构.您首先需要做的唯一一件事是生成一个配置并将其传递给指标客户端.所以一个简单的指标客户端看起来像这样:
It is correct that go-client does not have support for metrics type, but in the metrics package there is a pregenerated client that can be used for fetching metrics objects and assign them right away to the appropriate structure. The only thing you need to do first is to generate a config and pass it to metrics client. So a simple client for metrics would look like this:
package main
import (
"k8s.io/client-go/tools/clientcmd"
metrics "k8s.io/metrics/pkg/client/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main() {
var kubeconfig, master string //empty, assuming inClusterConfig
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil{
panic(err)
}
mc, err := metrics.NewForConfig(config)
if err != nil {
panic(err)
}
mc.MetricsV1beta1().NodeMetricses().Get("your node name", metav1.GetOptions{})
mc.MetricsV1beta1().NodeMetricses().List(metav1.ListOptions{})
mc.MetricsV1beta1().PodMetricses(metav1.NamespaceAll).List(metav1.ListOptions{})
mc.MetricsV1beta1().PodMetricses(metav1.NamespaceAll).Get("your pod name", metav1.GetOptions{})
}
metric 客户端的上述每个方法都返回一个适当的结构(您可以检查那些 此处)和一个错误(如果有),您应该根据您的要求进行处理.
Each of the above methods from metric client returns an appropriate structure (you can check those here) and an error (if any) which you should process according to your requirements.
这篇关于使用 Go 客户端获取 Kubernetes 中 Pod 的当前资源使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!