从 Kubernetes 1.8 开始,资源使用指标(如容器 CPU 和内存使用率)通过 Metrics API 在 Kubernetes 中获取, metrics-server 替代了heapster。Metrics Server 实现了Resource Metrics API,Metrics Server 是集群范围资源使用数据的聚合器。
Metrics Server 从每个节点上的 Kubelet 公开的 Summary API 中采集指标信息。
kubernetes metrics server 参考文档 https://github.com/kubernetes-incubator/metrics-server
会用到的yaml文件:
https://github.com/kubernetes-incubator/metrics-server/tree/master/deploy/1.8%2B 或者
https://github.com/kubernetes/kubernetes/tree/release-1.11/cluster/addons/metrics-server(建议使用)
下载六个文件并创建
for file in auth-delegator.yaml auth-reader.yaml metrics-apiservice.yaml metrics-server-deployment.yaml metrics-server-service.yaml resource-reader.yaml;do wget https://raw.githubusercontent.com/kubernetes/kubernetes/release-1.11/cluster/addons/metrics-server/$file; done
需要修改的地方:
metrics-server-deployment.yaml #
- --source=kubernetes.summary_api:''
- --source=kubernetes.summary_api:https://kubernetes.default?kubeletHttps=true&kubeletPort=10250&insecure=true
resource-reader.yaml#
resources:
- pods
- nodes
- namespaces
- nodes/stats #新加
开始运行: kubectl apply -f .
[root@master metrics]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1 apps/v1beta1
apps/v1beta2 authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
crd.projectcalico.org/v1
events.k8s.io/v1beta1
extensions/v1beta1
metrics.k8s.io/v1beta1
#metrics控制器,有说明成功
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1 v1
查看pods
[root@master metrics]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-78fcdf6894-cj6tn 1/1 Running 42 26d
coredns-78fcdf6894-wfvk8 1/1 Running 42 26d
elasticsearch-logging-0 1/1 Running 45 4d
elasticsearch-logging-1 1/1 Running 47 4d
etcd-master 1/1 Running 42 26d
grafana-7f8bcdfbbf-qzln5 1/1 Running 4 13d
kibana-logging-7444956bf8-x8qqd 1/1 Running 2 4d
kube-apiserver-master 1/1 Running 47 26d
kube-controller-manager-master 1/1 Running 43 26d
kube-flannel-ds-m4f4j 1/1 Running 33 26d
kube-flannel-ds-xvssj 1/1 Running 18 26d
kube-proxy-5lw6z 1/1 Running 39 26d
kube-proxy-qlhg7 1/1 Running 18 26d
kube-scheduler-master 1/1 Running 39 26d
kubernetes-dashboard-767dc7d4d-4bt48 1/1 Running 5 5d
metrics-server-v0.2.1-84678c956-hbz2b 2/2 Running 0 2m
#这个状态是正常的
master新开一个反向代理端口
[root@master ~]# kubectl proxy --port=8080
Starting to serve on 127.0.0.1:8080
master新开一个窗口查看
[root@master ~]# curl http://localhost:8080/apis/metrics.k8s.io/v1beta1/nodes
{
"kind": "NodeMetricsList",
"apiVersion": "metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes"
},
"items": [
{
"metadata": {
"name": "master",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/master",
"creationTimestamp": "2018-09-25T09:48:21Z"
},
"timestamp": "2018-09-25T09:48:00Z",
"window": "1m0s",
"usage": {
"cpu": "211m",
"memory": "2905388Ki"
}
},
{
"metadata": {
"name": "node01",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node01",
"creationTimestamp": "2018-09-25T09:48:21Z"
},
"timestamp": "2018-09-25T09:48:00Z",
"window": "1m0s",
"usage": {
"cpu": "150m",
"memory": "3670276Ki"
}
}
]
}
[root@master metrics]# kubectl top nodes
error: metrics not available yet
说明还未成功,需要等待一会
查看node
[root@master metrics]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 207m 10% 2832Mi 76%
node01 144m 4% 3619Mi 37%
查看pods
[root@master metrics]# kubectl top pods -n kube-system
NAME CPU(cores) MEMORY(bytes)
coredns-78fcdf6894-cj6tn 2m 11Mi
coredns-78fcdf6894-wfvk8 1m 11Mi
elasticsearch-logging-0 6m 1347Mi
elasticsearch-logging-1 6m 1326Mi
etcd-master 17m 84Mi
grafana-7f8bcdfbbf-qzln5 0m 21Mi
kibana-logging-7444956bf8-x8qqd 4m 83Mi
kube-apiserver-master 89m 486Mi
kube-controller-manager-master 19m 59Mi
kube-flannel-ds-m4f4j 5m 14Mi
kube-flannel-ds-xvssj 5m 12Mi
kube-proxy-5lw6z 3m 15Mi
kube-proxy-qlhg7 3m 16Mi
kube-scheduler-master 6m 13Mi
kubernetes-dashboard-767dc7d4d-4bt48 0m 11Mi
metrics-server-v0.2.1-84678c956-hbz2b 1m 24Mi