cronhpatrait

command module
v0.0.0-...-9421b25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

CronHPATrait

Use Cron expressions to periodically scale your workload.

Supported workloads:

  • ContainerizedWorkload
  • Deployment
  • StatefulSet

Prerequisites

  1. Please follow common prerequisites to install OAM Controllers.
  2. Please install kubernetes-cronhpa-controller.

Getting started

  • Get the CronHPATrait project to your GOPATH
git clone https://github.com/oam-dev/catalog.git
  • Install CRD to your Kubernetes cluster
cd catalog/traits/cronhpatrait/

make install
  • Run the CronHPATrait controller
go run main.go
ContainerizedWorkload
  • Apply the sample ContainerizedWorkload
kubectl apply -f config/samples/containerized
# ./config/samples/contaierized/sample_application_config.yaml

  ...
      traits:
        - trait:
            apiVersion: core.oam.dev/v1alpha2
            kind: CronHPATrait
            metadata:
              name: example-cronhpa-trait
            spec:
              jobs:
                - name: "scale-down"
                  schedule: "30 */1 * * * *"
                  targetSize: 1
                - name: "scale-up"
                  schedule: "0 */1 * * * *"
                  targetSize: 3
  • Verify CronHPATrait you should see a deployment created by ContainerizedWorkload
kubectl get deployment
NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
example-appconfig-workload-deployment   1/1     1            1           3s
  • And a CronHPA automatically created by CronHPATrait controller
kubectl get cronhpa
NAME                    AGE
example-cronhpa-trait   35s
  • You should see a CronHPA and it's spec looking like below
kubectl get cronhpa
NAME                    AGE
example-cronhpa-trait   2m18s

kubectl describe cronhpa
...
  Exclude Dates:  <nil>
  Jobs:
    Name:         scale-down
    Run Once:     false
    Schedule:     30 */1 * * * *
    Target Size:  1
    Name:         scale-up
    Run Once:     false
    Schedule:     0 */1 * * * *
    Target Size:  3
...
  • Verrify CronHPA works
# kubectl get deployment -w

NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
example-appconfig-workload-deployment   1/1     1            1           2m
example-appconfig-workload-deployment   1/3     1            1           2m
example-appconfig-workload-deployment   1/3     1            1           2m
example-appconfig-workload-deployment   1/3     1            1           2m
example-appconfig-workload-deployment   1/3     3            1           2m
example-appconfig-workload-deployment   2/3     3            2           2m
example-appconfig-workload-deployment   2/3     3            2           2m
example-appconfig-workload-deployment   3/3     3            3           2m
example-appconfig-workload-deployment   3/1     3            3           2m
example-appconfig-workload-deployment   3/1     3            3           2m
example-appconfig-workload-deployment   3/1     3            3           2m
example-appconfig-workload-deployment   1/1     1            1           2m
K8s Deployment
  • Apply the sample Deployment
kubectl apply -f config/samples/deployment
# ./config/samples/deployment/sample_workload_definition.yaml
  
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
metadata:
  name: deployments.apps
spec:
  definitionRef:
    name: deployments.apps
# ./config/samples/deployment/sample_application_config.yaml

  ...
      traits:
        - trait:
            apiVersion: core.oam.dev/v1alpha2
            kind: CronHPATrait
            metadata:
              name: example-cronhpa-trait
            spec:
              jobs:
                - name: "scale-down"
                  schedule: "30 */1 * * * *"
                  targetSize: 1
                  runOnce: true
                - name: "scale-up"
                  schedule: "0 */1 * * * *"
                  targetSize: 3
                  runOnce: true
              excludeDates:
                # exclude November 15th
                - "* * * 15 11 *"
                # exclude every Friday
                - "* * * * * 5"
  • Verify CronHPATrait you should see a deployment looking like below
kubectl get deployment
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    1/1     1            1           15s
  • And a CronHPA automatically created by CronHPATrait controller
kubectl get cronhpa
NAME                    AGE
example-cronhpa-trait   28s
  • You should see a CronHPA and it's spec looking like below
kubectl get cronhpa
NAME                    AGE
example-cronhpa-trait   28s

kubectl describe cronhpa
...
  Exclude Dates:
    * * * 15 11 *
    * * * * * 5
  Jobs:
    Name:         scale-down
    Run Once:     true
    Schedule:     30 */1 * * * *
    Target Size:  1
    Name:         scale-up
    Run Once:     true
    Schedule:     0 */1 * * * *
    Target Size:  3
  Scale Target Ref:
    API Version:  apps/v1
    Kind:         Deployment
    Name:         web
...
  • Verrify CronHPA works and the result is same as ContainerizedWorkload
K8s StatefulSet
  • Apply the sample StatefulSet
kubectl apply -f config/samples/statefulset
# ./config/samples/statefulset/sample_workload_definition.yaml
  
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
metadata:
  name: statefulsets.apps
spec:
  definitionRef:
    name: statefulsets.apps
# ./config/samples/statefulset/sample_application_config.yaml

  ...
      traits:
        - trait:
            apiVersion: core.oam.dev/v1alpha2
            kind: CronHPATrait
            metadata:
              name: example-cronhpa-trait
            spec:
              jobs:
                - name: "scale-down"
                  schedule: "30 */1 * * * *"
                  targetSize: 1
                - name: "scale-up"
                  schedule: "0 */1 * * * *"
                  targetSize: 3
  • Verify CronHPATrait you should see a statefulset looking like below
kubectl get statefulset
NAME   READY   AGE
web    1/1     22s
  • And a CronHPA automatically created by CronHPATrait controller
kubectl get cronhpa
NAME                    AGE
example-cronhpa-trait   62s
  • You should see a CronHPA and it's spec looking like below
kubectl get cronhpa
NAME                    AGE
example-cronhpa-trait   62s

kubectl describe cronhpa
...
  Exclude Dates:  <nil>
  Jobs:
    Name:         scale-down
    Run Once:     false
    Schedule:     30 */1 * * * *
    Target Size:  1
    Name:         scale-up
    Run Once:     false
    Schedule:     0 */1 * * * *
    Target Size:  3
...
  • Verrify CronHPA works and the result is same as ContainerizedWorkload

How it work?

Essentially, the CronHPATrait controller will generate CronHPA based on the workload spec.

In detail:

  • If the workload type is ContainerizedWorkload which has two child resources (Deployment and Service), CronHPATrait can help to create corresponding CronHPA.
  • If the workload type is K8S native resources (StatefulSet or Deployment), CronHPATrait can help to create a CronHPA.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
api
v1alpha2
Package v1alpha2 contains API Schema definitions for the core v1alpha2 API group +kubebuilder:object:generate=true +groupName=core.oam.dev
Package v1alpha2 contains API Schema definitions for the core v1alpha2 API group +kubebuilder:object:generate=true +groupName=core.oam.dev

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL