kubernetes 如何启用KubeAPI服务器进行HPA自动缩放

yxyvkwin  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(121)

我使用的是Kube v1.13.0版本。由于Heapster从v1.11开始就被贬低了,我一直在为集群服务器启用API服务器来实现HPA。

root:~# kubectl get pods,deployment,hpa
NAME                                   READY   STATUS    RESTARTS   AGE
pod/ctauthorization-754c686bc6-6adkx   1/1     Running   0          72s
pod/ctauthorization-754c686bc6-dpsbm   1/1     Running   0          57s
pod/ctauthorization-754c686bc6-qsavs   1/1     Running   0          72s

NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/ctauthorization   3/3     3            3           72s

NAME                                                  REFERENCE                    TARGETS                           MINPODS   MAXEODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/ctauthorization   Deployment/ctauthorization   <unknown>/1000Mi, <unknown>/50%   3         5         3          72s

有人可以指导我一步一步地启用API服务器或任何演示视频。如果能继续下去会很有帮助。
请让我知道如果任何进一步的信息需要。
谢谢蒂娜

oalqel3c

oalqel3c1#

我可以使用metrics-server实现HPA,因为heapster是折旧的。我遵循了以下步骤:
1.克隆metrics-server github repo: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

和豆荚缩放:

希望这有助于你的HPA工作。
编辑:
deploy/1.8+中的metrics-server-deployment.yaml文件替换为以下yaml文件:

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。
编辑2:您需要在要为其创建HPA的部署文件(在本例中为tomcat)中设置以下属性,然后只有您的HPA可以从您的部署中获取指标。

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

相关问题