By Niko Dedy Syarbaini, Solution Architect
Kubernetes has evolved into a strategic platform for deploying and scaling applications in data centers and the cloud. It provides built-in abstractions for efficiently deploying, scaling, and managing applications.
kubernetes-cronhpa-controller is Kubernetes cron horizontal pod auto scaller using crontab like scheme. You can use CronHorizontalPodAutoscaler with any kind object defined in Kubernetes which support scale subresource (such as Deployment and Statefulset).
Container Service for Kubernetes (ACK) is managed kubernetes service that helps you manage your containers to ensure high performance, it is also one of the first public publicly offered services in the world to pass the Certified Kubernetes Conformance Program.
ACK provides the following types of clusters: ACK dedicated cluster, ACK managed cluster, ACK Serveless cluster and ACK Edge Cluster.
If you don’t have Alibaba Cloud account you can register first after that you can purchase ACK product.
Login to Alibaba Cloud Console on the left menu just type ACK, for types of cluster we will use ACK managed cluster
For VPC and vSwitch if you don’t have it create it first, other configurations you can choose a default one.
When ACK cluster ready you can access use cluster using kubectl, you can easy to install it, if you use Linux OS follow this guide:
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
*Other OS you can find it on this url: https://kubernetes.io/docs/tasks/tools/?spm=5176.2020520152.0.0.59c961b19w46Wk next step it’s to configure cluster credentials, follow this step by step:
mkdir -p $HOME/.kube && cd $HOME/.kube*
vi config
Copy cluster credentials into config file. You can find cluster credential on the console ACK -> Connection Information tab.
You can choose what connection you used when access cluster, either using Public Access or Internal Access, if you have Elastic Compute Service (ECS) with same VPC you can access using Internal Access. Also it’s not recommend to access using Public Access.
If all steps configure correctly, you can access cluster using kubectl
ACK cluster is ready and we can access cluster using kubectl, now we will configure kubernetes-cronhpa-controller. we will clone repository into a instances that’s we use when install kubectl and we need to install git to clone a github repository.
git clone https://github.com/nds90/kubernetes-cronhpa-controller.git
you can see it there is a folder “kubernetes-cronhpa-controller”, go inside those folder and follow step by step below:
1) Install CRD
k8s < v1.22:
kubectl apply -f config/crds/autoscaling.alibabacloud.com_cronhorizontalpodautoscalers.yaml k8s >=v1.22:
kubectl apply -f config/crds/autoscaling.alibabacloud.com_cronhorizontalpodautoscalers.v1.22.yaml
2) Install RBAC settings
create ClusterRole:
kubectl apply -f config/rbac/rbac_role.yaml
create ClusterRolebinding and ServiceAccount:
kubectl apply -f config/rbac/rbac_role_binding.yaml
3) Deploy kubernetes-cronhpa-controller
kubectl apply -f config/deploy/deploy.yaml
4) Verify Installation
kubectl get deploy kubernetes-cronhpa-controller -n kube-system -o wide
You can see our kubernetes-cronhpa-controller is ready. Now we will use example workload in “examples-folder”.
1) Deploy some workload and cronhpa
kubectl apply -f examples/deployment_cronhpa.yaml
2) Check deployment replicas
kubectl get deploy nginx-deployment-basics
3) Describe cronhpa status
kubectl describe cronhpa cronhpa-sample
if the State of cronhpa job is Succeed that means the last execution is successful. Submitted means the cronhpa job is submitted to the cron engine but haven't be executed so far. Wait for 30s seconds and check the status.
If execute command kubectl get deploy nginx-deployment-basic at 1 minutes past the hour replicas will be scale-up to 3 and every 30s seconds past the hour replicas will be scale-down become 1
The following is an example of a cronHorizontalPodAutoscaller
The scaleTargetRef is the field to specify workload to scale. If the workload support scale subresource (such as Deployment and Statefulset), CronHorizontalPodAutoScaler should work well. CronHorizontalPodAutoScaler support multi cronhpa job in one spec.
The cronhpa job spec need three fields:
• name
name should be unique in one cronhpa spec. You can distinguish different job execution status by job name.
• schedule
The scheme of schedule is smiliar with crontab. Kubernetes-cronhpa-controller use an enhanced cron golang lib which support more expensive rules. The cron expression format is as described below:
Asterisk ( * )
The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 5th field (month) would indicates every month
Slash ( / )
Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form "*/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field. The form "N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the increment until the end of that specific range. It does not wrap around.
Comma ( , )
Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
Hyphen ( - )
Hyphens are used to define ranges. For example, 9-17 would indicate every hour between 9am and 5pm inclusive.
Question mark ( ? )
Question mark may be used instead of '*' for leaving either day-of-month or day-of-week blank.
Predefined schedules
You may use one of several pre-defined schedules in place of a cron expression.
@every where "duration" is a string accepted by time.ParseDuration (http://golang.org/pkg/time/#ParseDuration).
For example, "@every 1h30m10s" would indicate a schedule that activates after 1 hour, 30 minutes, 10 seconds, and then every interval after that.
Note:
The interval does not take the job runtime into account. For example, if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes, it will have only 2 minutes of idle time between each run.
more schedule scheme please check this doc.
Specific Date (@date)
You may use the specific date to schedule a job for scaling the workloads. It is useful when you want to do a daily promotion.
• targetSize
TargetSize is the size you desired to scale when the scheduled time arrive.
• runOnce
if runOnce is true then the job will only run and exit after the first execution.
• excludeDates
excludeDates is a dates array. The job will skip the execution when the dates is matched. The minimum unit is day. If you want to skip the date(November 15th), You can specific the excludeDates like below.
EMAS: Elevating Your Mobile App Performance in EMAS Mobile Testing
Tackling Ransomware Challenges: A Comprehensive Security Center and Cloud Firewall Approach
99 posts | 15 followers
FollowAlibaba Developer - June 30, 2020
Alibaba Developer - December 27, 2019
Alibaba Container Service - February 20, 2023
Alibaba Container Service - April 28, 2020
Alibaba Developer - June 30, 2020
Alibaba Cloud Native - September 4, 2023
99 posts | 15 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreAlibaba Cloud Container Service for Kubernetes is a fully managed cloud container management service that supports native Kubernetes and integrates with other Alibaba Cloud products.
Learn MoreAn agile and secure serverless container instance service.
Learn MoreA convenient and secure cloud-based Desktop-as-a-Service (DaaS) solution
Learn MoreMore Posts by Alibaba Cloud Indonesia