CronHPA

Cron Horizontal Pod Autoscaler(CronHPA)使我们能够使用crontab模式定期自动扩容工作负载(那些支持扩展子资源的负载,例如deployment、statefulset)。

CronHPA使用Cron格式进行编写,周期性地在给定的调度时间对工作负载进行扩缩容。

CronHPA 资源结构

CronHPA定义了一个新的CRD,cron-hpa-controller是该CRD对应的controller/operator,它解析CRD中的配置,根据系统时间信息对相应的工作负载进行扩缩容操作。

  1. // CronHPA represents a set of crontabs to set target's replicas.
  2. type CronHPA struct {
  3. metav1.TypeMeta `json:",inline"`
  4. metav1.ObjectMeta `json:"metadata,omitempty"`
  5. // Spec defines the desired identities of pods in this cronhpa.
  6. Spec CronHPASpec `json:"spec,omitempty"`
  7. // Status is the current status of pods in this CronHPA. This data
  8. // may be out of date by some window of time.
  9. Status CronHPAStatus `json:"status,omitempty"`
  10. }
  11. // A CronHPASpec is the specification of a CronHPA.
  12. type CronHPASpec struct {
  13. // scaleTargetRef points to the target resource to scale
  14. ScaleTargetRef autoscalingv2.CrossVersionObjectReference `json:"scaleTargetRef" protobuf:"bytes,1,opt,name=scaleTargetRef"`
  15. Crons []Cron `json:"crons" protobuf:"bytes,2,opt,name=crons"`
  16. }
  17. type Cron struct {
  18. // The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
  19. Schedule string `json:"schedule" protobuf:"bytes,1,opt,name=schedule"`
  20. TargetReplicas int32 `json:"targetReplicas" protobuf:"varint,2,opt,name=targetReplicas"`
  21. }
  22. // CronHPAStatus represents the current state of a CronHPA.
  23. type CronHPAStatus struct {
  24. // Information when was the last time the schedule was successfully scheduled.
  25. // +optional
  26. LastScheduleTime *metav1.Time `json:"lastScheduleTime,omitempty" protobuf:"bytes,2,opt,name=lastScheduleTime"`
  27. }
  28. // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
  29. // CronHPAList is a collection of CronHPA.
  30. type CronHPAList struct {
  31. metav1.TypeMeta `json:",inline"`
  32. metav1.ListMeta `json:"metadata,omitempty"`
  33. Items []CronHPA `json:"items"`
  34. }

使用示例

指定deployment每周五20点扩容到60个实例,周日23点缩容到30个实例

  1. apiVersion: extensions.tkestack.io/v1
  2. kind: CronHPA
  3. metadata:
  4. name: example-cron-hpa
  5. spec:
  6. scaleTargetRef:
  7. apiVersion: apps/v1
  8. kind: Deployment
  9. name: demo-deployment
  10. crons:
  11. - schedule: "0 20 * * 5"
  12. targetReplicas: 60
  13. - schedule: "0 23 * * 7"
  14. targetReplicas: 30

指定deployment每天8点到9点,19点到21点扩容到60,其他时间点恢复到10

  1. apiVersion: extensions.tkestack.io/v1
  2. kind: CronHPA
  3. metadata:
  4. name: web-servers-cronhpa
  5. spec:
  6. scaleTargetRef:
  7. apiVersion: apps/v1
  8. kind: Deployment
  9. name: web-servers
  10. crons:
  11. - schedule: "0 8 * * *"
  12. targetReplicas: 60
  13. - schedule: "0 9 * * *"
  14. targetReplicas: 10
  15. - schedule: "0 19 * * *"
  16. targetReplicas: 60
  17. - schedule: "0 21 * * *"
  18. targetReplicas: 10

查看cronhpa

  1. # kubectl get cronhpa
  2. NAME AGE
  3. example-cron-hpa 104s
  4. # kubectl get cronhpa example-cron-hpa -o yaml
  5. apiVersion: extensions.tkestack.io/v1
  6. kind: CronHPA
  7. ...
  8. spec:
  9. crons:
  10. - schedule: 0 20 * * 5
  11. targetReplicas: 60
  12. - schedule: 0 23 * * 7
  13. targetReplicas: 30
  14. scaleTargetRef:
  15. apiVersion: apps/v1
  16. kind: Deployment
  17. name: demo-deployment

删除cronhpa

  1. kubectl delete cronhpa example-cron-hpa