Posted on

HorizontalPodAutoscalers by customize metric

設定擴充的行為,sacle down及up的時候所做的行為,這邊的設定是假如維持300秒都穩定相同狀況,則做HPA縮放,最少維持這種狀態60秒,一次增加1個Pod​

這邊則設定要參考的數值,若是要使用自訂義資料,則describedObject這邊要設定的與我們在Rules裡面設定的一致,target部分則設定每一個pods的目標值為多少​

下面scaleTargetRef的部分則是設定要做HPA的目標是甚麼,有可能是Services,這邊做HPA的目標則為Pod​

apiVersion: autoscaling/v2beta2​
kind: HorizontalPodAutoscaler​
metadata:​
  name: srs-edge​
  namespace: srs3​
spec:​
  behavior:​
    scaleDown:​
      policies:​
      - periodSeconds: 60​
        type: Pods​
        value: 1​
      selectPolicy: Max​
      stabilizationWindowSeconds: 300​
    scaleUp:​
      policies:​
      - periodSeconds: 60​
        type: Pods​
        value: 1​
      selectPolicy: Max​
      stabilizationWindowSeconds: 300​
  maxReplicas: 2​
  metrics:​
  - object:​
      describedObject:​
        apiVersion: v1​
        kind: Service​
        name: eventqueue​
      metric:​
        name: stream_total_clients_by_pod​
      target:​
        type: Value​
        value: 1k​
    type: Object​
  minReplicas: 1​
  scaleTargetRef:​
    apiVersion: apps/v1​
    kind: Deployment​
    name: srs-edge​

stream_total_clients_by_pod是我們的客製化變數,這個變數是來自於普羅米修斯,要把普羅米修斯的變數給k8s使用,請參見
http://claire-chang.com/2022/12/16/prometheus-rule-for-alert/
這邊很重要的是可以看到label裡面有name: eventqueue,在Prometheus Rule裡一定也要加上labels: service: eventqueue
這樣這個值才會可以被輸出給普羅米修斯使用

如下圖:

其他更多的設定值請見: https://docs.openshift.com/container-platform/4.9/nodes/pods/nodes-pods-autoscaling.html​