K8S HPA

k8s hpa当前有3个版本分别支持, v1和v2/beta1版本只能使用CPU使用情况来进行扩容,v2beta2版本可以使用自定义指标来定义
使用v2beta2需要使用的metrics-server + prometheus, 使用另外的版本只需要metrics-server

metrics-server:https://github.com/kubernetes-sigs/metrics-server
prometheus:https://github.com/coreos/prometheus-operator

1
2
3
4
➜  metrics-server git:(master) ✗ k api-versions | grep autoscaling
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2

explame

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: test-hpa
name: test-hpa
spec:
replicas: 1
selector:
matchLabels:
app: test-hpa
template:
metadata:
labels:
app: test-hpa
spec:
containers:
- args:
- -cpus
- "2"
image: vish/stress
name: test
resources:
requests:
cpu: 0.01
memory: 25Mi
limits:
cpu: 0.05
memory: 60Mi
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: test-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-hpa
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜  metrics-server git:(master) ✗ k get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
test-hpa Deployment/test-hpa 485%/10% 1 10 10 22h
➜ metrics-server git:(master) ✗ kgp
NAME READY STATUS RESTARTS AGE
test-hpa-6b9dd99d-22db4 1/1 Running 0 29m
test-hpa-6b9dd99d-2dnt5 1/1 Running 0 30m
test-hpa-6b9dd99d-68vnv 1/1 Running 0 29m
test-hpa-6b9dd99d-8l59h 1/1 Running 0 29m
test-hpa-6b9dd99d-hqbwf 1/1 Running 0 30m
test-hpa-6b9dd99d-jx7s8 1/1 Running 0 30m
test-hpa-6b9dd99d-qsw4n 1/1 Running 0 22h
test-hpa-6b9dd99d-rvxw4 1/1 Running 0 29m
test-hpa-6b9dd99d-sq4vz 1/1 Running 0 29m
test-hpa-6b9dd99d-wppjm 1/1 Running 0 29m