Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Last active August 23, 2024 09:50
Show Gist options
  • Save hamza-cskn/8f7a06511ca62c9dd4369a3947c886dd to your computer and use it in GitHub Desktop.
Save hamza-cskn/8f7a06511ca62c9dd4369a3947c886dd to your computer and use it in GitHub Desktop.
Replica of Kubernetes HPA algorithm and auto scale simulation.
class K8sHpaSimulation {
public static void main(String[] args) {
final double tolerance = 0.1; // 0 < x < 1
final double targetUtilization = 50; // 0 < x < 100
final double requestedMetricPerPod = 40;
final double limitMetricPerPod = 80;
final int podScaleUpPolicyCount = 16;
final int podScaleDownPolicyCount = 2;
/* ^ Config Values ^ */
final double scaleUpLimit = targetUtilization + (tolerance * 100) / 2;
final double scaleDownLimit = targetUtilization - (tolerance * 100) / 2;
int currentReplicas = 2;
/* ^ Set current replica to simulate ^ */
int desiredReplicas;
double currentUtilization;
System.out.print("Scale steps: " + currentReplicas);
do {
// assume all pods using 1m cpu but one of them using cpu at its limit.
double totalMetricUsage = currentReplicas - 1 + limitMetricPerPod;
currentUtilization = (totalMetricUsage / (currentReplicas * requestedMetricPerPod)) * 100;
if (currentUtilization > scaleUpLimit || currentUtilization < scaleDownLimit) {
desiredReplicas = (int) Math.ceil((currentReplicas * currentUtilization) / targetUtilization);
} else {
desiredReplicas = currentReplicas;
}
if (desiredReplicas > currentReplicas) {
// limit scale up speed
currentReplicas += Math.min(desiredReplicas - currentReplicas, podScaleUpPolicyCount);
System.out.print(" -> " + currentReplicas);
} else if (desiredReplicas < currentReplicas) {
// limit scale down speed
currentReplicas -= Math.min(currentReplicas - desiredReplicas, podScaleDownPolicyCount);
System.out.print(" -> " + currentReplicas);
} else {
break;
}
} while (targetUtilization < currentUtilization);
System.out.println();
if (targetUtilization < currentUtilization)
System.out.println("Reason of scale stop: Tolerated.");
else
System.out.println("Reason of scale stop: Balanced.");
System.out.println("Current Utilization: " + currentUtilization);
System.out.println("Target Utilization: " + targetUtilization);
System.out.println("Scale Up Limit: " + (scaleUpLimit >= 100 ? "No limit" : scaleUpLimit));
System.out.println("Scale Down Limit: " + (scaleDownLimit < 0 ? "No limit" : scaleDownLimit));
System.out.println("Desired Replicas: " + desiredReplicas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment