kubernetes(23):安装部署资源收集插件metrics-server
安装部署资源收集插件metrics-server
https://blog.csdn.net/bbwangj/article/details/82832883
1 metrics-server简介
从 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
如果使用kubernetes的自动扩容功能的话,那首先得安装metrics-seve,将收集到的信息(cpu、memory..)与自动扩容的设置的值进行比对,自动调整pod数量。
2 下载
git clone https://github.com/kubernetes-incubator/metrics-server cd metrics-server/deploy/1.8+
3 修改metrics-server-deployment.yaml
#要不error: metrics not available yet错误
#修改增加了资源限制和参数,修改镜像地址
--- 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
4 部署
kubectl create -f metrics-server/deploy/1.8+/
5 验证
服务验证
kubectl get pods -n kube-system|grep metrics-server [root@k8s-master 1.8+]# kubectl get pods -n kube-system|grep metrics-server metrics-server-84998789f5-8j4pq 1/1 Running 0 63m [root@k8s-master 1.8+]#
命令验证-取得CPU和内存值
[root@k8s-master 1.8+]# kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master 241m 6% 1569Mi 19% k8s-node-1 42m 1% 2276Mi 28% k8s-node-2 36m 0% 662Mi 8% [root@k8s-master 1.8+]# kubectl top pods NAME CPU(cores) MEMORY(bytes) nginx-76546c5b7d-5sf8k 0m 2Mi [root@k8s-master 1.8+]#
验证API
重开窗口
[root@k8s-master ~]# kubectl proxy --port=8080 Starting to serve on 127.0.0.1:8080 [root@k8s-master 1.8+]# 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": "k8s-node-1", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/k8s-node-1", "creationTimestamp": "2019-08-29T06:35:05Z" }, "timestamp": "2019-08-29T06:35:02Z", "window": "30s", "usage": { "cpu": "61050370n", "memory": "2318092Ki" } }, { "metadata": { "name": "k8s-node-2", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/k8s-node-2", "creationTimestamp": "2019-08-29T06:35:05Z" }, "timestamp": "2019-08-29T06:34:56Z", "window": "30s", "usage": { "cpu": "35726510n", "memory": "666300Ki" } }, { "metadata": { "name": "k8s-master", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/k8s-master", "creationTimestamp": "2019-08-29T06:35:05Z" }, "timestamp": "2019-08-29T06:35:02Z", "window": "30s", "usage": { "cpu": "245064781n", "memory": "1605312Ki" } } ] }[root@k8s-master 1.8+]#