Kubernetes部署metrics-server监控集群性能

说明

  • Metrics Server
    是实现了 Metrics API 的元件,其目标是取代 Heapster 作位 Pod 与 Node 提供资源的 Usage metrics,该元件会从每个 Kubernetes 节点上的 Kubelet 所公开的 Summary API 中收集 Metrics
  • Horizontal Pod Autoscaler(HPA)控制器用于实现基于CPU使用率进行自动Pod伸缩的功能。
  • HPA控制器基于Master的kube-controller-manager服务启动参数–horizontal-pod-autoscaler-sync-period定义是时长(默认30秒),周期性监控目标Pod的CPU使用率,并在满足条件时对ReplicationController或deployment中的Pod副本数进行调整,以符合用户定义的平均Pod CPU使用率。
  • 在新版本的kubernetes中 Pod CPU使用率不在来源于heapster,而是来自于metrics-server
  • 官网原话是 The –horizontal-pod-autoscaler-use-rest-clients is true or unset. Setting this to false switches to Heapster-based autoscaling, which is deprecated.

准备工作

kube-apiserver运行参数

metric-server是扩展的apiserver,依赖于kube-aggregator,因此需要在apiserver中开启相关参数

1
2
3
4
5
6
7
--requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.pem
--proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.pem
--proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client-key.pem
--requestheader-allowed-names=aggregator
--requestheader-group-headers=X-Remote-Group
--requestheader-extra-headers-prefix=X-Remote-Extra-
--requestheader-username-headers=X-Remote-User

metrics-server版本兼容

Metrics ServerMetrics API group/versionSupported Kubernetes version
0.3.xmetrics.k8s.io/v1beta11.8+
0.2.xmetrics.k8s.io/v1beta11.8+
0.1.xmetrics/v1alpha11.7

metrics-server部署

创建工作目录

1
mkdir -p /home/k8s/

下载项目代码

1
wget -O - https://github.com/kubernetes-incubator/metrics-server/archive/v0.3.1.tar.gz | tar xz --directory=/home/k8s/

修改deployment

文件路径/home/k8s/metrics-server-v0.3.1/deploy/1.8+/metrics-server-deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
containers:
- name: metrics-server
image: gcrxio/metrics-server-amd64:v0.3.1
command:
- /metrics-server
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS
- --kubelet-insecure-tls
- --requestheader-extra-headers-prefix=x-remote-extra-
- --requestheader-group-headers=x-remote-group
- --requestheader-username-headers=x-remote-user
- -v=2

部署metrics-server

1
kubectl apply -f /home/k8s/metrics-server-v0.3.1/deploy/1.8+/

查看pod状态

1
2
3
kubectl -n kube-system get pod -l k8s-app=metrics-server
NAME READY STATUS RESTARTS AGE
pod/metrics-server-86bd9d7667-5hbn6 1/1 Running 0 1m

验证metrics

完成后,等待一段时间(约 30s - 1m)收集 Metrics

  • 请求metrics api的结果
1
kubectl get --raw /apis/metrics.k8s.io/v1beta1

示例输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"kind": "APIResourceList",
"apiVersion": "v1",
"groupVersion": "metrics.k8s.io/v1beta1",
"resources": [
{
"name": "nodes",
"singularName": "",
"namespaced": false,
"kind": "NodeMetrics",
"verbs": [
"get",
"list"
]
},
{
"name": "pods",
"singularName": "",
"namespaced": true,
"kind": "PodMetrics",
"verbs": [
"get",
"list"
]
}
]
}
  • 获取节点性能数据
1
kubectl top node

输出示例

1
2
3
4
5
6
NAME     CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
k8s-m1 695m 17% 914Mi 11%
k8s-m2 360m 9% 553Mi 7%
k8s-m3 492m 12% 533Mi 6%
k8s-n1 144m 3% 311Mi 3%
k8s-n2 149m 3% 321Mi 4%
  • 获取Pod性能数据
1
kubectl -n kube-system top pod

输出示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
NAME                                    CPU(cores)   MEMORY(bytes)   
coredns-56c964478c-g7q8n 14m 14Mi
coredns-56c964478c-zmt2j 13m 14Mi
kube-flannel-ds-amd64-8g6f9 6m 15Mi
kube-flannel-ds-amd64-hslkh 6m 21Mi
kube-flannel-ds-amd64-ppqkb 8m 14Mi
kube-flannel-ds-amd64-swj8m 7m 14Mi
kube-flannel-ds-amd64-zs2sd 8m 17Mi
kube-proxy-2dq5x 18m 23Mi
kube-proxy-9lzmj 28m 23Mi
kube-proxy-9xtjc 18m 23Mi
kube-proxy-w7mtg 2m 23Mi
kube-proxy-zvp8d 2m 21Mi
metrics-server-v0.3.1-98bdfb766-s6jf6 8m 22Mi