要获取 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,例如 Go 或 Python。其他库可以看here。