Mastering Scheduled Autoscaling in Kubernetes with cronhpa-controller
This guide explains how to install, configure, and verify the open‑source kubernetes‑cronhpa‑controller for periodic pod scaling, demonstrating step‑by‑step commands, CRD definitions, and a demo that reduces resource waste by combining scheduled scaling with cluster‑autoscaler.
Background
Container orchestration simplifies software delivery and operations, but static capacity planning often wastes resources. Typical web workloads show peak‑to‑valley traffic differences of 3‑4× or even 10×, leading to waste ratios of 25%–57%.
The standard Horizontal Pod Autoscaler (HPA) reacts to metric thresholds (CPU, memory, custom metrics) but incurs minute‑level latency in data collection, decision making, and scaling. This latency is problematic for workloads with sharp spikes, such as online games.
kubernetes‑cronhpa‑controller
The kubernetes-cronhpa-controller is an open‑source CRD‑based controller that enables time‑based scaling schedules. By defining cron‑like jobs, users can pre‑scale resources before predicted peaks and shrink them during troughs. When combined with cluster‑autoscaler, node‑level scaling further reduces infrastructure cost.
Installation
Install the CRD
kubectl apply -f config/crds/autoscaling_v1beta1_cronhorizontalpodautoscaler.yamlGrant RBAC permissions
# Create ClusterRole
kubectl apply -f config/rbac/rbac_role.yaml
# Create ClusterRoleBinding and ServiceAccount
kubectl apply -f config/rbac/rbac_role_binding.yamlDeploy the controller kubectl apply -f config/deploy/deploy.yaml Verify installation
kubectl get deploy kubernetes-cronhpa-controller -n kube-system -o wideDemo
Create a CronHorizontalPodAutoscaler resource that scales a sample deployment up to 3 replicas at the start of each minute and down to 1 replica at the 30‑second mark.
apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
name: cronhpa-sample
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1beta2
kind: Deployment
name: nginx-deployment-basic
jobs:
- name: "scale-down"
schedule: "30 */1 * * * *"
targetSize: 1
- name: "scale-up"
schedule: "0 */1 * * * *"
targetSize: 3Deploy the demo resources and observe the scaling behavior:
# Apply the CronHPA and the demo deployment
kubectl apply -f examples/deployment_cronhpa.yaml
# Check replica count of the demo deployment
kubectl get deploy nginx-deployment-basic
# Describe the CronHPA to see job status
kubectl describe cronhpa cronhpa-sampleThe describe output shows the jobs transitioning from Submitted to Succeed, confirming that the scheduled scaling took effect.
Key Specification Fields
scaleTargetRefidentifies the workload to be scaled (e.g., a Deployment). The jobs array defines cron‑style schedules ( schedule) and the desired replica count ( targetSize) for each scaling action.
Reference
GitHub repository (open source): https://github.com/AliyunContainerService/kubernetes-cronhpa-controller
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
