当前位置: 首页 > news >正文

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

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.mrgr.cn/news/78167.html

相关文章:

  • Unity UGUI原理剖析
  • [Redis#6] list | 命令 | 应用 | 消息队列 | 微博 Timeline
  • OSPTrack:一个包含多个生态系统中软件包执行时生成的静态和动态特征的标记数据集,用于识别开源软件中的恶意行为。
  • 优化注意力层提升 Transformer 模型效率:通过改进注意力机制降低机器学习成本
  • HarmonyOS应用开发者基础认证,Next版本发布后最新题库(10月8日更新题库未收录)
  • 构建 Java Web 应用程序:从 Servlet 到数据库交互(Eclipse使用JDBC连接Mysql数据库)
  • C++ 优先算法 —— 无重复字符的最长子串(滑动窗口)
  • QT QRadioButton控件 全面详解
  • 数据结构 (12)串的存储实现
  • 大语言模型(LLM)不平衡的内存使用问题;训练过程中 Transformer层1和Transformer层2的反向传播计算量差异
  • JVM详解:垃圾回收机制
  • Android OTA 更新面试题及参考答案
  • 深入解析 ArrayList 源码:从动态扩容到高效存取的秘密
  • 开展网络安全成熟度评估:业务分析师的工具和技术
  • 【kafka02】消息队列与微服务之Kafka部署
  • 深入探索Elasticsearch:多场景冷热架构实战指南
  • 运维面试整理总结
  • HTTP中GET和POST的区别是什么?
  • nodepad配置c/c++ cmd快速打开创建项目文件
  • 2024.11.26总结
  • JVM系列之OOM观测准备
  • 蓝桥杯练习题
  • c++学习:json库例子
  • 【C语言】关于 JavaScript 与 C语言在函数嵌套定义方面的差异探讨
  • 【2024】前端学习笔记19-ref和reactive使用
  • 技术文档,they are my collection!