在新版本的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.
如果APIService里指定了svc,那么请求kube-apiserver的web路径的时候kube-apiserver会把请求转发到你选中的svc上,这里可以看官方文件里一部分内容。当我们使用kubectl top node的时候实际是请求kube-apiserver的url路径/apis/metrics.k8s.io/v1beta1/nodes,由于创建了metrics-server的APIService,请求会被转发到svc的pod,pod工作流程是获取node列表,然后去请求node上的kubelet的metrics端口获取metrics信息收集起来,信息包括了node的基本cpu和内存以及上面跑的pod的cpu和内存。这之前流量是kube-apiserver到pod上中间经过svc的ip,如果没有kube-proxy和网络组件就无法通信。
利用nginx反向代理来理解:
1 2 3
location/apis/metrics.k8s.io/ { to svc pod; }
小细节:(使用如下命令可查看详细的请求)
[root@master-1 1.8+]# kubectl -v=8 top node
首先在k8s-m1测试一下 kubectl top 指令:
1 2
[root@master-1 ~]# kubectl top node Error from server (NotFound): the server could not find the requested resource (get services http:heapster:)
发现 top 命令无法取得 Metrics,这表示 Kubernetes 集群没有安装 Heapster 或着 Metrics Server 来提供 Metrics API 给 top 指令取得资源使用量。
由于上述问题,我们要在k8s-m1部署 Metrics Server来解决:
1 2 3 4 5 6 7 8 9
[root@master-1 1.8+]# ll total 28 -rw-r----- 1 root root 384 Mar 15 16:42 aggregated-metrics-reader.yaml -rw-r----- 1 root root 308 Mar 15 16:42 auth-delegator.yaml -rw-r----- 1 root root 329 Mar 15 16:42 auth-reader.yaml -rw-r----- 1 root root 298 Mar 15 16:42 metrics-apiservice.yaml -rw-r----- 1 root root 947 Mar 20 11:59 metrics-server-deployment.yaml -rw-r----- 1 root root 249 Mar 15 16:42 metrics-server-service.yaml -rw-r----- 1 root root 502 Mar 15 16:42 resource-reader.yaml
[root@master-11.8+]# kubectl get pod -n kube-system -l k8s-app=metrics-server NAME READY STATUS RESTARTS AGE metrics-server-7b5c5864ff-zr2k7 1/1 Running 04h1m
完成后,等待一段时间(约 30s - 1m)收集 Metrics,再次执行 kubectl top 指令查看:
1 2 3 4 5
[root@master-11.8+]# kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master-1389m4% 3908Mi 3% node-0129m1% 2582Mi 2% node-1193m2% 19716Mi 17%
DESCRIPTION: spec is the specification for the behaviour of the autoscaler. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status.
HorizontalPodAutoscalerSpec describes the desired functionality of the HorizontalPodAutoscaler.
FIELDS: maxReplicas <integer> -required- maxReplicas is the upperlimitfor the number of replicas to which the autoscaler can scale up. It cannot be less that minReplicas.
metrics <[]Object> metrics contains the specifications for which touseto calculate the desired replica count (the maximum replica count across all metrics will be used). The desired replica countis calculated multiplying the ratio between the target valueand the current valueby the current number of pods. Ergo, metrics used must decrease as the pod countis increased, and vice-versa. See the individual metric source types for more information about how eachtype of metric must respond.
minReplicas <integer> minReplicas is the lowerlimitfor the number of replicas to which the autoscaler can scale down. It defaults to1 pod.
scaleTargetRef <Object> -required- scaleTargetRef points to the target resource to scale, andis used to the pods for which metrics should be collected, as well asto actually change the replica count.