使用CronFederatedHPA自动伸缩跨集群Deployment

在Karmada中,CronFederatedHPA 负责扩展工作负载(如Deployments)的副本或 FederatedHPA 的 minReplicas/maxReplicas。其目的是为了主动扩展业务,以处理突发的负载峰值。

本文提供了一个示例,说明如何为跨集群部署 nginx deployment 启用CronFederatedHPA。

前提条件

Karmada 已安装

您可以参考 快速入门 安装 Karmada,或直接运行 hack/local-up-karmada.sh 脚本,该脚本也用于运行 E2E 测试。

member1member2 集群中部署工作负载

我们需要在 member1 和 member2 集群中部署 deployment(2 个副本):

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - image: nginx
  19. name: nginx
  20. resources:
  21. requests:
  22. cpu: 25m
  23. memory: 64Mi
  24. limits:
  25. cpu: 25m
  26. memory: 64Mi
  27. ---
  28. apiVersion: policy.karmada.io/v1alpha1
  29. kind: PropagationPolicy
  30. metadata:
  31. name: nginx-propagation
  32. spec:
  33. resourceSelectors:
  34. - apiVersion: apps/v1
  35. kind: Deployment
  36. name: nginx
  37. placement:
  38. clusterAffinity:
  39. clusterNames:
  40. - member1
  41. - member2
  42. replicaScheduling:
  43. replicaDivisionPreference: Weighted
  44. replicaSchedulingType: Divided
  45. weightPreference:
  46. staticWeightList:
  47. - targetCluster:
  48. clusterNames:
  49. - member1
  50. weight: 1
  51. - targetCluster:
  52. clusterNames:
  53. - member2
  54. weight: 1

部署完成后,您可以检查已创建的 pods:

  1. $ karmadactl get pods
  2. NAME CLUSTER READY STATUS RESTARTS AGE
  3. nginx-777bc7b6d7-rmmzv member1 1/1 Running 0 104s
  4. nginx-777bc7b6d7-9gf7g member2 1/1 Running 0 104s

在 Karmada 控制平面部署 CronFederatedHPA

然后,在 Karmada 控制平面中部署 CronFederatedHPA,以扩容 Deployment:

  1. apiVersion: autoscaling.karmada.io/v1alpha1
  2. kind: CronFederatedHPA
  3. metadata:
  4. name: nginx-cronfhpa
  5. namespace: default
  6. spec:
  7. scaleTargetRef:
  8. apiVersion: apps/v1
  9. kind: Deployment
  10. name: nginx
  11. rules:
  12. - name: "scale-up"
  13. schedule: "*/1 * * * *"
  14. targetReplicas: 5
  15. suspend: false

spec.schedule 遵循以下格式:

  1. # ┌───────────── minute (0 - 59)
  2. # │ ┌───────────── hour (0 - 23)
  3. # │ │ ┌───────────── day of the month (1 - 31)
  4. # │ │ │ ┌───────────── month (1 - 12)
  5. # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
  6. # │ │ │ │ │ 7 is also Sunday on some systems)
  7. # │ │ │ │ │ OR sun, mon, tue, wed, thu, fri, sat
  8. # │ │ │ │ │
  9. # * * * * *

表达式*/1 * * * *的意思是nginx deployment的副本应该每分钟更新为5个,确保了处理接下来的流量突发流量洪峰。

测试伸缩功能

一分钟后,通过CronFederatedHPA将nginx部署的副本扩展到5个。现在让我们检查Pod的数量,以验证是否按预期进行了扩展:

  1. $ karmadactl get pods
  2. NAME CLUSTER READY STATUS RESTARTS AGE
  3. nginx-777bc7b6d7-8v9b4 member2 1/1 Running 0 18s
  4. nginx-777bc7b6d7-9gf7g member2 1/1 Running 0 8m2s
  5. nginx-777bc7b6d7-5snhz member1 1/1 Running 0 18s
  6. nginx-777bc7b6d7-rmmzv member1 1/1 Running 0 8m2s
  7. nginx-777bc7b6d7-z9kwg member1 1/1 Running 0 18s

通过检查 CronFederatedHPA 的状态字段,您可以访问扩展历史记录:

  1. $ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get cronfhpa -oyaml
  2. -> # kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get cronfhpa/nginx-cronfhpa -oyaml
  3. apiVersion: autoscaling.karmada.io/v1alpha1
  4. kind: CronFederatedHPA
  5. metadata:
  6. name: nginx-cronfhpa
  7. namespace: default
  8. spec:
  9. rules:
  10. - failedHistoryLimit: 3
  11. name: scale-up
  12. schedule: '*/1 * * * *'
  13. successfulHistoryLimit: 3
  14. suspend: false
  15. targetReplicas: 5
  16. scaleTargetRef:
  17. apiVersion: apps/v1
  18. kind: Deployment
  19. name: nginx
  20. status:
  21. executionHistories:
  22. - nextExecutionTime: "2023-07-29T03:27:00Z" # 下一次执行时间
  23. ruleName: scale-up
  24. successfulExecutions:
  25. - appliedReplicas: 5 # CronFederatedHPA将replicas更新为5
  26. executionTime: "2023-07-29T03:26:00Z" # 上一次实际执行时间
  27. scheduleTime: "2023-07-29T03:26:00Z" # 上一次期待执行时间

伸缩历史包括成功和失败操作的信息。