k8s对象-HPA

前言
HPA(Horizontal Pod Autoscaler)Pod自动弹性伸缩,K8S通过对Pod中运行的容器各项指标(CPU占用、内存占用、网络请求量)的检测,实现对Pod实例个数的动态新增和减少。

1. autoscaling/v1

说明:该API版本仅对CPU做了限制

创建一个HPA,yml文件如下:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: iot-server
spec:
minReplicas: 1 #最小pod数量
maxReplicas: 10 #最大pod数量
scaleTargetRef:
apiVersion: v1
kind: Deployment #需要扩容的对象类型
name: iot-server #需要扩容的对象名称
targetCPUUtilizationPercentage: 90 #CPU平均使用率超过90%扩容

说明:需要对容器进行request进行限制。

2. autoscaling/v2beta1

说明:
autoscaling/v2beta1可以限制 CPU、MEM、自定义metric 做出限制。
但是依赖于metrics-server

2.1 metrics-server安装

命令不要复制,因为pod的名字是我自己的,tab补全就可以了。

[root@DoM01 crust-framework]# kubectl get pod -n kube-system metrics-server-5996bcd4f-b7xrh
NAME READY STATUS RESTARTS AGE
metrics-server-5996bcd4f-b7xrh 1/1 Running 1 49d
  • 测试metrics-server

安装好之后可以使用top命令了,在安装文档里可以理解到,这里不赘述了。我们直接看我自己的一个namespace下的pod占用资源。

[root@DoM01 crust-framework]# kubectl top pod -n crust-framework
NAME CPU(cores) MEMORY(bytes)
auth-6b68bccbcd-tp99p 1010m 5867Mi
gateway-679776557b-82gtv 2916m 1282Mi
user-6b4c7466c-tv572 97m 4179Mi
web-65d57c4559-bdq2x 0m

2.2 限制CPU和MEM

  • yml文件

说明:
这是我写在整个项目的chart里边的一部分,大家使用的时候只要把里边的变量替换成你的实际值即可。

---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: auth
namespace: "{{ .Values.global.namespace }}"
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: auth
minReplicas: {{ .Values.auth.minReplicas }}
maxReplicas: {{ .Values.auth.maxReplicas }}
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: {{ .Values.auth.targetAverageUtilization }}
- type: Resource
resource:
name: memory
targetAverageValue: {{ .Values.auth.targetAverageValue }}
---

注解:
minReplicas:副本最小值(示例 1)
maxReplicas: 副本最大值(示例 3)
targetAverageUtilization:CPU扩容阈值(示例 80)
targetAverageValue: MEM扩容限制(示例 12Gi)

  • 查看
[root@DoM01 crust-framework]# kubectl get pod -n crust-framework
NAME READY STATUS RESTARTS AGE
auth-66f9487df-g4v85 1/1 Running 0 102s
auth-66f9487df-hl4md 1/1 Running 0 42s
gateway-7db45f869c-bchvj 1/1 Running 0 118m
user-6b4c7466c-tv572 1/1 Running 1 9d
web-65d57c4559-bdq2x 1/1 Running 1 9d

2.3 自定义metric

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: podinfo
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: podinfo
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 80
- type: Resource
resource:
name: memory
targetAverageValue: 200Mi
- type: Pods
pods:
metric:
name: packets-per-second
target:
type: AverageValue
averageValue: 1k
- type: Object
object:
metric:
name: requests-per-second
describedObject:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
name: main-route
target:
type: Value
value: 10k

posted on   运维开发玄德公  阅读(42)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示