K8s的水平自动扩容和缩容HPA

devtools/2024/11/27 5:59:58/
HPA全称是Horizontal Pod Autoscaler,翻译成中文是POD水平自动伸缩,HPA可以基于CPU利用率对replication controller、deployment和replicaset中的pod数量进行自动扩缩容(除了CPU利用率也可以基于其他应程序提供的度量指标custom metrics进行自动扩缩容)。pod自动缩放不适用于无法缩放的对象,比如DaemonSets。
HPA由Kubernetes API资源和控制器实现。资源决定了控制器的行为。控制器会周期性的获取目标资源指标(如,平均CPU利用率),并与目标值相比较后来调整Pod副本数量。

1.安装metrics进行监控集群,获取指标

root@k8s-master:/home/vagrant# cat metrics.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:labels:k8s-app: metrics-servername: metrics-servernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:labels:k8s-app: metrics-serverrbac.authorization.k8s.io/aggregate-to-admin: "true"rbac.authorization.k8s.io/aggregate-to-edit: "true"rbac.authorization.k8s.io/aggregate-to-view: "true"name: system:aggregated-metrics-reader
rules:
- apiGroups:- metrics.k8s.ioresources:- pods- nodesverbs:- get- list- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:labels:k8s-app: metrics-servername: system:metrics-server
rules:
- apiGroups:- ""resources:- nodes/metricsverbs:- get
- apiGroups:- ""resources:- pods- nodesverbs:- get- list- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:labels:k8s-app: metrics-servername: metrics-server-auth-readernamespace: kube-system
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccountname: metrics-servernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:labels:k8s-app: metrics-servername: metrics-server:system:auth-delegator
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:auth-delegator
subjects:
- kind: ServiceAccountname: metrics-servernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:labels:k8s-app: metrics-servername: system:metrics-server
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:metrics-server
subjects:
- kind: ServiceAccountname: metrics-servernamespace: kube-system
---
apiVersion: v1
kind: Service
metadata:labels:k8s-app: metrics-servername: metrics-servernamespace: kube-system
spec:ports:- name: httpsport: 443protocol: TCPtargetPort: httpsselector:k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:k8s-app: metrics-servername: metrics-servernamespace: kube-system
spec:selector:matchLabels:k8s-app: metrics-serverstrategy:rollingUpdate:maxUnavailable: 0template:metadata:labels:k8s-app: metrics-serverspec:containers:- args:- --cert-dir=/tmp- --secure-port=10250- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname- --kubelet-use-node-status-port- --metric-resolution=15s- --kubelet-insecure-tlsimage: registry.k8s.io/metrics-server/metrics-server:v0.7.2imagePullPolicy: IfNotPresentlivenessProbe:failureThreshold: 3httpGet:path: /livezport: httpsscheme: HTTPSperiodSeconds: 10name: metrics-serverports:- containerPort: 10250name: httpsprotocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /readyzport: httpsscheme: HTTPSinitialDelaySeconds: 20periodSeconds: 10resources:requests:cpu: 100mmemory: 200MisecurityContext:allowPrivilegeEscalation: falsecapabilities:drop:- ALLreadOnlyRootFilesystem: truerunAsNonRoot: truerunAsUser: 1000seccompProfile:type: RuntimeDefaultvolumeMounts:- mountPath: /tmpname: tmp-dirnodeSelector:kubernetes.io/os: linuxpriorityClassName: system-cluster-criticalserviceAccountName: metrics-servervolumes:- emptyDir: {}name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:labels:k8s-app: metrics-servername: v1beta1.metrics.k8s.io
spec:group: metrics.k8s.iogroupPriorityMinimum: 100insecureSkipTLSVerify: trueservice:name: metrics-servernamespace: kube-systemversion: v1beta1versionPriority: 100
root@k8s-master:/home/vagrant# kubectl apply -f metrics.yaml 
root@k8s-master:/home/vagrant# kubectl get pods -n kube-system|grep metrics
metrics-server-587b667b55-tjhfp            1/1     Running   0             11m

2.创建测试Deployment

root@k8s-master:/home/vagrant# cat php-apache.yaml 
kind: Deployment
apiVersion: apps/v1
metadata:name: php-apachelabels:app: apache
spec:replicas: 1selector: matchLabels:app: apachetemplate:metadata:name: apachelabels:app: apachespec:containers:- name: apacheimage: httpdimagePullPolicy: IfNotPresentports:- containerPort: 80name: httpdresources:limits:cpu: 800mrequests:cpu: 500m
---
kind: Service
apiVersion: v1
metadata:name: apache-servicelabels:app: apache
spec:selector:app: apacheports:- name: httptargetPort: 80port: 80root@k8s-master:/home/vagrant# kubectl apply -f php-apache.yaml 
deployment.apps/php-apache created
root@k8s-master:/home/vagrant# kubectl get svc,deployment,pods
NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/apache-service   ClusterIP   10.105.192.165   <none>        80/TCP     2m
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP    43d
service/redis-svc        ClusterIP   10.109.12.172    <none>        6379/TCP   23hNAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/php-apache   1/1     1            1           24mNAME                             READY   STATUS    RESTARTS   AGE
pod/php-apache-b7cb67d98-b6qx9   1/1     Running   0          24m

3.创建HPA

root@k8s-master:/home/vagrant# vim hpa.yaml
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2
metadata:name: php-apache
spec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: php-apacheminReplicas: 1maxReplicas: 10metrics:- type: Resourceresource:name: cputarget:            type: UtilizationaverageUtilization: 50
root@k8s-master:/home/vagrant# kubectl apply -f hpa.yaml 
horizontalpodautoscaler.autoscaling/php-apache created

4.模拟php-apache Pod CPU使用率增加

建议多开几个终端进行,效果更加明显
root@k8s-master:/home/vagrant# kubectl run -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while true; do wget -q -O- http://10.105.192.165; done"
root@k8s-master:/home/vagrant# kubectl get pods|grep load
load-generator               1/1     Running   0          54s
root@k8s-master:/home/vagrant# kubectl get deployment,po,hpa
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/php-apache   2/2     2            2           33mNAME                              READY   STATUS        RESTARTS   AGE
pod/load-generator                1/1     Running       0          105s
pod/load-generator2               1/1     Terminating   0          103s
pod/load-generator3               1/1     Running       0          24s
pod/php-apache-67fd659dcb-6z8tz   1/1     Running       0          23s
pod/php-apache-67fd659dcb-thb68   1/1     Running       0          114sNAME                                             REFERENCE               TARGETS        MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/php-apache   Deployment/php-apache   cpu: 80%/50%   1         10        2          18m
root@k8s-master:/home/vagrant# kubectl get deployment,po,hpa
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/php-apache   10/10   10           10          36mNAME                              READY   STATUS    RESTARTS   AGE
pod/load-generator                1/1     Running   0          4m32s
pod/load-generator3               1/1     Running   0          3m11s
pod/load-generator4               1/1     Running   0          106s
pod/php-apache-67fd659dcb-2hdg5   1/1     Running   0          2m25s
pod/php-apache-67fd659dcb-52rcx   1/1     Running   0          2m40s
pod/php-apache-67fd659dcb-6z8tz   1/1     Running   0          3m10s
pod/php-apache-67fd659dcb-8wc7j   1/1     Running   0          2m25s
pod/php-apache-67fd659dcb-8zdf6   1/1     Running   0          2m40s
pod/php-apache-67fd659dcb-ckqkm   1/1     Running   0          2m9s
pod/php-apache-67fd659dcb-jncsb   1/1     Running   0          2m24s
pod/php-apache-67fd659dcb-pb89r   1/1     Running   0          2m25s
pod/php-apache-67fd659dcb-t66kv   1/1     Running   0          2m9s
pod/php-apache-67fd659dcb-thb68   1/1     Running   0          4m41sNAME                                             REFERENCE               TARGETS        MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/php-apache   Deployment/php-apache   cpu: 40%/50%   1         10        10         21m

发现自动增加了Pod,实现自动扩容


http://www.ppmy.cn/devtools/137317.html

相关文章

[SWPUCTF 2021 新生赛]error

[SWPUCTF 2021 新生赛]error 报错注入&#xff1a;?idand updatexml(1,concat(0x7e,database(),0x7e),1) -- 爆出了数据库名称 test_db 爆表名&#xff1a;?idand updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_sc…

实现Excel文件和其他文件导出为压缩包,并导入

导出 后端&#xff1a; PostMapping("/exportExcelData")public void exportExcelData(HttpServletRequest request, HttpServletResponse response, RequestBody ResData resData) throws IOException {List<Long> menuIds resData.getMenuIds();List<Co…

docker启动kafka、zookeeper、kafdrop

1、启动zookeeper docker run -d --name zookeeper -p 2181:2181 -t wurstmeister/zookeeper2、启动kafka docker run -d --name kafka --publish 9092:9092 --link zookeeper:zookeeper -e KAFKA_BROKER_ID1 -e HOST_IP127.0.0.1 -e KAFKA_ZOOKEEPER_CONNECTzookeeper:2181…

elementui el-input修改字体样式

elementui el-input修改样式 需求&#xff0c;要求配送备注红色字体显示 在这里插入图片描述 解决方案 1.使用id定位到一个el-input 元素 2.为此id指定样式 代码 <!--代码--> <el-form-item prop"dispatchRemark" label"派单备注" :label-wid…

3mf 格式详解,javascript加载导出3mf文件示例

3MF 格式详解 3MF&#xff08;3D Manufacturing Format&#xff09;是一种开放标准的文件格式&#xff0c;专门用于三维制造和打印。3MF 格式旨在解决 STL 格式的局限性&#xff0c;提供更丰富和灵活的数据表示。3MF 文件是一种 ZIP 文件&#xff0c;其中包含了描述三维模型的…

从零开始:Linux 环境下的 C/C++ 编译教程

个人主页&#xff1a;chian-ocean 文章专栏 前言&#xff1a; GCC&#xff08;GNU Compiler Collection&#xff09;是一个功能强大的编译器集合&#xff0c;支持多种语言&#xff0c;包括 C 和 C。其中 gcc 用于 C 语言编译&#xff0c;g 专用于 C 编译。 Linux GCC or G的安…

【深度学习】【RKNN】【C++】模型转化、环境搭建以及模型部署的详细教程

【深度学习】【RKNN】【C】模型转化、环境搭建以及模型部署的详细教程 提示:博主取舍了很多大佬的博文并亲测有效,分享笔记邀大家共同学习讨论 文章目录 【深度学习】【RKNN】【C】模型转化、环境搭建以及模型部署的详细教程前言模型转换--pytorch转rknnpytorch转onnxonnx转rkn…

移动端相关 BFC CSS原子化 ✅

移动端相关 设备宽度&视口 设备宽度是指设备屏幕的实际物理宽度&#xff0c;通常以像素&#xff08;px&#xff09;表示。它是固定的&#xff0c;取决于设备的硬件。不同设备&#xff08;如手机、平板、桌面等&#xff09;有不同的设备宽度。 常被提及的视口可被分为3种…