Carbon-Aware Kubernetes: Scaling Workloads Based on Grid Intensity
Cloud computing has a massive physical footprint. While we often treat 'the cloud' as an abstract layer of limitless resources, every CPU cycle and every byte of storage translates directly to electricity consumption. As engineers, we have spent decades optimizing for performance, cost, and availability. However, a fourth pillar is rapidly becoming essential: sustainability.
Most modern infrastructure scales based on demand—CPU utilization, memory pressure, or request latency. While efficient from a cost perspective, this approach is 'carbon-blind.' It treats a kilowatt-hour generated by a coal plant the same as one generated by a wind farm. To build truly sustainable systems, we need to transition to carbon-aware architectures.
In this article, we will explore how to implement carbon-aware workload scheduling in Kubernetes using KEDA (Kubernetes Event-driven Autoscaling) and the Green Software Foundation’s Carbon-Aware SDK.
The Concept: Carbon Intensity and Temporal Shifting
Before diving into the implementation, we must understand the core metric: Carbon Intensity. This is a measure of how many grams of Carbon Dioxide (CO2e) are emitted per kilowatt-hour of electricity produced.
Carbon intensity fluctuates throughout the day based on the mix of energy sources powering the grid. When the wind is blowing or the sun is shining, intensity is low. When the grid relies on 'peaker' gas plants to meet high demand, intensity spikes.
Temporal Shifting is the practice of moving non-urgent workloads (like batch processing, model training, or backups) to times when the carbon intensity is lowest. By making our Kubernetes clusters aware of these fluctuations, we can significantly reduce the operational emissions of our applications without changing our underlying hardware.
The Architecture: KEDA and the Carbon-Aware SDK
To build a carbon-aware scaler, we need three primary components:
- A Data Provider: Services like WattTime or Electricity Maps that provide real-time and forecasted carbon intensity data for specific geographic regions.
- The Carbon-Aware SDK: An open-source toolset from the Green Software Foundation that abstracts these different data providers into a consistent API.
- KEDA: An event-driven autoscaler for Kubernetes that allows us to scale workloads based on external metrics (in this case, carbon intensity) rather than just standard resource metrics.
Why KEDA?
Standard Kubernetes Horizontal Pod Autoscalers (HPA) are great for CPU and Memory. However, scaling based on external, non-resource metrics requires a more flexible approach. KEDA acts as a metrics adapter, fetching data from external sources and feeding it to the HPA to drive scaling decisions. By using KEDA's 'External Scaler' capability, we can create a bridge between the Carbon-Aware SDK and our cluster.
Step 1: Setting Up the Carbon-Aware SDK
The Carbon-Aware SDK provides a Web API that we can deploy within our cluster. This API acts as a proxy, fetching data from WattTime or Electricity Maps and providing a standardized JSON response.
You can deploy the SDK as a containerized service. Once running, you can query it for the 'Best' time to run a job or the current 'Emissions' for your region.
# Example query to the SDK API GET /emissions/bylocation?location=eastus&time=2023-10-27T10:00:00Z
The SDK handles the complexities of API keys, caching, and data normalization across different regions, which is crucial if your cluster spans multiple availability zones.
Step 2: Implementing the KEDA External Scaler
KEDA doesn't have a built-in 'Carbon Scaler' yet, but it supports External Scalers via gRPC. To make this work, we write a small 'Carbon Metrics Adapter' (a lightweight Go or Node.js service) that performs the following logic:
- KEDA polls our adapter to ask: 'What is the current carbon intensity?'
- Our adapter queries the Carbon-Aware SDK.
- Our adapter compares the current intensity against a user-defined threshold.
- If the intensity is below the threshold, the adapter tells KEDA to scale up. If it's above, it tells KEDA to scale down (or keep the count at zero).
Example: The ScaledObject Configuration
Once the adapter is running, we define a ScaledObject in Kubernetes. This CRD tells KEDA which deployment to scale and how to talk to our carbon adapter.
apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: carbon-aware-batch-job namespace: processing spec: scaleTargetRef: name: data-worker-deployment minReplicaCount: 0 maxReplicaCount: 20 triggers: - type: external metadata: scalerAddress: carbon-metrics-adapter.svc.cluster.local:50051 threshold: "250" # Grams of CO2e/kWh region: "westus"
In this scenario, if the grid intensity in westus drops below 250g/kWh, KEDA will trigger the scaling of our data-worker-deployment. If the intensity rises, the workers are scaled back down to zero, effectively pausing the work until the grid 'cleans up.'
Step 3: Differentiating Workload Types
Not every workload is a candidate for carbon-aware scaling. We generally categorize workloads into two buckets:
1. Delay-Tolerant (Batch) Workloads
These are the low-hanging fruit. Image processing, data warehouse ingestion, and periodic reporting can usually wait a few hours. For these, we use Temporal Shifting. We set aggressive carbon thresholds, ensuring these jobs only run during peak renewable generation.
2. User-Facing (Demand-Driven) Workloads
You cannot turn off your web API just because the sun went down. For these, we use Carbon-Aware Shaping. Instead of scaling to zero, we might adjust the 'quality' of the service. For example, during high carbon intensity, we might disable non-essential background tasks, reduce image resolution, or switch to a more efficient (but less accurate) AI model version.
Real-World Scenario: The 'Green' CI/CD Pipeline
Consider a large enterprise running thousands of CI/CD builds daily. These builds are resource-intensive. By implementing a carbon-aware KEDA scaler for the build agents:
- Priority 1 (Hotfixes): Bypasses the carbon scaler; runs immediately.
- Priority 2 (Feature Branches): Runs only when intensity is below 400g/kWh.
- Priority 3 (Nightly Integration Tests): Runs only at the absolute 'cleanest' window predicted by the SDK within a 12-hour lookahead.
This approach doesn't just save carbon; it often saves money. In many energy markets, low carbon intensity correlates with lower wholesale electricity prices (though not always), making this a win-win for both the environment and the CFO.
Challenges and Considerations
Implementing carbon-aware scheduling is not without its hurdles:
- Data Latency: Carbon intensity data is often delayed by 15-30 minutes. Your scaling logic must account for this lag.
- Threshold Management: Setting a static threshold (e.g., 300g/kWh) can be problematic. In some regions, the grid never gets that clean. A better approach is to use 'relative' thresholds—scaling when intensity is in the bottom 20% of the daily average.
- The 'Rebound' Effect: If every company shifts their load to the exact same 'clean' window, they might create a new demand peak that forces the grid to spin up dirty peaker plants. Coordination at scale is an ongoing area of research in the Green Software community.
Strategy for Technical Decision Makers
If you are looking to introduce this to your organization, start small. Don't try to refactor your entire microservices architecture on day one.
- Identify your 'Carbon Hogs': Use tools like Kepler (Kubernetes-based Efficient Power Level Exporter) to find which workloads consume the most energy.
- Audit for Delay-Tolerance: Find the jobs that don't have strict SLAs.
- Implement a Pilot: Set up the Carbon-Aware SDK and KEDA for a single non-critical batch process.
- Measure and Report: Document the estimated CO2e saved. This data is invaluable for ESG (Environmental, Social, and Governance) reporting and helps build the business case for further green-ops initiatives.
Conclusion
Carbon-aware scheduling represents a shift in how we think about cloud-native architecture. By moving from 'always-on' to 'right-time' computing, we leverage the elasticity of Kubernetes to solve a physical-world problem. Using KEDA and the Carbon-Aware SDK provides a standardized, scalable way to integrate environmental metrics into our existing DevOps workflows.
As the grid becomes more volatile with the integration of intermittent renewables, the ability to shift workloads dynamically will become a standard requirement for high-maturity engineering teams. Start experimenting now: identify your batch jobs, deploy the SDK, and let the state of the grid guide your scaling logic.