restful api訪問k8s集羣,增刪改查信息,做界面二次開發。
需要預先創建訪問權限的配置。
官網api文檔
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/
下面羅列部分api
curl -u admin:admin "https://localhost:6443/api/v1" -k
curl -u admin:admin "https://localhost:6443/api/v1/pods" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/{namespace}/pods" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default/pods" -k
獲取節點信息
curl -u admin:admin "https://localhost:6443/api/v1/nodes/{nodename}" -k
curl -u admin:admin "https://localhost:6443/api/v1/nodes/tensorflow1" -k
...
"status": {
"capacity": {
"cpu": "4",
"memory": "7970316Ki",
"pods": "110"
},
"allocatable": {
"cpu": "4",
"memory": "7867916Ki",
"pods": "110"
},
...
獲取namespace信息
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/{namespace}" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default" -k
獲得quota信息
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/{namespace}/resourcequotas/" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default/resourcequotas/" -k
實踐
k8s_master_ip:192.168.1.138
username 不同用戶不同
password 不同用戶不同
namespace 不同用戶不同
api地址
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/
版本更新到v1.10以後 上面這個鏈接就找不到了 要把v1.9改成v1.10才能訪問。
查看容器
curl -u {username}:{password} "https://{k8s_master_ip}:6443/api/v1/namespaces/{namespace}/pods/" -k
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/default/pods/" -k
看起來像是把所有的pod都拿出來了,包括活的和死的。
看了一下信息很多不過沒有資源使用信息。
"phase": "Running"
這個是正在運行的pod
"phase": "Failed"
"reason":"Evicted"
這種是刪除了的,狀態是failed 原因是被驅逐
增加continue參數取出正在運行的容器
curl -u {username}:{password} "https://{k8s_master_ip}:6443/api/v1/namespaces/{namespace}/pods?continue" -k
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/default/pods?continue" -k
查看replicationcontroller
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/user1/replicationcontrollers/" -k
查看service
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/user1/services/" -k
查看資源總覽resourcequotas
curl -u {username}:{password} "https://{k8s_master_ip}:6443/api/v1/namespaces/{namespace}/resourcequotas/" -k
[root@tensorflow1 info]# curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default/resourcequotas/" -k
...
"status": {
"hard": {
"limits.cpu": "2",
"limits.memory": "6Gi",
"pods": "20",
"requests.cpu": "1",
"requests.memory": "1Gi"
},
"used": {
"limits.cpu": "400m",
"limits.memory": "1Gi",
"pods": "2",
"requests.cpu": "200m",
"requests.memory": "512Mi"
}
}
...
hard是限額 used是當前申請的限額
limits 和 requests 的區別是 limits是上限,不能突破,但不保證能給。 requests是下限,保證能給。 舉例說明:一個容器 requests.memory 512Mi,limits.memory 1Gi。宿主機內存使用量高時,一定會留512Mi內存給這個容器,不一定能拿到1Gi內存。宿主機內存使用量低時,容器不能突破1Gi內存。
Gi 和 G 的區別是 Gi是1024進制,G是1000進制,M Mi也是同理。就像一個U盤8G但實際能使用的是7.45G(其實這裏單位就是Gi)
pods是指容器,單位個
cpu單位 m指千分之一,200m即0.2個cpu。這是絕對值,不是相對值。比如0.1CPU不管是在單核或者多核機器上都是一樣的,都嚴格等於0.1CPU core
實時數據
官方文檔
https://kubernetes.io/docs/tasks/debug-application-cluster/core-metrics-pipeline/
https://github.com/kubernetes/metrics
https://github.com/kubernetes-incubator/metrics-server
下載 metrics-server 壓縮包文件
下載 googlecontainer/metrics-server-amd64:v0.2.0
cd metrics-server-0.2.1/deploy
修改 metrics-server-deployment.yaml 文件 image 和 imagePullPolicy: IfNotPresent
kubectl create -f .
獲取節點信息
curl -u {username}:{password} "https://{k8s_master_ip}:6443/apis/metrics.k8s.io/v1beta1/nodes" -k
curl -u admin:admin "https://192.168.1.138:6443/apis/metrics.k8s.io/v1beta1/nodes" -k
{
"kind": "NodeMetricsList",
"apiVersion": "metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes"
},
"items": [
...
{
"metadata": {
"name": "tensorflow1",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/tensorflow1",
"creationTimestamp": "2018-04-09T08:44:17Z"
},
"timestamp": "2018-04-09T08:44:00Z",
"window": "1m0s",
"usage": {
"cpu": "265m",
"memory": "3448228Ki"
}
}
...
]
}
獲取pod信息
curl -u {username}:{password} "https://{k8s_master_ip}:6443/apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pods" -k
curl -u admin:admin "https://192.168.1.138:6443/apis/metrics.k8s.io/v1beta1/namespaces/default/pods" -k
{
"kind": "PodMetricsList",
"apiVersion": "metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods"
},
"items": [
...
{
"metadata": {
"name": "tensorflow-worker-rc-998wf",
"namespace": "default",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/tensorflow-worker-rc-998wf",
"creationTimestamp": "2018-04-09T08:52:38Z"
},
"timestamp": "2018-04-09T08:52:00Z",
"window": "1m0s",
"containers": [
{
"name": "worker",
"usage": {
"cpu": "0",
"memory": "39964Ki"
}
}
]
}
...
]
}
獲取namespace信息
沒找到url,就把上面獲取pod的使用量全加起來就是namespace的使用量了
Metrics API 文檔
網上找不到文檔 只能從 kubectl top 命令幫助裏找
[root@tensorflow1 ~]# kubectl top
Display Resource (CPU/Memory/Storage) usage.
The top command allows you to see the resource consumption for nodes or pods.
This command requires Heapster to be correctly configured and working on the server.
Available Commands:
node Display Resource (CPU/Memory/Storage) usage of nodes
pod Display Resource (CPU/Memory/Storage) usage of pods
Usage:
kubectl top [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@tensorflow1 ~]# kubectl top pod --help
Display Resource (CPU/Memory/Storage) usage of pods.
The 'top pod' command allows you to see the resource consumption of pods.
Due to the metrics pipeline delay, they may be unavailable for a few minutes since pod creation.
Aliases:
pod, pods, po
Examples:
# Show metrics for all pods in the default namespace
kubectl top pod
# Show metrics for all pods in the given namespace
kubectl top pod --namespace=NAMESPACE
# Show metrics for a given pod and its containers
kubectl top pod POD_NAME --containers
# Show metrics for the pods defined by label name=myLabel
kubectl top pod -l name=myLabel
Options:
--all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
context is ignored even if specified with --namespace.
--containers=false: If present, print usage of containers within a pod.
--heapster-namespace='kube-system': Namespace Heapster service is located in
--heapster-port='': Port name in service to use
--heapster-scheme='http': Scheme (http or https) to connect to Heapster as
--heapster-service='heapster': Name of Heapster service
-l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
Usage:
kubectl top pod [NAME | -l label] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
棄用的數據獲取
參考 https://jimmysong.io/posts/using-heapster-to-get-object-metrics/
官方api文檔 https://github.com/kubernetes/heapster/blob/master/docs/model.md 棄用了
棄用的api取值 https://blog.csdn.net/mofiu/article/details/77126848
獲取heapster url
[root@tensorflow1 influxdb]kubectl cluster-info
Kubernetes master is running at https://192.168.1.138:6443
Heapster is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
monitoring-grafana is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
monitoring-influxdb is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy/api/v1/model/namespaces/" -k
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy/api/v1/model/namespaces/default/metrics" -k
[
"memory/request",
"memory/limit",
"cpu/usage_rate",
"memory/usage",
"cpu/request",
"cpu/limit"
]
[root@tensorflow1 influxdb]# curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy/api/v1/model/namespaces/default/metrics/memory/usage" -k
{
"metrics": [
...
{
"timestamp": "2018-04-09T07:45:00Z",
"value": 81121280
},
{
"timestamp": "2018-04-09T07:46:00Z",
"value": 81121280
}
...
],
"latestTimestamp": "2018-04-09T07:46:00Z"
}
本文轉自CSDN-k8s restful api 訪問