90 / 95 · 05 Performance & Chaos Engineering · Kubernetes Scaling and Container Performance Testing← prev⊞ allnext →☰ Read as one page
14.2k6 Test for HPA Validation
// k6-container-scaling-test.js
// Verify Kubernetes HPA responds correctly to traffic spikes
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Trend } from 'k6/metrics';
const scalingLatency = new Trend('scaling_response_time', true);
export const options = {
scenarios: {
spike: {
executor: 'ramping-arrival-rate',
startRate: 10,
timeUnit: '1s',
preAllocatedVUs: 50,
maxVUs: 500,
stages: [
{ duration: '1m', target: 10 }, // baseline
{ duration: '30s', target: 200 }, // sudden spike
{ duration: '5m', target: 200 }, // sustain spike (HPA should scale)
{ duration: '30s', target: 10 }, // drop back
{ duration: '5m', target: 10 }, // verify scale-down
],
},
},
thresholds: {
// Even during spike, 95th percentile should stay under 2s
// (once HPA has scaled, which may take 1-2 minutes)
http_req_duration: ['p(95)<2000'],
http_req_failed: ['rate<0.05'],
},
};
export default function () {
const res = http.get('https://app.example.com/api/heavy-computation');
scalingLatency.add(res.timings.duration);
check(res, {
'status is 200': (r) => r.status === 200,
'no 503 (service unavailable)': (r) => r.status !== 503,
});
}
What to Monitor During the Test
While k6 runs, monitor the Kubernetes cluster in a parallel terminal or dashboard:
# Watch pod count change in real-time
kubectl get pods -l app=my-service -w
# Watch HPA status
kubectl get hpa my-service-hpa -w
# Check HPA events for scaling decisions
kubectl describe hpa my-service-hpa
Expected Timeline
T+0:00 - 10 req/s, 3 pods (baseline)
T+1:00 - Spike to 200 req/s, latency increases immediately
T+1:30 - HPA detects CPU > target, begins scaling
T+2:00 - New pods scheduled, pulling images
T+2:30 - New pods running, latency begins to decrease
T+3:00 - Full scale-up complete (e.g., 15 pods), latency normalized
T+6:30 - Traffic drops to 10 req/s
T+7:00 - HPA begins scale-down (cooldown period)
T+11:30 - Scale-down complete, back to 3 pods
Critical question: What happens to users during T+1:00 to T+3:00 (the scaling gap)? This is where you discover if your HPA configuration is adequate.