{"id":332,"date":"2026-01-11T21:06:40","date_gmt":"2026-01-11T13:06:40","guid":{"rendered":"https:\/\/www.xxiaoteng.cn\/?p=332"},"modified":"2026-05-11T21:19:08","modified_gmt":"2026-05-11T13:19:08","slug":"k8s-hpa%ef%bc%9ahorizontal-pod-autoscaling","status":"publish","type":"post","link":"https:\/\/www.xxiaoteng.cn\/?p=332","title":{"rendered":"K8s HPA\uff1aHorizontal Pod Autoscaling"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-0\">1.HPA\u7b80\u4ecb<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">ai '\u8bb2\u4e00\u4e0bhpa'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"prettyprint\" >Horizontal Pod Autoscaling<\/code>\uff0c\u7b80\u79f0HPA \u81ea\u52a8\u6c34\u5e73\u4f38\u7f29\uff0c\u8fd8\u6709\u4e00\u4e2avpa\u7eb5\u5411\u6269\u5bb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u52a8\u6c34\u5e73\u4f38\u7f29\uff0c\u662f\u6307\u8fd0\u884c\u5728k8s\u4e0a\u7684\u5e94\u7528\u8d1f\u8f7d(POD)\uff0c\u53ef\u4ee5\u6839\u636e\u8d44\u6e90\u4f7f\u7528\u7387\u8fdb\u884c\u81ea\u52a8\u6269\u5bb9\u3001\u7f29\u5bb9\uff0c\u5b83\u4f9d\u8d56metrics-server\u670d\u52a1pod\u4f7f\u7528\u8d44\u6e90\u6307\u6807\u6536\u96c6\uff1b\u6211\u4eec\u77e5\u9053\u5e94\u7528\u7684\u8d44\u6e90\u4f7f\u7528\u7387\u901a\u5e38\u90fd\u6709\u9ad8\u5cf0\u548c\u4f4e\u8c37\uff0c\u6240\u4ee5k8s\u7684HPA\u7279\u6027\u5e94\u8fd0\u800c\u751f\uff1b\u5b83\u4e5f\u662f\u6700\u80fd\u4f53\u73b0\u533a\u522b\u4e8e\u4f20\u7edf\u8fd0\u7ef4\u7684\u4f18\u52bf\u4e4b\u4e00\uff0c\u4e0d\u4ec5\u80fd\u591f\u5f39\u6027\u4f38\u7f29\uff0c\u800c\u4e14\u5b8c\u5168\u81ea\u52a8\u5316\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-1\">2.\u5f39\u6027\u4f38\u7f29\u81ea\u52a8\u5316\u5b9e\u8df5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528HPA\u524d\u6211\u4eec\u8981\u786e\u4fddK8s\u96c6\u7fa4\u7684dns\u670d\u52a1\u548cmetrics\u670d\u52a1\u662f\u6b63\u5e38\u8fd0\u884c\u7684\uff0c\u5e76\u4e14\u6211\u4eec\u6240\u521b\u5efa\u7684\u670d\u52a1\u9700\u8981\u914d\u7f6e\u6307\u6807\u5206\u914d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u5c0f\u8d44\u6e90\u5206\u914d\u7684request\u53c2\u6570yaml\u5b9e\u4f8b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cat &gt; web.yaml &lt;&lt;-'EOF'<br># cat web.yaml <br>apiVersion: apps\/v1<br>kind: Deployment<br>metadata:<br>  labels:<br> &nbsp;  app: web<br>  name: web<br>  namespace: default<br>spec:<br>  replicas: 1<br>  selector:<br> &nbsp;  matchLabels:<br> &nbsp; &nbsp;  app: web<br>  template:<br> &nbsp;  metadata:<br> &nbsp; &nbsp;  labels:<br> &nbsp; &nbsp; &nbsp;  app: web<br> &nbsp;  spec:<br> &nbsp; &nbsp;  containers:<br> &nbsp; &nbsp;  - image: nginx:1.21.6<br> &nbsp; &nbsp; &nbsp;  name: nginx<br> &nbsp; &nbsp; &nbsp;  resources:<br> &nbsp; &nbsp; &nbsp; &nbsp;  limits: &nbsp; # \u56e0\u4e3a\u6211\u8fd9\u91cc\u662f\u6d4b\u8bd5\u73af\u5883\uff0c\u6240\u4ee5\u8fd9\u91ccCPU\u53ea\u5206\u914d50\u6beb\u6838\uff080.05\u6838CPU\uff09\u548c20M\u7684\u5185\u5b58<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  cpu: \"50m\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  memory: 20Mi<br> &nbsp; &nbsp; &nbsp; &nbsp;  requests: # \u4fdd\u8bc1\u8fd9\u4e2apod\u521d\u59cb\u5c31\u80fd\u5206\u914d\u8fd9\u4e48\u591a\u8d44\u6e90<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  cpu: \"50m\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  memory: 20Mi<br>EOF<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u5e76\u521b\u5efa\u670d\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl apply -f web.yaml <br>kubectl expose deployment web --port 80 --target-port 80<br>\u200b<br>#\u67e5\u770b\u6807\u7b7e\u5339\u914d\u7ed3\u679c<br>kubectl get svc -o wide<br>kubectl get pod -l app=web<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-2\">3.\u521b\u5efahpa\u89c4\u5219<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ai \u5feb\u901f\u638c\u63e1hpa<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ai '\u521b\u5efa\u4e00\u4e2ahpa\u89c4\u5219'<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl autoscale deployment web --max=5 --min=2 --cpu-percent=30<br>#\u7b49\u5f85\u4e00\u4f1a\uff0c\u53ef\u4ee5\u770b\u5230\u76f8\u5173\u7684hpa\u4fe1\u606f\uff08K8s\u4e0ametrics\u670d\u52a1\u6536\u96c6\u6240\u6709pod\u8d44\u6e90\u7684\u65f6\u95f4\u95f4\u9694\u5927\u6982\u572860s\u7684\u65f6\u95f4\uff09<br>kubectl get hpa -w<br>#\u5220\u9664\u547d\u4ee4\u5982\u4e0b\uff1a<br>kubectl delete horizontalpodautoscalers.autoscaling web<br>\u200b<br>#\u5220\u9664pod\u719f\u7ec3\u6d4b\u8bd5 \u6700\u5c0fpod\u6570\u4e24<br>kubectl scale --replicas=1 deployment web<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6a21\u62df\u4e1a\u52a1\u6d41\u91cf\u589e\u957f\uff0c\u770b\u770bhpa\u81ea\u52a8\u4f38\u7f29\u7684\u6548\u679c\u3002\u5f00\u542f\u53e6\u5916\u4e00\u4e2a\u7ec8\u7aef\u8fd0\u8425\u5de5\u5177\u5bb9\u5668\u6d4b\u8bd5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ai 'kubectl\u8fd0\u884c\u4e00\u4e2a\u5de5\u5177pod\u6d4b\u8bd5\u4e13\u7528'<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl run -it --rm busybox --image=registry.cn-shanghai.aliyuncs.com\/acs\/busybox:v1.29.2 -- sh<br>while :;do wget -q -O- http:\/\/web;done<br>\u200b<br>#\u7b49\u5f852 ~ 3\u5206\u949f\uff0c\u6ce8\u610fk8s\u4e3a\u4e86\u907f\u514d\u9891\u7e41\u589e\u5220pod\uff0c\u5bf9\u526f\u672c\u7684\u589e\u52a0\u901f\u5ea6\u6709\u9650\u5236<br>kubectl get hpa web -w<br>#\u770b\u4e0bhpa\u7684\u63cf\u8ff0\u4fe1\u606f\u4e0b\u9762\u7684\u4e8b\u4ef6\u8bb0\u5f55<br>kubectl describe hpa web<br>\u200b<br>#\u505c\u6389\u538b\u6d4b\uff0c\u89c2\u5bdf\u4e0bHPA\u7684\u81ea\u52a8\u6536\u7f29\u529f\u80fd5\u5206\u949f\u5de6\u4f1a\u6709\u6536\u7f29<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-3\">4.kubernetes yaml\u5bfc\u5165\u5bfc\u51fa<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kubernetes\u96c6\u7fa4\u8d44\u6e90\u901a\u8fc7yaml\u5bfc\u5165\u5bfc\u51fa\u7684\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1.\u5bfc\u51fa\u67d0\u4e2aDeployment\u7684YAML\u6587\u4ef6<br>&lt;deployment-name&gt;\u662f\u60a8\u60f3\u8981\u5bfc\u51fa\u7684Deployment\u7684\u540d\u79f0,\u6b64\u547d\u4ee4\u5c06\u5bfc\u51faDeployment\u7684\u5b8c\u6574YAML\u6587\u4ef6\uff0c\u5305\u62ec\u6240\u6709\u6ce8\u91ca\u548c\u6ce8\u91ca\u884c<br>kubectl get deployment &lt;deployment-name&gt; -n &lt;namespace&gt; -o yaml &gt; &lt;deployment-name&gt;.yaml<br>\u200b<br>2.\u53ea\u60f3\u5bfc\u51faDeployment\u7684\u89c4\u8303\u90e8\u5206<br>kubectl get deployment &lt;deployment-name&gt; -n &lt;namespace&gt; -o yaml | grep -v '^\\s*#' &gt; &lt;deployment-name&gt;.yaml<br>\u200b<br>3.\u96c6\u7fa4configMap\uff0cdeployment\uff0cservice\uff0csecret\u7b49\u5bfc\u51fa\u548c\u5bfc\u5165\u7684\u65b9\u6cd5\uff0c\u4ee5service\u4e3a\u4f8b\uff0c\u5176\u4ed6\u540c\u7406\uff1a<br>\u5355\u4e2aservice\uff1a<br> &nbsp; &nbsp; \u5bfc\u51fa: kubectl get service serviceName -o yaml &gt; backup.yaml<br> &nbsp; &nbsp; \u5bfc\u5165: kubectl create -f backup.yaml<br>\u6240\u6709service\uff1a<br> &nbsp; &nbsp; \u5bfc\u51fa: kubectl get service -o yaml &gt; backup.yaml<br> &nbsp; &nbsp; \u5bfc\u5165: kubectl create -f backup.yaml<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/kubernetes\/autoscaler\">https:\/\/github.com\/kubernetes\/autoscaler<\/a> vpa<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.HPA\u7b80\u4ecb ai &#8216;\u8bb2\u4e00\u4e0bhpa&#8217; Horizontal Pod Autoscaling\uff0c\u7b80\u79f0HPA \u81ea\u52a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-332","post","type-post","status-publish","format-standard","hentry","category-k8s"],"_links":{"self":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=332"}],"version-history":[{"count":1,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/332\/revisions"}],"predecessor-version":[{"id":333,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/332\/revisions\/333"}],"wp:attachment":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}