如何设置使用metrics-server获取HPA的CPU使用率?
# kubectl top nodes
错误:指标尚不可用
# kubectl get pods -n kube-system
NAME READY STATUS重启年龄
heapster-709db6bd48-f2gba 2/2运行0 6h
metrics-server-70647b8f8b-99pja 1/1运行0 5h
.....
# kubectl get hpa
名称参考目标MINPODS MAXPODS REPLICAS年龄
devops-deployment部署/ devops-deployment <未知> / 50%4 10 4 1h
答案 0 :(得分:0)
我和你有同样的问题。 对我有帮助的是this post。
-删除指标服务器
-更改metrics-server / deploy / 1.8 + / metrics-server-deployment.yaml
-再次应用
-或简单地
更多详细信息如下:
kubectl delete -f metrics-server/deploy/1.8+
编辑metrics-server/deploy/1.8+/metrics-server-deployment.yaml
并添加下一个选项:
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
metrics-server-deployment.yaml应该如下所示,或直接从here复制并粘贴
---
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
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
volumeMounts:
- name: tmp-dir
mountPath: /tmp
再次应用指标服务器文件:kubectl apply -f metrics-server/deploy/1.8+
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader 被创造 clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator 被创造 rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader 创建了apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io 创建的serviceaccount / metrics-server已创建 deployment.extensions / metrics-server创建的service / metrics-server 创建了clusterrole.rbac.authorization.k8s.io/system:metrics-server 被创造 clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server 已创建
并检查结果:
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-deployment Deployment/nginx-deployment <unknown>/80% 3 10 10 25
kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
kube-master-1 255m 12% 2582Mi 35%
kube-worker-1 124m 6% 2046Mi 27
kubectl top pods
NAME CPU(cores) MEMORY(bytes)
nginx-deployment-76bf4969df-4bbdc 0m 2Mi
nginx-deployment-76bf4969df-5m6xc 0m 2Mi
nginx-deployment-76bf4969df-b4zh7 0m 2Mi
nginx-deployment-76bf4969df-c58wl 0m 2Mi
nginx-deployment-76bf4969df-cktcg 0m 2Mi
nginx-deployment-76bf4969df-fbjj9 0m 2Mi
nginx-deployment-76bf4969df-gh94w 0m 2Mi
nginx-deployment-76bf4969df-qx6ld 0m 2Mi
nginx-deployment-76bf4969df-rvt54 0m 2Mi
nginx-deployment-76bf4969df-vq9gs 0m 2Mi
此外,如果要基于资源的利用率在Pod上使用自动缩放,则Pod需要提供资源限制,否则它将无法计算已使用资源的百分比。如果部署的Pod规范没有资源限制,则需要将HPA设置为基于绝对值(包括单位)进行扩展。对于CPU,您可以将其作为字符串设置为300m
。例如,对于RAM,可以将其设置为400Mi
。缺乏资源限制可以解释为什么在current
下运行kubectl get hpa
时看不到指标的TARGETS
值。