See Part 1 here:
Reverse Proxies Visually Explained
Overview on Reverse Proxies:
Reverse Proxy Explained
Testing Kubernetes Load Balancing and Monitoring OrderBot Output
Alright, we’ve got Foobar OrderBot running in Kubernetes, scaling up and down like a champ. But now comes the big question:
How do we actually see the output of these dynamically created and destroyed bots?
And more importantly:
- How do we test if Kubernetes is really load balancing?
- How do we create a test scenario that forces Kubernetes to scale up?
- How do we track which OrderBots handle which requests?
- How do we monitor when OrderBots are added and removed?
Well, buckle up because we’re diving deep into testing, monitoring, and visualizing Kubernetes in action!
Step 1: Setting Kubernetes to Use Low Resource Thresholds
To make scaling happen fast, we need to lower the CPU and memory thresholds that trigger new OrderBots to spin up.
Update Your Deployment with Low CPU Requests
Modify your deployment.yaml
to use minimal CPU requests:
|
|
Apply the changes:
|
|
Now, Kubernetes will scale up quickly when CPU usage increases.
Step 2: Forcing Load Balancing with a Stress Test
Now, let’s force Kubernetes to load balance by sending a massive flood of requests.
Create a Load Test Script (load_test.py
)
This Python script will hammer the OrderBots with requests:
|
|
Run the script:
|
|
Since we set low CPU limits, Kubernetes should scale up new OrderBots dynamically!
Step 3: Watching Kubernetes Scale Up and Down
Now let’s see Kubernetes adding and removing OrderBots in real time.
Watch the Kubernetes Pods Live
|
|
This will show output like:
|
|
You’ll see new pods being created as requests flood in.
Watching Logs for Each OrderBot
To see which OrderBots are handling which requests, open a separate terminal and run:
|
|
This will stream logs like:
|
|
Now, we can see that different OrderBots are processing different requests!
Step 4: Monitoring Kubernetes Load Balancing with Metrics
To visualize load balancing, install Metrics Server for Kubernetes:
|
|
Then, monitor CPU usage in real time:
|
|
Example output:
|
|
This confirms that multiple OrderBots are sharing the workload.
Step 5: Testing Load Balancing on Localhost
Run Kubernetes Locally with Minikube
If you’re using Minikube, start it with a LoadBalancer enabled:
|
|
Then expose your service:
|
|
Now, point ORDERBOT_URL
in your load_test.py
script to Minikube’s URL.
Step 6: How Do We Know It’s Load Balancing?
✅ Watching Pods: kubectl get pods --watch
confirms new bots are created and removed.
✅ Checking Logs: kubectl logs -f deployment/foobar-orderbot
shows different bots processing requests.
✅ Tracking CPU Usage: kubectl top pods
confirms multiple OrderBots are sharing load.
✅ Running a Load Test: Our load_test.py
script forces Kubernetes to scale up dynamically.
✅ Local Testing with Minikube: Running services via minikube service
proves load balancing works even on a dev machine.
Conclusion
We’ve now fully tested and visualized Kubernetes load balancing in action! 🚀
Step | Action |
---|---|
1 | Lower CPU thresholds for rapid scaling |
2 | Run a Python script to flood OrderBots with requests |
3 | Watch pods dynamically scale up/down with kubectl get pods --watch |
4 | Track request distribution via kubectl logs -f |
5 | Monitor CPU load via kubectl top pods |
6 | Test load balancing locally using Minikube |
Reference Links
Now, go forth and watch Kubernetes do the heavy lifting! 🚀🔥