kubernetes 中部署metrics-server

环境:

  • kubeadm v1.13.4部署

从 v1.8 开始,资源使用情况的度量(如容器的 CPU 和内存使用)可以通过 Metrics API 获取。注意

  • Metrics API 只可以查询当前的度量数据,并不保存历史数据
  • Metrics API URI 为 /apis/metrics.k8s.io/,在 k8s.io/metrics 维护
  • 必须部署 metrics-server 才能使用该 API,metrics-server 通过调用 Kubelet Summary API 获取数据

开始部署metrics-server

克隆github上的项目

git clone https://github.com/kubernetes-incubator/metrics-server.git

项目克隆下来,官方的教程里面非常简单,直接kubectl apply -f 1.8+/就好了。但是实际操作中远不止这些,我在操作过程中就出现证书错误,无法访问metircs等问题。

error: metrics not available yet

查看日志后发现各种证书错误,获取不到每个node的10250端口。

所以我们需要对官方的deployment文件进行修改下:

$ cd metrics-server/deploy/1.8+

$ ls
aggregated-metrics-reader.yaml  auth-delegator.yaml  auth-reader.yaml  metrics-apiservice.yaml  metrics-server-deployment.yaml  metrics-server-service.yaml  resource-reader.yaml

里面有一个deployment文件,原内容如下:

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metrics-server
  namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: metrics-server
  namespace: kube-system
  labels:
    k8s-app: metrics-server
spec:
  selector:
    matchLabels:
      k8s-app: metrics-server
  template:
    metadata:
      name: metrics-server
      labels:
        k8s-app: metrics-server
    spec:
      serviceAccountName: metrics-server
      volumes:
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
      - name: tmp-dir
        emptyDir: {}
      containers:
      - name: metrics-server
        image: k8s.gcr.io/metrics-server-amd64:v0.3.1
        imagePullPolicy: Always
        volumeMounts:
        - name: tmp-dir
          mountPath: /tmp

将其修改一下,修改后如下:

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metrics-server
  namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: metrics-server
  namespace: kube-system
  labels:
    k8s-app: metrics-server
spec:
  selector:
    matchLabels:
      k8s-app: metrics-server
  template:
    metadata:
      name: metrics-server
      labels:
        k8s-app: metrics-server
    spec:
      serviceAccountName: metrics-server
      volumes:
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
      - name: tmp-dir
        emptyDir: {}
      containers:
      - name: metrics-server
        image: mirrorgooglecontainers/metrics-server-amd64:v0.3.1
        imagePullPolicy: IfNotPresent
        command:
        - /metrics-server
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalDNS,InternalIP,ExternalDNS,ExternalIP,Hostname
        volumeMounts:
        - name: tmp-dir
          mountPath: /tmp
        resources:
          limits:
            cpu: 300m
            memory: 200Mi
          requests:
            cpu: 200m
            memory: 100Mi

修改后我们增加了资源限制和参数, 默认镜像是需要梯子的,这里改成国内可访问的镜像仓库,资源限制的requests其实还可以在低点。

kubectl apply -f metrics-server/deploy/1.8+/

等待几分钟后就可以查看node信息了

$ kubectl top node
NAME          CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
10.1.65.104   119m         2%     3982Mi          50%
10.1.65.105   119m         2%     2502Mi          31%
10.1.65.110   63m          1%     1676Mi          21%
10.1.65.111   61m          1%     1634Mi          20%
10.1.65.123   86m          2%     1476Mi          18%
10.1.65.124   81m          2%     1480Mi          18%
10.1.65.125   56m          1%     1421Mi          18%
10.1.65.126   105m         2%     1474Mi          18%
10.1.65.135   104m         2%     660Mi           8%
10.1.65.136   66m          1%     1424Mi          18%
10.1.65.137   89m          2%     1459Mi          18%
10.1.65.76    183m         4%     2361Mi          29%
10.1.65.77    121m         3%     2150Mi          27%
10.1.65.78    183m         4%     2579Mi          32%
10.1.65.79    1967m        24%    10222Mi         64%
10.1.65.80    76m          0%     8165Mi          51%
10.1.65.81    737m         9%     8537Mi          53%
10.1.65.82    129m         3%     694Mi           8%
10.1.65.83    162m         4%     4657Mi          59%
10.1.65.84    117m         2%     4649Mi          59%
10.1.65.85    135m         3%     692Mi           8%
10.1.65.86    145m         3%     4554Mi          57%
10.1.65.88    215m         2%     11599Mi         72%
10.1.65.89    1894m        23%    13540Mi         84%

你可能感兴趣的:(kubernetes 中部署metrics-server)