从 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


安装metrics-server


用到的yaml文件:

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
  - nodes/stats
  - namespaces

开始运行:

kubectl apply -f .

查看spi-versions是否有metrics资源:要等一会

]# kubectl api-versions | grep metrics
metrics.k8s.io/v1beta1

查看pods:

]# kubectl get pods -n kube-system
NAME                                    READY     STATUS    RESTARTS   AGE
coredns-78fcdf6894-q2ls6                1/1       Running   6          22h
coredns-78fcdf6894-qn4gl                1/1       Running   6          22h
etcd-master                             1/1       Running   32         22h
kube-apiserver-master                   1/1       Running   32         22h
kube-controller-manager-master          1/1       Running   32         22h
kube-flannel-ds-amd64-b267q             1/1       Running   5          21h
kube-flannel-ds-amd64-qp5br             1/1       Running   5          21h
kube-flannel-ds-amd64-z6845             1/1       Running   5          21h
kube-proxy-8gwbn                        1/1       Running   6          21h
kube-proxy-d2t7j                        1/1       Running   6          22h
kube-proxy-lbqfp                        1/1       Running   5          21h
kube-scheduler-master                   1/1       Running   32         21h
metrics-server-v0.2.1-84678c956-44rj5   2/2       Running   0          6m  # 这个Pod

测试是否安装成功:

master新开一个反向代理端口:

~]# kubectl proxy --port=8080

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"
      }
    }
  ]
}

查看资源使用情况:

]# kubectl top nodes
NAME      CPU(cores)   CPU%      MEMORY(bytes)   MEMORY%   
master    174m         8%        872Mi           50%       
node1     25m          1%        338Mi           19%       
node2     38m          1%        375Mi           21%

kubectl top 命令可以使用了说明metrics-server 安装成功。