Automating Namespace Sleep Cycles for Kubernetes Cost Reduction

To implement automated resource suspension, first ensure the cluster has the necessary certification authority and the operator itself installed.

Deploy the certificate manager using the following command:

kubectl apply -f https://github.com/jetstack/cert-manager/releases/latest/download/cert-manager.yaml --wait

Next, provision the scheduling operator into the cluster:

kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml

Configuring Idle Schedules

Define a SleepInfo custom resource within the target namespace to control workload lifecycles. This configuration dictates when workloads scale down or suspend operations based on specific time windows.

Key parameters include:

  • weekdays: Define the days of operation. Use * for daily execution, numeric values like 1 for Monday, or ranges such as 1-5 for weekdays.
  • sleepAt: Specify the start time for suspension (HH:mm). Setting this to *:* allows for continuous cycling if configured alongside other triggers.
  • wakeUpAt (Optional): Determine when resources should return to normal state (HH:mm). Omitting this requires manual intervention to restore pods after they have been scaled down.
  • timeZone (Optional): Set the IANA timezone standard (e.g., Asia/Shanghai). Defaults to UTC.
  • suspendDeployments (Optional): Boolean flag. If true, deployments will be scaled to zero replicas. Default is true.
  • suspendCronJobs (Optional): Boolean flag. If true, active cronjob schedules will be paused. Default is false.
  • excludeRef (Optional): A list of objects to bypass the suspension logic. You can define specific API versions (apps/v1, batch/v1beta1) and kind types (Deployment, CronJob). Filtering can be done via exact name matching or label selectors.

Implementation Example

Create a policy named night-idle-policy to suspend development resources overnight in the Pacific timezone while excluding critical database services.

apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
  name: night-idle-policy
spec:
  weekdays: "1-5"
  sleepAt: "18:00"
  wakeUpAt: "08:00"
  timeZone: "America/Los_Angeles"
  suspendDeployments: true
  suspendCronJobs: false
  excludeRef:
    - apiVersion: apps/v1
      kind: Deployment
      name: production-db-cluster
    - matchLabels:
        env: staging

Operational Behavior

Once applied, the controller monitors the cluster schedule. At the defined sleepAt time, it reduces replica counts to zero for managed Deployments and pauses CronJobs unless explicitly excluded. During the wakeUpAt window, the system attempts to revert the state to pre-sleep configurations, ensuring services resume automatically without manual orchestration.

Tags: kubernetes kube-green cost-optimization automation devops

Posted on Tue, 18 Aug 2026 16:44:25 +0000 by doc