Kubernetes的Metrics API和Metrics Server

2018-10-22 阅读: Kubernetes

Kubernetes 1.8关于资源使用情况的metrics(例如容器的CPU和内存),可以通过Metrics API获取到。 前面在做Kubernetes 1.11的升级工作时,Kubernetes 1.11已经废弃heapster那套监控的东东。因此是时候了解一下Kubernetes的Metrics API和Metrics Server了。

Metrics Server的安装

这里使用Heml来部署metrics-server,chat模板的值文件metrics-server.yaml如下:

args:
- --logtostderr
- --kubelet-insecure-tls

将其安装到Kubernetes的kube-system名字空间:

helm install stable/metrics-server \
-n metrics-server \
--namespace kube-system \
-f metrics-server.yaml

部署完成后使用下面的命令查看node相关的指标:

 kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"
{"kind":"NodeMetricsList","apiVersion":"metrics.k8s.io/v1beta1","metadata":{"selfLink":"/apis/metrics.k8s.io/v1beta1/nodes"},"items":[]}

没有获取到信息,此时查看metric-server容器的日志,有下面的错误:

E1003 05:46:13.757009       1 manager.go:102] unable to fully collect metrics: [unable to fully scrape metrics from source kubelet_summary:node1: unable to fetch metrics from Kubelet node1 (node1): Get https://node1:10250/stats/summary/: dial tcp: lookup node1 on 10.96.0.10:53: no such host, unable to fully scrape metrics from source kubelet_summary:node2: unable to fetch metrics from Kubelet node2 (node2): Get https://node2:10250/stats/summary/: dial tcp: lookup node2 on 10.96.0.10:53: read udp 10.244.1.6:45288->10.96.0.10:53: i/o timeout]

可以看到metrics-server在从kubelet的10250端口获取信息时,使用的是hostname,而因为node1和node2是一个独立的Kubernetes演示环境,只是修改了这两个节点系统的/etc/hosts文件,而并没有内网的DNS服务器,所以metrics-server中不认识node1和node2的名字。这里我们可以直接修改Kubernetes集群中的coredns的configmap,修改Corefile加入hostnames插件,将Kubernetes的各个节点的主机名加入到hostnames中,这样Kubernetes集群中的所有Pod都可以从CoreDNS中解析各个节点的名字。

kubectl edit configmap coredns -n kube-system

apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health
        hosts {
           192.168.61.11 node1
           192.168.61.12 node2
           fallthrough
        }
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           upstream
           fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        proxy . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
kind: ConfigMap

配置修改完毕后重启集群中coredns和metrics-server,确认metrics-server不再有错误日志。

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"

Metrics API

Metrics Server从Kubernetes集群中每个Node上kubelet的API收集metrics数据。通过Metrics API可以获取Kubernetes资源的Metrics指标,Metrics API挂载/apis/metrics.k8s.io/ 下。 可以使用kubectl top命令访问Metrics API,例如:

kubectl top node
NAME    CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
node1   140m         14%    1285Mi          73%
node2   37m          3%     458Mi           26%
kubectl top pod --all-namespaces
NAMESPACE       NAME                                             CPU(cores)   MEMORY(bytes)
ingress-nginx   nginx-ingress-controller-77fc55d6dd-hmlmt        3m           90Mi
ingress-nginx   nginx-ingress-controller-77fc55d6dd-htms6        2m           84Mi
ingress-nginx   nginx-ingress-default-backend-684f76869d-pxlmz   1m           1Mi
kube-system     coredns-576cbf47c7-mlfcd                         2m           13Mi
kube-system     coredns-576cbf47c7-xgqdd                         2m           10Mi
kube-system     etcd-node1                                       13m          86Mi
kube-system     kube-apiserver-node1                             23m          514Mi
kube-system     kube-controller-manager-node1                    26m          54Mi
kube-system     kube-flannel-ds-amd64-8rcq4                      1m           18Mi
kube-system     kube-flannel-ds-amd64-mhx9t                      2m           14Mi
kube-system     kube-proxy-nljs8                                 2m           30Mi
kube-system     kube-proxy-pjdsj                                 2m           19Mi
kube-system     kube-scheduler-node1                             10m          18Mi
kube-system     kubernetes-dashboard-5746dd4544-gtj65            1m           28Mi
kube-system     metrics-server-8854b78d9-nx9tx                   1m           12Mi
kube-system     tiller-deploy-6f6fd74b68-mc2cw                   1m           27Mi

参考

标题:Kubernetes的Metrics API和Metrics Server
本文链接:https://blog.frognew.com/2018/10/kubernetes-metrics-api-and-metrics-server.html
转载请注明出处。

目录