Kubernetes之Pod的自动水平伸缩(HPA)
LiuSw Lv6

Kubernetes之Pod的自动水平伸缩(HPA)

HPA(Horizontal Pod Autoscaler )

pod的自动水平伸缩

有了HPA,我们就不用为上面的问题而烦恼,HPA会帮我们自动完成pod的扩缩容。

当资源需求过高时,会自动创建出pod副本;当资源需求低时,会自动收缩pod副本数。

注意:首先必须确保集群中已经安装metrics-server的组件,否则无法获取集群内资源数据,无法进行以下操作。

原理

通过集群内的资源监控系统(metrics-server),来获取集群中资源的使用状态。

根据CPU、内存、以及用户自定义的资源指标数据的使用量或连接数为参考依据,来制定一个临界点,一旦超出这个点,HPA就会自动创建出pod副本。

版本

通过kubectl api-versions可以看到,目前有3个版本:

autoscaling/v1 #(默认)只支持通过cpu为参考依据,来改变pod副本数
autoscaling/v2beta1 #支持通过cpu、内存、连接数以及用户自定义的资源指标数据为参考依据。
autoscaling/v2beta2 #同上,小的变动

1
2
3
4
5
kubectl api-versions | grep autoscal

autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2

查看使用的版本:

1
2
3
4
kubectl explain hpa

KIND: HorizontalPodAutoscaler
VERSION: autoscaling/v1

示例

使用 Deployment “foo”设定,使用默认的自动伸缩策略,指定目标CPU使用率,使其Pod数量在2到10之间。

1
kubectl autoscale deployment foo --min=2 --max=10

使用RC“foo”设定,使其Pod的数量介于1和5之间,CPU使用率维持在80%。

1
kubectl autoscale rc foo --max=5 --cpu-percent=80

示例2

例如:我有个deployment叫myapp现在只有一个副本数,最多只能8个副本数,当pod的cpu平均利用率超过百分之50或内存平均值超过百分之50时,pod将自动增加副本数以提供服务。
SVC、Deployment资源清单:

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
apiVersion: v1
kind: Service
metadata:
name: svc-hpa
namespace: default
spec:
selector:
app: myapp
type: NodePort ##注意这里是NodePort,下面压力测试要用到。
ports:
- name: http
port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
name: myapp-demo
namespace: default
labels:
app: myapp
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
resources:
requests:
cpu: 50m
memory: 50Mi
limits:
cpu: 50m
memory: 50Mi

HPA资源清单如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa-v2
namespace: default
spec:
minReplicas: 1 ##至少1个副本
maxReplicas: 8 ##最多8个副本
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50 ##注意此时是根据使用率,也可以根据使用量:targetAverageValue
- type: Resource
resource:
name: memory
targetAverageUtilization: 50 ##注意此时是根据使用率,也可以根据使用量:targetAverageValue

使用ab工具模拟压力测试:

1
ab -c 1000 -n 5000000 http://192.168.1.103:31727/index.html

等待数分钟后,查看hpa及pod数量:

1
2
3
4
5
6
7
8
9
kubectl get hpa

NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myapp-hpa-v2 Deployment/myapp 5%/50%, 72%/50% 1 8 2 44m

kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-558db64459-pwzsd 1/1 Running 0 16m
myapp-558db64459-x9c4k 1/1 Running 0 23s

此示例来自 Smbands

官方参数

Name Shorthand Default Usage
allow-missing-template-keys true If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
cpu-percent -1 The target average CPU utilization (represented as a percent of requested CPU) over all the pods. If it’s not specified or negative, a default autoscaling policy will be used.
dry-run false If true, only print the object that would be sent, without sending it.
filename f [] Filename, directory, or URL to files identifying the resource to autoscale.
generator horizontalpodautoscaler/v1 The name of the API generator to use. Currently there is only 1 generator.
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
max -1 The upper limit for the number of pods that can be set by the autoscaler. Required.
min -1 The lower limit for the number of pods that can be set by the autoscaler. If it’s not specified or negative, the server will apply a default value.
name The name for the newly created object. If not specified, the name of the input resource will be used.
no-headers false When using the default or custom-column output format, don’t print headers (default print headers).
output o Output format. One of: json|yaml|wide|name|custom-columns=…|custom-columns-file=…|go-template=…|go-template-file=…|jsonpath=…|jsonpath-file=… See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
output-version DEPRECATED: To use a specific API version, fully-qualify the resource, version, and group (for example: ‘jobs.v1.batch/myjob’).
record false Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursive R false Process the directory used in -f, –filename recursively. Useful when you want to manage related manifests organized within the same directory.
save-config false If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
show-all a false When printing, show all resources (default hide terminated pods.)
show-labels false When printing, show all labels as the last column (default hide labels column)
sort-by If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. ‘{.metadata.name}’). The field in the API resource specified by this JSONPath expression must be an integer or a string.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
 评论