【问题标题】:How to get kubernetes resource information(overall CPU and memory usage) through APIs如何通过 API 获取 kubernetes 资源信息(整体 CPU 和内存使用情况)
【发布时间】:2019-12-06 16:33:00
【问题描述】:

我已经在 VIM 中安装了 minikube,并且我拥有具有所有权限的服务帐户令牌。是否有任何来自 kubernetes 的 API 来获取资源使用情况(总体)。

【问题讨论】:

  • kubectl top node 呢?
  • @IvanAracki 我正在寻找 API

标签: kubernetes google-kubernetes-engine kubectl minikube kubernetes-pod


【解决方案1】:

如果您安装 kubernetes 指标服务器,它会将这些指标公开为 api https://github.com/kubernetes-incubator/metrics-server

【讨论】:

    【解决方案2】:

    要获取 CPU 和内存使用情况,您可以使用(取决于您希望查看的对象)以下内容:

    kubectl top pods 要么 kubectl top nodes 它会告诉你

    $ kubectl top pods
    NAME                       CPU(cores)   MEMORY(bytes)
    nginx-1-5d4f8f66d9-xmhnh   0m           1Mi
    

    API 参考可能如下所示:

    $ curl http://localhost:8080/apis/metrics.k8s.io/v1beta1/pods

    ...
    {
          "metadata": {
            "name": "nginx-1-5d4f8f66d9-xmhnh",
            "namespace": "default",
            "selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/nginx-1-5d4f8f66d9-xmhnh",
            "creationTimestamp": "2019-07-29T11:48:13Z"
          },
          "timestamp": "2019-07-29T11:48:11Z",
          "window": "30s",
          "containers": [
            {
              "name": "nginx",
              "usage": {
                "cpu": "0",
                "memory": "1952Ki"
              }
            }
          ]
        }
    ...
    

    对于 API,访问它的方法很少。

    你可以通过运行kubectl proxy --port:8080 &来使用proxy

    以下命令以充当反向代理的模式运行 kubectl。它负责定位 API 服务器并进行身份验证。

    更多详情请见kubectl proxy

    然后您可以使用 curl、wget 或浏览器探索 API,如下所示:

    curl http://localhost:8080/api/

    您可以使用身份验证令牌访问它without proxy

    可以通过将身份验证令牌直接传递给 API 服务器来避免使用 kubectl 代理,如下所示:

    使用grep/cut 方法:

    # Check all possible clusters, as you .KUBECONFIG may have multiple contexts:
    kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
    
    # Select name of cluster you want to interact with from above output:
    export CLUSTER_NAME="some_server_name"
    
    # Point to the API server refering the cluster name
    APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
    
    # Gets the token value
    TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 -d)
    
    # Explore the API with TOKEN
    curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
    

    您还可以使用多个官方客户端库访问 API,例如 GoPython。其他库可以看here

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 2019-01-09
      • 2019-08-04
      • 1970-01-01
      相关资源
      最近更新 更多