使用Go客户端获取Kubernetes中Pod的当前资源使用情况 [英] Get current resource usage of a pod in Kubernetes with Go client

查看:1526
本文介绍了使用Go客户端获取Kubernetes中Pod的当前资源使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).

有人可以告诉我我需要调用什么方法才能获得Pod&的当前用法.节点?

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不支持指标类型是正确的,但是指标包中有一个预先生成的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆