|
| 1 | +# Kong Rate Limiting for /healthz Endpoint |
| 2 | + |
| 3 | +This guide explains how to add rate limiting to your `/healthz` endpoint using the Kong MCP Server tools we've built. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Based on your request log: |
| 8 | +```json |
| 9 | +{ |
| 10 | + "@timestamp": ["2025-08-24T19:10:03.000Z"], |
| 11 | + "request_method": ["GET"], |
| 12 | + "request_uri": ["/healthz"], |
| 13 | + "request_url": ["http://localhost/healthz"], |
| 14 | + "client_ip": ["127.0.0.1"], |
| 15 | + "request_user_agent": ["PostmanRuntime/7.45.0"], |
| 16 | + "response_status": [200] |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +We've created comprehensive rate limiting tools and scripts to protect your `/healthz` endpoint from abuse while allowing normal health check traffic. |
| 21 | + |
| 22 | +## What We've Built |
| 23 | + |
| 24 | +### 1. Core Rate Limiting Tools (`src/kong_mcp_server/tools/kong_rate_limiting.py`) |
| 25 | + |
| 26 | +Complete CRUD operations for Kong's rate limiting plugins: |
| 27 | + |
| 28 | +- **Basic Rate Limiting**: Simple time-based limits (minute/hour/day) |
| 29 | +- **Advanced Rate Limiting**: Complex sliding window limits with Redis support |
| 30 | +- **All Scopes**: Global, service, route, and consumer-scoped rate limiting |
| 31 | +- **Full Management**: Create, read, update, delete operations |
| 32 | + |
| 33 | +**Available Functions:** |
| 34 | +- `create_rate_limiting_plugin()` - Create basic rate limiting |
| 35 | +- `get_rate_limiting_plugins()` - List rate limiting plugins |
| 36 | +- `update_rate_limiting_plugin()` - Update existing rate limiting |
| 37 | +- `delete_rate_limiting_plugin()` - Remove rate limiting |
| 38 | +- `create_rate_limiting_advanced_plugin()` - Create advanced rate limiting |
| 39 | +- `get_rate_limiting_advanced_plugins()` - List advanced plugins |
| 40 | +- `update_rate_limiting_advanced_plugin()` - Update advanced plugins |
| 41 | +- `delete_rate_limiting_advanced_plugin()` - Remove advanced plugins |
| 42 | +- `get_plugin()` - Get specific plugin by ID |
| 43 | +- `get_plugins()` - List all plugins with filters |
| 44 | + |
| 45 | +### 2. Setup Scripts |
| 46 | + |
| 47 | +#### `setup_healthz_complete.py` - Complete Setup (Recommended) |
| 48 | +Interactive script that sets up everything: |
| 49 | +- Creates health check service |
| 50 | +- Creates `/healthz` route |
| 51 | +- Adds rate limiting with user-selectable levels |
| 52 | +- Provides testing instructions |
| 53 | + |
| 54 | +**Usage:** |
| 55 | +```bash |
| 56 | +python3 setup_healthz_complete.py |
| 57 | +``` |
| 58 | + |
| 59 | +#### `add_healthz_rate_limit.py` - Quick Rate Limiting |
| 60 | +Adds rate limiting to existing `/healthz` route: |
| 61 | +```bash |
| 62 | +python3 add_healthz_rate_limit.py |
| 63 | +``` |
| 64 | + |
| 65 | +#### `example_healthz_rate_limiting.py` - Advanced Example |
| 66 | +Comprehensive example with monitoring and management features. |
| 67 | + |
| 68 | +## Quick Start |
| 69 | + |
| 70 | +### Option 1: Complete Setup (Recommended) |
| 71 | + |
| 72 | +1. **Run the complete setup script:** |
| 73 | + ```bash |
| 74 | + python3 setup_healthz_complete.py |
| 75 | + ``` |
| 76 | + |
| 77 | +2. **Follow the prompts:** |
| 78 | + - Enter your backend URL (default: `http://localhost:8080`) |
| 79 | + - Choose rate limiting level: |
| 80 | + - **Generous**: 300/min, 18000/hour - For frequent health checks |
| 81 | + - **Standard**: 120/min, 7200/hour - Balanced protection (recommended) |
| 82 | + - **Strict**: 60/min, 3600/hour - Strong protection |
| 83 | + - **Custom**: Enter your own limits |
| 84 | + |
| 85 | +3. **Test the setup:** |
| 86 | + ```bash |
| 87 | + curl -i http://localhost/healthz |
| 88 | + ``` |
| 89 | + |
| 90 | +### Option 2: Manual Setup Using MCP Tools |
| 91 | + |
| 92 | +If you prefer to use the tools directly: |
| 93 | + |
| 94 | +```python |
| 95 | +import asyncio |
| 96 | +from src.kong_mcp_server.tools.kong_rate_limiting import create_rate_limiting_plugin |
| 97 | + |
| 98 | +async def setup_rate_limiting(): |
| 99 | + # Add rate limiting to existing route |
| 100 | + plugin = await create_rate_limiting_plugin( |
| 101 | + route_id="your-healthz-route-id", |
| 102 | + minute=120, # 120 requests per minute |
| 103 | + hour=7200, # 7200 requests per hour |
| 104 | + limit_by="ip", |
| 105 | + policy="local", |
| 106 | + fault_tolerant=True, |
| 107 | + tags=["healthz", "monitoring"] |
| 108 | + ) |
| 109 | + print(f"Created rate limiting: {plugin['id']}") |
| 110 | + |
| 111 | +asyncio.run(setup_rate_limiting()) |
| 112 | +``` |
| 113 | + |
| 114 | +## Rate Limiting Configuration |
| 115 | + |
| 116 | +### Recommended Settings for /healthz |
| 117 | + |
| 118 | +**Standard Configuration (Recommended):** |
| 119 | +- **120 requests/minute** (2 per second) |
| 120 | +- **7,200 requests/hour** |
| 121 | +- **172,800 requests/day** |
| 122 | +- **Limited by IP address** |
| 123 | +- **Fault tolerant** (continues serving if rate limiting fails) |
| 124 | +- **Headers visible** (shows rate limiting info to clients) |
| 125 | + |
| 126 | +**Why these limits?** |
| 127 | +- Health checks are typically automated and frequent |
| 128 | +- 2 requests per second allows for aggressive monitoring |
| 129 | +- IP-based limiting prevents single sources from overwhelming |
| 130 | +- Fault tolerance ensures health checks don't break |
| 131 | + |
| 132 | +### Rate Limiting Headers |
| 133 | + |
| 134 | +When rate limiting is active, responses include headers: |
| 135 | +``` |
| 136 | +X-RateLimit-Limit-Minute: 120 |
| 137 | +X-RateLimit-Remaining-Minute: 119 |
| 138 | +X-RateLimit-Reset: 1629876543 |
| 139 | +``` |
| 140 | + |
| 141 | +### Rate Limiting Responses |
| 142 | + |
| 143 | +When limits are exceeded: |
| 144 | +``` |
| 145 | +HTTP/1.1 429 Too Many Requests |
| 146 | +X-RateLimit-Limit-Minute: 120 |
| 147 | +X-RateLimit-Remaining-Minute: 0 |
| 148 | +X-RateLimit-Reset: 1629876603 |
| 149 | +
|
| 150 | +{ |
| 151 | + "message": "API rate limit exceeded" |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +## Testing Your Setup |
| 156 | + |
| 157 | +### 1. Basic Test |
| 158 | +```bash |
| 159 | +curl -i http://localhost/healthz |
| 160 | +``` |
| 161 | +Look for `X-RateLimit-*` headers in the response. |
| 162 | + |
| 163 | +### 2. Rate Limiting Test |
| 164 | +```bash |
| 165 | +# Send 10 rapid requests |
| 166 | +for i in {1..10}; do |
| 167 | + curl -s -o /dev/null -w '%{http_code}\n' http://localhost/healthz |
| 168 | +done |
| 169 | +``` |
| 170 | + |
| 171 | +### 3. Monitor Rate Limiting |
| 172 | +```bash |
| 173 | +# Check plugin status |
| 174 | +curl http://localhost:8001/plugins | jq '.data[] | select(.name == "rate-limiting")' |
| 175 | +``` |
| 176 | + |
| 177 | +## Advanced Configuration |
| 178 | + |
| 179 | +### Redis-Based Rate Limiting |
| 180 | + |
| 181 | +For distributed Kong deployments, use Redis: |
| 182 | + |
| 183 | +```python |
| 184 | +await create_rate_limiting_advanced_plugin( |
| 185 | + route_id="your-route-id", |
| 186 | + limit=[{"minute": 120}, {"hour": 7200}], |
| 187 | + window_size=[60, 3600], |
| 188 | + strategy="redis", |
| 189 | + redis_host="your-redis-host", |
| 190 | + redis_port=6379, |
| 191 | + redis_password="your-password" |
| 192 | +) |
| 193 | +``` |
| 194 | + |
| 195 | +### Consumer-Based Rate Limiting |
| 196 | + |
| 197 | +For authenticated health checks: |
| 198 | + |
| 199 | +```python |
| 200 | +await create_rate_limiting_plugin( |
| 201 | + route_id="your-route-id", |
| 202 | + minute=300, # Higher limits for authenticated consumers |
| 203 | + limit_by="consumer", |
| 204 | + policy="local" |
| 205 | +) |
| 206 | +``` |
| 207 | + |
| 208 | +## Monitoring and Management |
| 209 | + |
| 210 | +### Check Current Rate Limiting |
| 211 | +```python |
| 212 | +from src.kong_mcp_server.tools.kong_rate_limiting import get_rate_limiting_plugins |
| 213 | + |
| 214 | +async def check_rate_limiting(): |
| 215 | + plugins = await get_rate_limiting_plugins() |
| 216 | + for plugin in plugins: |
| 217 | + if "healthz" in plugin.get("tags", []): |
| 218 | + print(f"Plugin: {plugin['id']}") |
| 219 | + print(f"Config: {plugin['config']}") |
| 220 | +``` |
| 221 | + |
| 222 | +### Update Rate Limiting |
| 223 | +```python |
| 224 | +from src.kong_mcp_server.tools.kong_rate_limiting import update_rate_limiting_plugin |
| 225 | + |
| 226 | +async def update_limits(): |
| 227 | + await update_rate_limiting_plugin( |
| 228 | + plugin_id="your-plugin-id", |
| 229 | + minute=200, # Increase to 200/minute |
| 230 | + hour=12000 # Increase to 12000/hour |
| 231 | + ) |
| 232 | +``` |
| 233 | + |
| 234 | +### Remove Rate Limiting |
| 235 | +```python |
| 236 | +from src.kong_mcp_server.tools.kong_rate_limiting import delete_rate_limiting_plugin |
| 237 | + |
| 238 | +async def remove_rate_limiting(): |
| 239 | + await delete_rate_limiting_plugin("your-plugin-id") |
| 240 | +``` |
| 241 | + |
| 242 | +## Troubleshooting |
| 243 | + |
| 244 | +### Common Issues |
| 245 | + |
| 246 | +1. **"No /healthz route found"** |
| 247 | + - Run `setup_healthz_complete.py` to create the route |
| 248 | + - Or check existing routes: `curl http://localhost:8001/routes` |
| 249 | + |
| 250 | +2. **"Kong not accessible"** |
| 251 | + - Ensure Kong is running: `curl http://localhost:8001/status` |
| 252 | + - Check Kong Admin API port (default: 8001) |
| 253 | + |
| 254 | +3. **Rate limiting not working** |
| 255 | + - Check plugin is enabled: `curl http://localhost:8001/plugins/{plugin-id}` |
| 256 | + - Verify route association |
| 257 | + - Check Kong error logs |
| 258 | + |
| 259 | +### Verification Commands |
| 260 | + |
| 261 | +```bash |
| 262 | +# Check Kong status |
| 263 | +curl http://localhost:8001/status |
| 264 | + |
| 265 | +# List all services |
| 266 | +curl http://localhost:8001/services |
| 267 | + |
| 268 | +# List all routes |
| 269 | +curl http://localhost:8001/routes |
| 270 | + |
| 271 | +# List all plugins |
| 272 | +curl http://localhost:8001/plugins |
| 273 | + |
| 274 | +# Check specific plugin |
| 275 | +curl http://localhost:8001/plugins/{plugin-id} |
| 276 | +``` |
| 277 | + |
| 278 | +## Integration with Monitoring |
| 279 | + |
| 280 | +### Prometheus Metrics |
| 281 | + |
| 282 | +Kong can export rate limiting metrics to Prometheus: |
| 283 | +- `kong_rate_limiting_requests_total` |
| 284 | +- `kong_rate_limiting_requests_exceeded_total` |
| 285 | + |
| 286 | +### Log Analysis |
| 287 | + |
| 288 | +Rate limiting events appear in Kong logs: |
| 289 | +```json |
| 290 | +{ |
| 291 | + "message": "rate limit exceeded", |
| 292 | + "client_ip": "127.0.0.1", |
| 293 | + "route": {"id": "healthz-route"}, |
| 294 | + "plugin": {"id": "rate-limiting-plugin"} |
| 295 | +} |
| 296 | +``` |
| 297 | + |
| 298 | +## Best Practices |
| 299 | + |
| 300 | +1. **Start Conservative**: Begin with generous limits and tighten as needed |
| 301 | +2. **Monitor Usage**: Track actual health check patterns |
| 302 | +3. **Use Fault Tolerance**: Enable for critical health checks |
| 303 | +4. **Tag Everything**: Use consistent tags for easy management |
| 304 | +5. **Test Thoroughly**: Verify rate limiting works as expected |
| 305 | +6. **Document Limits**: Keep track of configured limits |
| 306 | +7. **Regular Review**: Periodically review and adjust limits |
| 307 | + |
| 308 | +## Summary |
| 309 | + |
| 310 | +You now have comprehensive rate limiting for your `/healthz` endpoint that: |
| 311 | + |
| 312 | +✅ **Protects against abuse** - Prevents overwhelming your health check endpoint |
| 313 | +✅ **Allows normal traffic** - Generous limits for legitimate health checks |
| 314 | +✅ **Provides visibility** - Rate limiting headers show current status |
| 315 | +✅ **Fault tolerant** - Won't break health checks if rate limiting fails |
| 316 | +✅ **Easy to manage** - Full CRUD operations via MCP tools |
| 317 | +✅ **Scalable** - Supports Redis for distributed deployments |
| 318 | + |
| 319 | +Your `/healthz` endpoint is now production-ready with enterprise-grade rate limiting protection! |
0 commit comments