pods一段时间不使用,如何自动缩小为0?

how to automatically scale down to 0 when a pods is not used for some time?

我有 N 个状态集,每个状态集都通过 nginx ingress 部署到它们唯一的主机。

例如:

abcde.example.com - Statefulset 1

pqrstu.example.com - Statefulset 2

所以在这里我想在一段时间内(例如:3 天)没有人访问它时将我的 statefulset 副本缩减为 0。 这在 kubernetes 中可行吗?

HPA 可以应用于 Statefulsets,但有一个警告。您将能够通过这种方式自动缩放:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: YOUR_HPA_NAME
spec:
  maxReplicas: 3
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: StatefulSet
    name: YOUR_STATEFUL_SET_NAME
  targetCPUUtilizationPercentage: 80

但是设置minReplicas: 0会产生如下错误

The HorizontalPodAutoscaler "xxxxxx" is invalid: spec.minReplicas: Invalid value: 0: must be greater than 0

你可以试试科达。它处于早期阶段,但它是一项前途光明的技术(恕我直言)。

KEDA allows for fine grained autoscaling (including to/from zero) for event driven Kubernetes workloads. KEDA serves as a Kubernetes Metrics Server and allows users to define autoscaling rules using a dedicated Kubernetes custom resource definition.

https://github.com/kedacore/keda