如何为HPA自动缩放指标启用KubeAPI服务器

时间:2018-12-11 13:33:59

标签: kubernetes kubernetes-apiserver

我正在使用Kube v1.13.0版本。由于Heapster从v1.11开始贬值,因此我一直无法为集群指标启用API服务器来实现HPA。

Attached Image for reference

有人可以指导我逐步启用API Metrics服务器或任何演示视频。进一步进行将非常有帮助。

请让我知道是否需要进一步的信息。

谢谢 迪娜

1 个答案:

答案 0 :(得分:7)

我已经能够使用metrics-server来实现HPA,因为不提倡使用stackster。我已按照以下步骤操作:

  1. 克隆度量服务器github存储库:git clone https://github.com/kubernetes-incubator/metrics-server.git

进入目录cd deploy/1.8+并运行以下yaml文件:

[root@ip-10-0-1-91 1.8+]# kubectl apply -f aggregated-metrics-reader.yaml 
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f auth-reader.yaml 
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f auth-delegator.yaml 
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f metrics-apiservice.yaml 
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f resource-reader.yaml 
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f metrics-server-deployment.yaml 
serviceaccount/metrics-server created
deployment.extensions/metrics-server created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f metrics-server-service.yaml 
service/metrics-server created

现在创建要测试自动缩放的pod(取自kubernetes官方文档):

[root@ip-10-0-1-91 auto]#  kubectl run --generator=run-pod/v1 php-apache -- 
image=k8s.gcr.io/hpa-example --requests=cpu=200m --expose --port=80
service/php-apache created
deployment.apps/php-apache created

现在创建自动扩展部署:

[root@ip-10-0-1-91 auto]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
horizontalpodautoscaler.autoscaling/php-apache autoscaled

现在检查HPA,您的指标是否达到:

[root@ip-10-0-1-91 manifests]# kubectl get hpa
NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   0%/50%    1         10        1          2m

现在使用以下命令从另一个窗口生成负载:

kubectl run -i --tty load-generator --image=busybox /bin/sh

它将打开一个sh终端,您可以使用以下命令从该sh终端运行负载:

while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done

需要花一分钟左右的时间才能在您的吊舱上承受足够的负载,并且您会看到一声繁荣:

[root@ip-10-0-1-91 manifests]# kubectl get hpa
NAME         REFERENCE               TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   120%/50%   1         10        4          7m

吊舱缩放:

enter image description here

希望这有助于使HPA正常工作。

编辑:

用以下yaml文件替换metrics-server-deployment.yaml中的deploy/1.8+文件:

 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:
       - command:
         - /metrics-server
         - --metric-resolution=30s
         - --kubelet-insecure-tls
         - --kubelet-preferred-address-types=InternalIP
         name: metrics-server
         image: k8s.gcr.io/metrics-server-amd64:v0.3.1
         imagePullPolicy: Always
         volumeMounts:
         - name: tmp-dir
           mountPath: /tmp

此外,在kubelet.conf中启用--authentication-token-webhook,然后您将能够获得HPA。

EDIT2:您需要在要为其创建HPA的部署文件(在您的情况下为tomcat)中设置以下属性,然后只有HPA才能从部署中获取指标。

resources:
  requests:
    memory: "64Mi"
    cpu: "250m"
  limits:
    memory: "128Mi"
    cpu: "500m"