gpt4 book ai didi

kubernetes - 如何通过 API 获取 kubernetes 资源信息(整体 CPU 和内存使用情况)

转载 作者:行者123 更新时间:2023-12-02 12:03:34 29 4
gpt4 key购买 nike

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

最佳答案

要获取 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,访问它的方法很少。

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

The following command runs kubectl in a mode where it acts as a reverse proxy. It handles locating the API server and authenticating.

See kubectl proxy for more details.

Then you can explore the API with curl, wget, or a browser, like so:

curl http://localhost:8080/api/



您可以访问它 without proxy通过使用身份验证 token 。

It is possible to avoid using kubectl proxy by passing an authentication token directly to the API server, like this:

Using grep/cut approach:



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

关于kubernetes - 如何通过 API 获取 kubernetes 资源信息(整体 CPU 和内存使用情况),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57252073/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com