{"id":117,"date":"2024-03-13T23:53:00","date_gmt":"2024-03-13T15:53:00","guid":{"rendered":"https:\/\/www.xueyaa.top\/?p=117"},"modified":"2026-03-10T22:07:33","modified_gmt":"2026-03-10T14:07:33","slug":"hpa%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.xueyaa.top\/?p=117","title":{"rendered":"hpa\u5b9e\u6218"},"content":{"rendered":"\n<p>1.\u90e8\u7f72\u4fee\u4ed9\u4e1a\u52a1\uff0c\u8981\u6c42\u526f\u672c\u6570\u91cf\u4e3a1;<br>2.\u4f7f\u7528ab\u6d4b\u8bd5\u5de5\u5177\u5bf9\u4fee\u4ed9\u4e1a\u52a1\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5;<br>3.\u8bf7\u5b9e\u73b0Pod\u6c34\u5e73\u4f38\u7f29\uff0c\u5f53cpu\u538b\u529b\u8fbe\u523095%\u6216\u8005\u5185\u5b58\u8fbe\u523090%\u65f6\u81ea\u52a8\u8fdb\u884c\u6269\u5bb9pod\u526f\u672c\u6570\u91cf(\u4e0a\u9650\u6570\u91cf10)\u3002<\/p>\n\n\n\n<pre id=\"TVMrA\" class=\"wp-block-code\"><code>\t1.\u7f16\u5199\u8d44\u6e90\u6e05\u5355\n&#91;root@master231 horizontalpodautoscalers]# cat 03-deploy-hpa-xiuxian.yaml \napiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: deploy-xiuxian\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      apps: xiuxian\n  template:\n    metadata:\n      labels:\n        apps: xiuxian\n    spec:\n      containers:\n      - name: c1\n        image: registry.cn-hangzhou.aliyuncs.com\/yinzhengjie-k8s\/apps:v1 \n        resources:\n          requests:\n            memory: 100Mi\n            cpu: 100m\n\n---\n\napiVersion: autoscaling\/v2\nkind: HorizontalPodAutoscaler\nmetadata:\n  name: hpa-memory\nspec:\n  minReplicas: 2\n  maxReplicas: 10\n  metrics:\n  - type: Resource\n    resource:\n      name: memory\n      target: \n        type: Utilization\n        averageUtilization: 90\n  scaleTargetRef:\n    apiVersion: apps\/v1\n    kind: Deployment\n    name: deploy-xiuxian\n\n\n---\n\napiVersion: autoscaling\/v1\nkind: HorizontalPodAutoscaler\nmetadata:\n  name: hpa-cpu\nspec:\n  maxReplicas: 10\n  minReplicas: 2\n  scaleTargetRef:\n    apiVersion: apps\/v1\n    kind: Deployment\n    name: deploy-xiuxian\n  targetCPUUtilizationPercentage: 95\n\n\n---\n\napiVersion: v1\nkind: Service\nmetadata:\n  name: svc-xiuxian\nspec:\n  type: LoadBalancer\n  selector:\n    apps: xiuxian\n  ports:\n  - port: 80\n&#91;root@master231 horizontalpodautoscalers]# \n\n\n\t2.\u521b\u5efa\u8d44\u6e90\n&#91;root@master231 horizontalpodautoscalers]# kubectl apply -f  03-deploy-hpa-xiuxian.yaml \ndeployment.apps\/deploy-xiuxian created\nhorizontalpodautoscaler.autoscaling\/hpa-memory created\nhorizontalpodautoscaler.autoscaling\/hpa-cpu created\nservice\/svc-xiuxian created\n&#91;root@master231 horizontalpodautoscalers]# \n\n\n\t3.\u67e5\u770b\u521b\u5efa\u7684\u8d44\u6e90\n&#91;root@master231 horizontalpodautoscalers]# kubectl get hpa,svc,deploy,po -o wide\nNAME                                             REFERENCE                   TARGETS   MINPODS   MAXPODS   REPLICAS   AGE\nhorizontalpodautoscaler.autoscaling\/hpa-cpu      Deployment\/deploy-xiuxian   0%\/95%    2         10        2          46s\nhorizontalpodautoscaler.autoscaling\/hpa-memory   Deployment\/deploy-xiuxian   3%\/90%    2         10        2          46s\n\nNAME                  TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE    SELECTOR\nservice\/kubernetes    ClusterIP      10.200.0.1    &lt;none&gt;        443\/TCP        2d4h   &lt;none&gt;\nservice\/svc-xiuxian   LoadBalancer   10.200.71.3   10.0.0.151    80:14241\/TCP   46s    apps=xiuxian\n\nNAME                             READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR\ndeployment.apps\/deploy-xiuxian   2\/2     2            2           46s   c1           registry.cn-hangzhou.aliyuncs.com\/yinzhengjie-k8s\/apps:v1   apps=xiuxian\n\nNAME                                  READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES\npod\/deploy-xiuxian-5c66679644-b46sl   1\/1     Running   0          46s   10.100.2.211   worker233   &lt;none&gt;           &lt;none&gt;\npod\/deploy-xiuxian-5c66679644-fjp7h   1\/1     Running   0          31s   10.100.1.171   worker232   &lt;none&gt;           &lt;none&gt;\n&#91;root@master231 horizontalpodautoscalers]# \n\n\t\n\t4.\u538b\u529b\u6d4b\u8bd5\n&#91;root@harbor250.oldboyedu.com ~]# apt -y install apache2-utils\n\n\u6e29\u99a8\u63d0\u793a:\n\t\u6211\u4eec\u7684k8s\u96c6\u7fa4\u53ef\u80fd\u625b\u4e0d\u4f4f100w\u7684\u8bf7\u6c42\u91cf\uff0c\u6700\u7ec8\u62a5\u9519\u5982\u4e0b\uff0c\u591a\u91cd\u590d\u6267\u884c\u51e0\u6b21\u5373\u53ef\uff0c\u770b\u5230pod\u6269\u5bb9\u6548\u679c\u5373\u53ef\u3002\n\n&#91;root@harbor250.oldboyedu.com ~]# ab -n 1000000 -c 100 http:\/\/10.0.0.151\/\nThis is ApacheBench, Version 2.3 &lt;$Revision: 1879490 $&gt;\nCopyright 1996 Adam Twiss, Zeus Technology Ltd, http:\/\/www.zeustech.net\/\nLicensed to The Apache Software Foundation, http:\/\/www.apache.org\/\n\nBenchmarking 10.0.0.151 (be patient)\nCompleted 100000 requests\napr_socket_recv: Connection reset by peer (104)  # \u5982\u679c\u62a5\u9519\u8be5\u4fe1\u606f\uff0c\u8bf4\u660e\u670d\u52a1\u7aef\u625b\u4e0d\u4f4f\u5566\uff01\nTotal of 138386 requests completed\n&#91;root@harbor250.oldboyedu.com ~]# \n\n\n\t5.\u518d\u6b21\u67e5\u770bpod\u6570\u91cf\n\u5982\u4e0a\u56fe\u6240\u793a\u3002\n\n\n\t6.\u5220\u9664\u8d44\u6e90\n&#91;root@master231 horizontalpodautoscalers]# kubectl delete -f 03-deploy-hpa-xiuxian.yaml \ndeployment.apps \"deploy-xiuxian\" deleted\nhorizontalpodautoscaler.autoscaling \"hpa-memory\" deleted\nhorizontalpodautoscaler.autoscaling \"hpa-cpu\" deleted\nservice \"svc-xiuxian\" deleted\n&#91;root@master231 horizontalpodautoscalers]# <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u90e8\u7f72\u4fee\u4ed9\u4e1a\u52a1\uff0c\u8981\u6c42\u526f\u672c\u6570\u91cf\u4e3a1;2.\u4f7f\u7528ab\u6d4b\u8bd5\u5de5\u5177\u5bf9\u4fee\u4ed9\u4e1a\u52a1\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5;3.\u8bf7\u5b9e\u73b0Pod\u6c34\u5e73\u4f38\u7f29\uff0c\u5f53c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":53,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=117"}],"version-history":[{"count":1,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/posts\/117\/revisions\/119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=\/wp\/v2\/media\/53"}],"wp:attachment":[{"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xueyaa.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}