See Part 2 here:
Overview on Reverse Proxies:
Reverse Proxy Explained
The Company: Foobar Inc.
So, picture this: We have a company called Foobar Inc. And, shocker, we sell foobars.
Clever, huh? I thought so.
Now, our customers are hardcore foobar enthusiasts.
They don’t want fancy web interfaces or mobile apps.. (sorry IPhone and Android users… :) )
No, sir! They use Python command-line console apps to order their beloved foobars.
Because if you’re REALLY into foobars, how ELSE would you order them?
The Setup: Customer and OrderBot
We have two Python scripts:
foobar_customer.py
: Acts as the customer, sending REST calls to order foobars.foobar_orderbot.py
: A Flask-based REST service that takes orders and confirms them.
Everything runs on localhost, so you can try this out on your own machine!
foobar_customer.py
- The Customer Script
This script continuously prompts the user to order foobars. It:
- Generates a random number (between 1 and 10) of foobars to order.
- Waits for the user to press Enter.
- Sends a REST request to
foobar_orderbot.py
to place the order. - Loops infinitely because foobars are life.
|
|
foobar_orderbot.py
- The OrderBot
This script:
- Uses Flask to expose a REST endpoint.
- Generates a random name on startup using
faker
. - Returns a message confirming the order.
|
|
Running the Scripts
Start the order bot:
1
python foobar_orderbot.py
Run the customer script:
1
python foobar_customer.py
Observe the interactions:
OrderBot Output:
1 2
Alice-45678 - Ready for orders on port 8080! 127.0.0.1 - - [DATE] "GET /order?quantity=5 HTTP/1.1" 200 -
Customer Output:
1 2
Press Enter to order 5 foobars from the Foobar Orderbot... Alice-45678: 5 foobars successfully ordered!
Setting Up a Reverse Proxy
Linux (Using Nginx)
- Install Nginx:
1
sudo apt install nginx
- Configure the reverse proxy:Add the following:
1
sudo nano /etc/nginx/sites-available/foobar
1 2 3 4 5 6
server { listen 80; location / { proxy_pass http://localhost:8080; } }
- Enable the config and restart Nginx:
1 2
sudo ln -s /etc/nginx/sites-available/foobar /etc/nginx/sites-enabled/ sudo systemctl restart nginx
Windows (Using IIS)
- Install IIS and the Application Request Routing (ARR) module.
- Open IIS Manager → Select your server → Open Application Request Routing Cache.
- Click Server Proxy Settings → Enable Proxy.
- Add a Reverse Proxy rule:
- Condition:
localhost
- Rewrite URL:
http://localhost:8080
- Condition:
(if you get ill in the stomach at bloating down your dev box with IIS.. i understand.. later in the article ill show how to do this without IIS… )
Running Multiple OrderBots
Now, let’s start multiple foobar_orderbot.py
instances on different ports:
|
|
Update your Nginx config to distribute traffic:
|
|
Restart Nginx, and now requests get load-balanced across multiple bots!
Key Ideas so Far
Concept | Explanation |
---|---|
Reverse Proxy | Routes traffic to different backend servers |
Flask REST API | Handles foobar ordering |
Nginx Reverse Proxy | Load-balances requests across multiple order bots |
Multiple Backends | Running multiple instances of the order bot |
Setting Up a Reverse Proxy for Windows Development Without IIS
So far our Foobar Ordering System up and running using a reverse proxy.
We demonstrated how to set it up with Nginx on Linux and IIS on Windows.
But let’s be real—IIS setup on Windows dev machines can be a pain.
Many companies lock down development environments, preventing installation of things like IIS.
So lets explore alternative ways to set up a reverse proxy on Windows without using IIS.
We will look at Python-based solutions, and even see if Nginx on Windows is viable.
Quick Recap: What We Have so far
- We created two scripts:
foobar_customer.py
: A client that sends requests to order foobars.foobar_orderbot.py
: A Flask-based REST service that takes orders.
- We set up a reverse proxy to distribute requests across multiple order bots.
- We demonstrated Nginx on Linux and IIS on Windows as reverse proxies.
- We showed multiple instances of
foobar_orderbot.py
running behind the proxy.
But now, let’s go IIS-free and explore Python-based proxy solutions and Nginx on Windows.
Option 1: Setting Up a Reverse Proxy in Python
Luckily, Python has libraries that let us create a reverse proxy without breaking a sweat.
The http.server
module in Python provides basic proxying functionality, but we’ll use mitmproxy
, which is more powerful.
Step 1: Install mitmproxy
|
|
Step 2: Create reverse_proxy.py
|
|
Step 3: Run the Proxy
|
|
This will intercept all requests to localhost
and forward them to 127.0.0.1:8080
, where foobar_orderbot.py
is running.
Option 2: Running Nginx on Windows
Yes, you can run Nginx on Windows, and it works surprisingly well.
Step 1: Download Nginx for Windows
- Go to Nginx’s official website.
- Download the Windows
.zip
file. - Extract it to
C:\nginx
.
Step 2: Configure nginx.conf
Navigate to C:\nginx\conf\nginx.conf
and edit it like this:
|
|
Step 3: Start Nginx
Open Command Prompt and run:
|
|
Running Multiple OrderBots
Start multiple foobar_orderbot.py
instances on different ports:
|
|
With Nginx on Windows or our Python-based proxy, the foobar_customer.py
script will now load-balance across the different bots!
Example Output
OrderBot Instances
|
|
|
|
Customer Output
|
|
|
|
What did we do?
- We ditched IIS and found Python-based and Nginx-based reverse proxy alternatives for Windows.
- We saw that
mitmproxy
can handle proxying entirely in Python. - We learned that Nginx runs just fine on Windows, making it a solid choice.
- Our Foobar Orderbot system now runs on any Windows dev box without admin rights!
Key Ideas So Far
Concept | Explanation |
---|---|
Python Reverse Proxy | mitmproxy handles request forwarding |
Nginx on Windows | Lightweight and efficient reverse proxy solution |
Load Balancing | Distributes requests across multiple order bots |
No IIS Needed | Avoids admin restrictions on Windows dev boxes |
Running Foobar OrderBot in Docker and Kubernetes
Previously, we set up a reverse proxy to balance load across multiple instances of foobar_orderbot.py
. But let’s be real—manually launching instances isn’t ideal.
Today, we’re leveling up by deploying Foobar OrderBot in Docker and Kubernetes, making it dynamically scale based on demand. We’ll set up auto-scaling, load balancing, and even test it locally.
Docker vs. Kubernetes: What’s the Deal?
Technology | Role |
---|---|
Docker | Creates lightweight, portable containers that package our app with all its dependencies. |
Kubernetes | Manages multiple Docker containers, enabling scaling, auto-healing, and networking. |
In short:
- Docker gets our Foobar OrderBot running in isolated environments.
- Kubernetes manages many OrderBots efficiently.
Step 1: Containerizing Foobar OrderBot with Docker
We need a Dockerfile to package our Flask-based order bot into a container.
Dockerfile
|
|
Step 2: Build and Run the Docker Container
|
|
At this point, foobar_orderbot.py
runs inside a Docker container, just like it would on your local machine. But let’s take it one step further with Kubernetes!
Step 3: Deploying Foobar OrderBot on Kubernetes
Step 3.1: Define the Kubernetes Deployment
We’ll create a Kubernetes Deployment to manage multiple instances of foobar_orderbot.py
.
deployment.yaml
|
|
Step 3.2: Define the Kubernetes Service
To distribute traffic, we need a Kubernetes Service.
service.yaml
|
|
Step 3.3: Deploy to Kubernetes
|
|
Now, Kubernetes spins up 3 OrderBots, and traffic gets evenly distributed!
Step 4: Dynamic Scaling with Kubernetes
Imagine traffic spikes at lunchtime when everyone orders foobars. We need auto-scaling!
Step 4.1: Enable Auto-Scaling
|
|
This command tells Kubernetes:
- Keep at least 3 instances running.
- If CPU usage goes above 50%, scale up to 10 OrderBots.
Step 5: Reverse Proxy with Kubernetes and Nginx
To load-balance requests across all OrderBots, we use Nginx as a reverse proxy.
nginx-deployment.yaml
|
|
nginx-config.yaml
|
|
Deploy Nginx to Kubernetes
|
|
Now, all requests go through Nginx, which load-balances across all OrderBots! 🎉
Step 6: Testing Load Balancing Locally
If you’re on Minikube, enable the LoadBalancer:
|
|
Then, run the customer script against the Minikube IP:
|
|