kube-greenis a simple Kubernetes addon that automatically shuts down (some of) your resources when you don’t need them. If you’re already using kube-green, consider adding yourself as an adopter!
Table of Contents
-
Getting Started
- Prerequisites
- Installation
-
Running Tests
- Unit Tests
- Integration Tests
-
Deployment
- Local Deployment with KinD
-
Usage
- Configuring the SleepInfo CRD
- CRD Examples
-
How kube-green Works
- Resources Handled
- Sleep and Wake Up Mechanism
- State Preservation
-
Installation Guide
- Prerequisites
- kubectl Apply
- Customization with kustomize
- Operator Lifecycle Manager (OLM)
-
Cloud Compatibility
- GKE
- AWS EKS
-
Configuration Details
- Examples
-
Metrics and Savings
- Small Cluster Example
- Numeric Summary
- Conclusion
Getting Started
These instructions will help you set up kube-green on your local machine for development and testing purposes. For installation on a live system, refer to our documentation.
Prerequisites
- Go: Ensure you have Go installed (version 1.19 or higher). Download Go
Installation
Clone the Kube-green repository and install the dependencies:
git clone https://github.com/kube-green/kube-green.git
cd kube-green
go get
Running Tests
kube-green comes with a suite of tests to ensure everything runs smoothly.Unit Tests
Run all unit tests with:
make test
Integration Tests
To run integration tests:
make e2e-test
Deployment
For development purposes, you can deploy kube-green locally using KinD and ko.Local Deployment with KinD
Start a KinD Cluster:
kind create cluster --name kube-green-development
make local-run clusterName=kube-green-development
Usage
Using kube-green is straightforward. Once installed on the cluster, you configure it using the SleepInfo Custom Resource Definition (CRD).Configuring the SleepInfo CRD
The SleepInfo CRD allows you to specify sleep and wake-up times for namespaces. Here’s how you can define it.CRD Examples
Example 1: Pods running during working hours with Europe/Rome timezone, suspend CronJobs, and exclude a deployment named api-gateway:
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: working-hours
spec:
weekdays: "1-5"
sleepAt: "20:00"
wakeUpAt: "08:00"
timeZone: "Europe/Rome"
suspendCronJobs: true
excludeRef:
- apiVersion: "apps/v1"
kind: Deployment
name: api-gateway
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: nightly-shutdown
spec:
sleepAt: "20:00"
timeZone: "Europe/Rome"
weekdays: "*"
How kube-green Works
Once installed, kube-green uses the SleepInfo CRD to manage the sleep and wake-up cycles of your Kubernetes resources.Resources Handled
kube-green currently handles:- Deployments
- CronJobs
Sleep and Wake Up Mechanism
- Sleep:
- Deployments: Replicas are set to 0.
- CronJobs: The suspend field is set to true.
- Wake Up:
- Deployments: Replicas are restored to their original count before sleep.
- CronJobs: The suspend field is restored to its original value.
State Preservation
The state of resources before sleep is saved in a Secret with the name of the SleepInfo in the respective namespace. Understanding Secrets in KubernetesInstallation Guide
Prerequisites
- Kubernetes Version: Supports versions >= 1.23 and <= 1.30.
- OpenShift: Compatible with OpenShift Container Platform v4.
- Architectures: Supports amd64 and arm64.
- Certificates: Requires certificates for webhooks. Using cert-manager is recommended.
kubectl Apply
To install the latest release with default settings:
kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml
kubectl apply -f https://github.com/kube-green/kube-green/releases/download/${RELEASE_TAG}/kube-green.yaml
Customization with kustomize
For custom configurations, clone the repository and modify the config files as needed. For example, to deploy kube-green in a different namespace, edit the kustomization.yaml file. Deploy with:
make deploy
Operator Lifecycle Manager (OLM)
Install from OperatorHub
kube-green is available on OperatorHub.io. Click the Install button and follow the instructions.
Install Kube-green Operator on OpenShift
Red Hat® OpenShift® is a critical component of creating a secure cloud-native development environment. OpenShift 4 is the best enterprise platform for building production-ready applications today and for the decade ahead. On OpenShift 4, you can install kube-green from the OperatorHub under the community operators catalog. Follow the OpenShift guide for Installation.
Cloud Compatibility
GKE
For GKE private clusters, you need to configure an additional firewall rule to allow the GKE control plane to access your webhook pod (exposed on port 9443 by default). This is necessary for the webhook component to function correctly. Learn how to add firewall rules to GKE.
AWS EKS
When using a custom CNI (like Cilium) on EKS, set hostNetwork: true in the webhook deployment to ensure the control plane can reach the webhook.
To apply this setting, uncomment the host_network_patch.yaml line in the kustomization file.Configuration Details
Apply the SleepInfo CRD in the namespace where you want to enable kube-green. The SleepInfo spec includes:- weekdays: Days of the week (for every day, 1 for Monday, 1-5 for Monday to Friday).
- sleepAt: Time (HH:mm) when the namespace goes to sleep.
- wakeUpAt (optional): Time (HH:mm) when the namespace wakes up.
- timeZone (optional): Time zone in IANA format (e.g., Europe/Rome).
- suspendDeployments (optional, default true): Whether to suspend Deployments.
- suspendCronJobs (optional, default false): Whether to suspend CronJobs.
- excludeRef (optional): Resources to exclude from sleep, specified by apiVersion, kind, name, or matchLabels.
Examples
Simplest SleepInfo Resource
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: basic-schedule
spec:
weekdays: "1-5"
sleepAt: "20:00"
wakeUpAt: "08:00"
timeZone: "Europe/Rome"
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: full-schedule
spec:
weekdays: "1-5"
sleepAt: "20:00"
wakeUpAt: "08:00"
timeZone: "Europe/Rome"
suspendCronJobs: true
excludeRef:
- apiVersion: "apps/v1"
kind: Deployment
name: api-gateway
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: sleep-only
spec:
weekdays: "1-5"
sleepAt: "20:00"
timeZone: "Europe/Rome"
suspendCronJobs: true
excludeRef:
- apiVersion: "apps/v1"
kind: Deployment
name: api-gateway
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: cronjob-suspend
spec:
weekdays: "*"
sleepAt: "20:00"
wakeUpAt: "08:00"
suspendCronJobs: true
suspendDeployments: false
excludeRef:
- apiVersion: "batch/v1"
kind: CronJob
name: do-not-suspend
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: label-exclusion
spec:
weekdays: "*"
sleepAt: "20:00"
wakeUpAt: "08:00"
suspendCronJobs: true
excludeRef:
- matchLabels:
kube-green.dev/exclude: "true"
Metrics and Savings
Small Cluster Example
In a small development cluster with 15 namespaces (kube-green enabled in the 3 largest), we used the following configuration:
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: working-hours
spec:
weekdays: "1-5"
sleepAt: "20:00"
wakeUpAt: "08:00"
timeZone: "Europe/Rome"
excludeRef:
- apiVersion: "apps/v1"
kind: Deployment
name: api-gateway
Numeric Summary
Total | With kube-green | Difference | |
---|---|---|---|
Number of Pods | 446 | 205 | 241 |
Memory Consumed [GB] | 28 | 16 | 12 |
CPU Consumed [cores] | 2 | 1.3 | 0.7 |
Memory Allocated [GB] | 34 | 22 | 12 |
CPU Allocated [cores] | 14 | 12 | 2 |
CO₂e Savings per Week [kg] | 94 | 66 | 28 |
By using kube-green, we saved 28 kg of CO₂e per week for this cluster. Over a year (52 weeks), that’s a reduction of 1,456 kg CO₂e.
Conclusion
As demonstrated, kube-green significantly reduces resource usage in Kubernetes clusters by automatically shutting down resources when they’re not needed. This not only saves costs but also reduces the environmental impact of running Kubernetes workloads. For larger clusters, the savings can lead to node downscaling, further amplifying the benefits. Note: kube-green exposes Prometheus metrics available on port 8080 at the /metrics path. You can change the metrics port using the metrics-bind-address startup variable. Exposed Metrics:- current_sleepinfo: Gauge metric providing information about the SleepInfo resources, including their names and namespaces.