All Documents
Current Document

Content is empty

If you don't find the content you expect, please try another search term

Documentation

Configure Cron horizontal pod autoscaling

Last updated:2021-05-11 10:41:27

A Cron Horizontal Pod Autoscaler (CronHPA) regularly scales pod resources in a Kubernetes cluster based on Crontab. A CronHPA is applicable to the scale-out or scale-in of any resources, for example, Deployments and StatefulSets.

Scenarios

After you figure out the operating pattern of workloads during maintenance, you can configure a CronHPA for regular and automatic scale-out and scale-in to cope with expected business requirements.

Instructions

Prerequisites

A Kubernetes cluster is created in KCE, and the cluster is running properly. For more information, see Create a cluster.

Install the CronHPA plug-in

  • Log in to any node in the cluster and create a cron-hpa.yaml file.
$ kubectl apply -f cron-hpa.yaml
  • The cron-hpa.yaml file is as follows:
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  creationTimestamp: null
  labels:
    controller-tools.k8s.io: "1.0"
  name: cronhorizontalpodautoscalers.autoscaling.kce.ksyun.com
spec:
  group: autoscaling.kce.ksyun.com
  names:
    kind: CronHorizontalPodAutoscaler
    plural: cronhorizontalpodautoscalers
    shortNames:
      - cronhpa
  scope: Namespaced
  validation:
    openAPIV3Schema:
      properties:
        apiVersion:
          description: 'APIVersion defines the versioned schema of this representation
            of an object. Servers should convert recognized schemas to the latest
            internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
          type: string
        kind:
          description: 'Kind is a string value representing the REST resource this
            object represents. Servers may infer this from the endpoint the client
            submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
          type: string
        metadata:
          type: object
        spec:
          properties:
            jobs:
              items:
                properties:
                  name:
                    type: string
                  schedule:
                    type: string
                  targetSize:
                    format: int32
                    type: integer
                required:
                - name
                - schedule
                - targetSize
                type: object
              type: array
            scaleTargetRef:
              description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
                Important: Run "make" to regenerate code after modifying this file'
              properties:
                apiVersion:
                  type: string
                kind:
                  type: string
                name:
                  type: string
              required:
              - apiVersion
              - kind
              - name
              type: object
          required:
          - scaleTargetRef
          - jobs
          type: object
        status:
          properties:
            conditions:
              description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
                of cluster Important: Run "make" to regenerate code after modifying
                this file'
              items:
                properties:
                  jobId:
                    type: string
                  lastProbeTime:
                    format: date-time
                    type: string
                  message:
                    description: Human readable message indicating details about last
                      transition.
                    type: string
                  name:
                    description: Type of job condition, Complete or Failed.
                    type: string
                  schedule:
                    type: string
                  state:
                    type: string
                required:
                - name
                - jobId
                - schedule
                - state
                - lastProbeTime
                - message
                type: object
              type: array
          required:
          - conditions
          type: object
  version: v1beta1
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kubernetes-cronhpa-controller-role
rules:
- apiGroups: [""]
  resources: ["*"]
  verbs:
  - get
  - list
  - watch
  - update
- apiGroups:
  - extensions
  resources: ["*"]
  verbs:
  - get
  - list
  - watch
  - update
- apiGroups:
  - apps
  resources: ["*"]
  verbs:
  - get
  - list
  - watch
  - update
- apiGroups: [""]
  resources: ["events"]
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - patch
- apiGroups: [""]
  resources: ["configmaps"]
  verbs:
    - get
    - create
    - update
- apiGroups:
  - autoscaling.kce.ksyun.com
  resources:
  - cronhorizontalpodautoscalers
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - patch
- apiGroups:
  - autoscaling
  resources:
  - horizontalpodautoscalers
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - patch
  - delete
- apiGroups:
  - admissionregistration.k8s.io
  resources:
  - mutatingwebhookconfigurations
  - validatingwebhookconfigurations
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - patch
  - delete
- apiGroups:
  - apps.kruise.io
  resources:
  - statefulsets/scale
  - uniteddeployments/scale
  - clonesets/scale
  verbs:
  - get
  - list
  - watch
  - update
  - patch
- apiGroups:
  - autoscaling.kce.ksyun.com
  resources:
  - elasticworkloads/scale
  verbs:
  - get
  - list
  - watch
  - update
  - patch
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kubernetes-cronhpa-controller
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  creationTimestamp: null
  name: kubernetes-cronhpa-controller-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kubernetes-cronhpa-controller-role
subjects:
- kind: ServiceAccount
  name: kubernetes-cronhpa-controller
  namespace: kube-system
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubernetes-cronhpa-controller
  namespace: kube-system
  labels:
    app: kubernetes-cronhpa-controller
    controller-tools.k8s.io: "2.0"
spec:
  replicas: 1 # The default is primary and standby mode (currently cold standby)
  selector:
    matchLabels:
      app: kubernetes-cronhpa-controller
      controller-tools.k8s.io: "2.0"
  template:
    metadata:
      labels:
        app: kubernetes-cronhpa-controller
        controller-tools.k8s.io: "2.0"
    spec:
      containers:
      - image: hub.kce.ksyun.com/ksyun/kubernetes-cronhpa-controller:v1.0.0
        imagePullPolicy: Always
        name: kubernetes-cronhpa-controller
        env:
        - name: TZ
          value: "Asia/Shanghai"
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi
      serviceAccount: kubernetes-cronhpa-controller
  • Check whether the plug-in is installed.
# Check whether the custom resource is installed.
$ kubectl api-resources | grep cronhpa
cronhorizontalpodautoscalers      cronhpa      autoscaling.kce.ksyun.com      true         CronHorizontalPodAutoscaler

# Check whether the CronHPA Controller is installed.
$ kubectl get deploy kubernetes-cronhpa-controller -n kube-system
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-cronhpa-controller   1/1     1            1                 4m11s

Test the CronHPA plug-in

This section describes how to deploy and test a CronHPA after the CronHPA plug-in is installed.

  • Deploy the test object and CronHPA.
$ kubectl apply -f cronhpa-example.yaml
  • The cronhpa-example.yaml file is as follows:
---
apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment-basic
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
        ports:
        - containerPort: 80

---     
apiVersion: autoscaling.kce.ksyun.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
  labels:
    controller-tools.k8s.io: "1.0"
  name: cronhpa-sample
spec:
   scaleTargetRef:
      apiVersion: apps/v1beta2
      kind: Deployment
      name: nginx-deployment-basic
   excludeDates:
   - "* * * 15 11 *"                            # exclude November 15th
   - "* * * * * 5"                              # exclude every Friday
   jobs:
   - name: "scale-down"
     schedule: "30 */1 * * * *"                 # Indicates that the number of replicas for the Deployment nginx-deployment-basic is adjusted to 1 at the 30th second of every minute. 
     targetSize: 1
   - name: "scale-up"
     schedule: "0 */1 * * * *"                  # Indicates that the number of replicas for the Deployment nginx-deployment-basic is adjusted to 3 at the 0th second of every minute.
     targetSize: 3
  • The following table describes the fields in the CronHPA configuration.
FieldDescriptionRemarks
scaleTargetRefThe scaling object. A CronHPA can scale sub-resources of the object as long as the object supports sub-resource scaling.A CronHPA can also adjust the number of pods. To ensure stability, the system uses the larger one of the expected number of replicas specified in the CronHPA configuration and that specified in the HPA configuration as the final value for scaling.
excludeDatesThe date array. The job is skipped on the dates specified by this field.This field is specified by day.
jobsThe CronHPA jobs. You can configure multiple CronHPA jobs in one spec. The following fields are configured for each CronHPA job:
- name: the name of the job, which must be unique in the CronHPA configuration.
- schedule: the scaling schedule, which is similar to Crontab.
- targetSize: the expected number of pods at the scheduled time.
- runOnce: specifies whether the job runs only once. If this field is set to true, the job runs only once and then exits.
The Cron expression contains the following fields:
Field nameMandatoryAllowed valuesAllowed special characters
SecondsYes0-59* / , -
MinutesYes0-59* / , -
HoursYes0-23* / , -
Day of monthYes1-31* / , - ?
MonthYes1-12 or JAN-DEC* / , -
Day of weekYes0-6 or SUN-SAT* / , - ?
  • Check the CronHPA instance.
$ kubectl get cronhpa 
NAME             AGE
cronhpa-sample   15s
  • Check details about the cronhpa-sample instance.
$ kubectl describe cronhpa cronhpa-sample
Events:
  Type    Reason  Age            From                                   Message
  ----    ------  ----           ----                                   -------
  Normal  Succeed  8m31s                cron-horizontal-pod-autoscaler  cron hpa job scale-down executed successfully. current replicas:2, desired replicas:1.
  Normal  Succeed  31s (x8 over 7m31s)  cron-horizontal-pod-autoscaler  cron hpa job scale-down executed successfully. current replicas:3, desired replicas:1.
  Normal  Succeed  1s (x9 over 8m1s)    cron-horizontal-pod-autoscaler  cron hpa job scale-up executed successfully. current replicas:1, desired replicas:3.
  • Check the changes of replicas for the Deployment nginx-deployment-basic.
$ kubectl get deploy nginx-deployment-basic  -w
NAME                    READY   UP-TO-DATE   AVAILABLE      AGE
nginx-deployment-basic   1/1      1          1             3m31s
nginx-deployment-basic   1/3      1          1             3m48s
nginx-deployment-basic   1/3      1          1             3m48s
nginx-deployment-basic   1/3      3          1             3m48s   
nginx-deployment-basic   2/3      3          2             3m50s
nginx-deployment-basic   3/3      3          3             3m50s
nginx-deployment-basic   3/1      3          3             4m18s
nginx-deployment-basic   3/1      3          3             4m18s
nginx-deployment-basic   1/1      1          1             4m18s
nginx-deployment-basic   1/3      1          1             4m48s
nginx-deployment-basic   1/3      1          1             4m48s
nginx-deployment-basic   1/3      1          1             4m48s
nginx-deployment-basic   1/3      3          1             4m48s
nginx-deployment-basic   2/3      3          2             4m49s
nginx-deployment-basic   3/3      3          3             4m49s
On this page
Pure ModeNormal Mode

Pure Mode

Click to preview the document content in full screen
Feedback