CronHPATrait
Use Cron expressions to periodically scale your workload.
Supported workloads:
- ContainerizedWorkload
- Deployment
- StatefulSet
Prerequisites
- Please follow common prerequisites to install OAM Controllers.
- 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
...
# 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.