如何通过API获取kubernetes资源信息(总体CPU和内存使用情况)
How to get kubernetes resource information(overall CPU and memory usage) through APIs
我已经在 VIM 中安装了 minikube,并且我拥有具有所有权限的服务帐户令牌。是否有来自 kubernetes 的 API 来获取资源使用情况(总体)。
如果您安装 kubernetes 指标服务器,它会将这些指标公开为 api https://github.com/kubernetes-incubator/metrics-server
要获取 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
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。
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,例如 Go or Python. Other libraries are available to see here。
我已经在 VIM 中安装了 minikube,并且我拥有具有所有权限的服务帐户令牌。是否有来自 kubernetes 的 API 来获取资源使用情况(总体)。
如果您安装 kubernetes 指标服务器,它会将这些指标公开为 api https://github.com/kubernetes-incubator/metrics-server
要获取 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 &
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。
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,例如 Go or Python. Other libraries are available to see here。