[DAY11]Horizontal Pod Autoscaling(HPA)

Horizontal Pod Autoscaler

Pod 水平自动扩缩(Horizontal Pod Autoscaler)(以下都简称HPA) 可以基於CPU利用率(by metrics)自动扩缩Pod数量。 除了CPU利用率,也可以透过自定义的metrics进行扩缩。

架构图


资料来源

还记得前面的Deployment replicas的设定值吗,这个设定值决定了要产生多少个pod,但是如果今天request很少或是很高时,这样子的pod数量是否有符合需求呢?
除了人工scaling pods外,是否有自动性的机会咧~~科技始终来自於惰性,有HPA後就可以省去这些动作啦,一切交给k8s进行自动扩缩Pod。

HPA如何运作

k8s中的controller manager会定时去查询HPA中定义的metrics设定资料(ex cpu使用率),metrics资料来源是controller manager使用resource metric API或是custom metrics API取得,前一篇有提到,如果Deployment没有设定resources的话是无法使用HPA喔。
HPA需要判断目前metrics跟设定值的百分比。

实作HPA-kubectl autoscale

使用上篇文章中的test-myapp deployment,执行kubectl autoscale 可以简单的把原本的test-myapp套上HPA设定

kubectl autoscale deployment test-myapp --cpu-percent=60 --min=1 --max=3

实作HPA-HPA YAML

apiVersion: autoscaling/v2beta1 
kind: HorizontalPodAutoscaler
metadata:
  name: test-myapp 
spec:
  minReplicas: 1
  maxReplicas: 3
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: test-myapp
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization:  60

取得所有HPA

kubectl get hpa
NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
test-myapp   Deployment/test-myapp   10%/60%    1         3         1          1d
test-myapp2  Deployment/test-myapp2   5%/80%    1         2         1          1d
test-myapp3  Deployment/test-myapp3   4%/80%    1         1         1          1d

取得HPA详细资讯

kubectl describe hpa test-myapp
Name:                                                  test-myapp
Namespace:                                             default
CreationTimestamp:                                     
Reference:                                             Deployment/test-myapp
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  9% (36m) / 60%
Min replicas:                                          1
Max replicas:                                          3
Deployment pods:                                       1 current / 1 desired
Conditions:
  Type            Status  Reason              Message
  ----            ------  ------              -------
  AbleToScale     True    ReadyForNewScale    recommended size matches current size
  ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
Events:           <none>

使用原则

HPA建议使用在无状态的服务上面,因为k8s会随时根据资源使用率进行扩缩pod,如果pod是有状态的情形时,这样子自动缩pod会导致原本的资料或是状态遗失掉,後续需要更多的精力进行hpa後资料遗失的补救。


<<:  不要带着做法去要答案

>>:  Html表单元素(DAY6)

企业资料通讯 Week2 (讲到Circuit Switching v.s Packet Switching)

**参考资料与文章在文章最下方!! **这篇有点长,大家看需要的地方就行 比比看: 因为节点上距离的...

[Day 30] 会员登入及登出(二)

今天继续使用者登入的部分. 登入资料验证 当接收到表单送来的资讯之後, 首先我们要针对资料作验证. ...

NEC SV9100 小总机WEBPRO设定问题

请问大神: NEC SV9100 小总机进线会到语音宣告,但按1会跑到特定分机(例711),目前分机...

Flutter基础介绍与实作-Day3 Flutter和React Native的比较

Flutter的特色 1.一切皆为元件(widgwt) 在Flutter中一切皆为元件,我们在开始写...

计算API所需要的参数: 内文杂凑

嗯 API 所需要的参数内文杂凑 … 依照规格说明书的指示,需要以下的流程 (1) 先移除所有空值的...