|
| 1 | +# Injecting Redis into KrakenD plugins |
| 2 | + |
| 3 | +This example demonstrates how to build and use custom KrakenD Enterprise plugins with Redis integration. It includes a simple middleware plugin that increments a counter in Redis for each API request. |
| 4 | + |
| 5 | +## What's Inside |
| 6 | + |
| 7 | +- **KrakenD EE**: API Gateway with plugin support |
| 8 | +- **Redis**: In-memory data store for the counter |
| 9 | +- **Custom Plugin**: `counter-example-mw` - A Go plugin that increments a Redis counter on each request |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- Docker |
| 14 | +- Docker Compose |
| 15 | + |
| 16 | +## Project Structure |
| 17 | + |
| 18 | +``` |
| 19 | +. |
| 20 | +├── docker-compose.yml # Service orchestration |
| 21 | +├── Dockerfile # Multi-stage build for KrakenD with plugin |
| 22 | +├── config/ |
| 23 | +│ └── krakend/ |
| 24 | +│ └── krakend.json # KrakenD configuration |
| 25 | +└── plugins/ |
| 26 | + └── counter-example-mw/ # Custom middleware plugin |
| 27 | + ├── middleware.go # Plugin implementation |
| 28 | + ├── Makefile # Build configuration |
| 29 | + ├── go.mod |
| 30 | + └── go.sum |
| 31 | +``` |
| 32 | + |
| 33 | +## Quick Start |
| 34 | + |
| 35 | +### 1. Launch the Stack |
| 36 | + |
| 37 | +```bash |
| 38 | +docker compose up --build |
| 39 | +``` |
| 40 | + |
| 41 | +This will: |
| 42 | +- Build the custom plugin as a shared object (.so file) |
| 43 | +- Create a KrakenD EE container with the plugin loaded |
| 44 | +- Start a Redis instance |
| 45 | +- Expose KrakenD on port 8080 and Redis on port 6379 |
| 46 | + |
| 47 | +### 2. Test the API |
| 48 | + |
| 49 | +Once the stack is running, test the endpoint: |
| 50 | + |
| 51 | +```bash |
| 52 | +curl http://localhost:8080/ |
| 53 | +``` |
| 54 | + |
| 55 | +Each request will: |
| 56 | +- Pass through the custom middleware plugin |
| 57 | +- Increment a counter in Redis with key `foo-counter:some-id` |
| 58 | +- Return the response from the echo endpoint |
| 59 | + |
| 60 | +### 3. Check the Redis Counter |
| 61 | + |
| 62 | +Connect to Redis and check the counter value: |
| 63 | + |
| 64 | +```bash |
| 65 | +# Connect to Redis CLI |
| 66 | +docker compose exec redis redis-cli |
| 67 | + |
| 68 | +# Select database 1 (as configured in krakend.json) |
| 69 | +SELECT 1 |
| 70 | + |
| 71 | +# Get the counter value |
| 72 | +GET foo-counter:some-id |
| 73 | + |
| 74 | +# Exit Redis CLI |
| 75 | +EXIT |
| 76 | +``` |
| 77 | + |
| 78 | +The counter should increment with each request to the API. |
| 79 | + |
| 80 | +## Configuration Details |
| 81 | + |
| 82 | +### KrakenD Configuration |
| 83 | + |
| 84 | +The `config/krakend/krakend.json` file configures: |
| 85 | + |
| 86 | +- **Redis Connection**: Points to the Redis service on port 6379, database 1 |
| 87 | +- **Plugin Loading**: Loads `.so` files from `/opt/krakend/plugins/` |
| 88 | +- **Middleware**: Applies `counter-example-mw` plugin to the `/` endpoint |
| 89 | +- **Debug Features**: Enables `echo_endpoint` and `debug_endpoint` for testing |
| 90 | + |
| 91 | +### Plugin Configuration |
| 92 | + |
| 93 | +The plugin accepts configuration in the endpoint's `extra_config`: |
| 94 | + |
| 95 | +```json |
| 96 | +"plugin/middleware": { |
| 97 | + "name": ["counter-example-mw"], |
| 98 | + "counter-example-mw": { |
| 99 | + "key_prefix": "foo-counter" |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +The `key_prefix` is used to create Redis keys in the format: `{key_prefix}:some-id` |
| 105 | + |
| 106 | +## How the Plugin Works |
| 107 | + |
| 108 | +The `counter-example-mw` plugin: |
| 109 | + |
| 110 | +1. Registers itself as a KrakenD middleware |
| 111 | +2. Gets access to the Redis client configured as "local_instance" |
| 112 | +3. On each request: |
| 113 | + - Passes the request to the next middleware/backend |
| 114 | + - Increments a counter in Redis using the configured key prefix |
| 115 | + - Returns the response |
| 116 | + |
| 117 | +## Modifying the Plugin |
| 118 | + |
| 119 | +### Change the Plugin Logic |
| 120 | + |
| 121 | +1. Edit `plugins/counter-example-mw/middleware.go` |
| 122 | +2. Rebuild the stack: |
| 123 | + |
| 124 | +```bash |
| 125 | +docker compose up --build |
| 126 | +``` |
| 127 | + |
| 128 | +## Stopping the Stack |
| 129 | + |
| 130 | +```bash |
| 131 | +docker compose down |
| 132 | +``` |
| 133 | + |
| 134 | +To also remove volumes (including Redis data): |
| 135 | + |
| 136 | +```bash |
| 137 | +docker compose down -v |
| 138 | +``` |
| 139 | + |
| 140 | +## Troubleshooting |
| 141 | + |
| 142 | +### Plugin Not Loading |
| 143 | + |
| 144 | +Check the KrakenD logs for plugin loading errors: |
| 145 | + |
| 146 | +```bash |
| 147 | +docker compose logs krakend_ee |
| 148 | +``` |
| 149 | + |
| 150 | +Look for messages like `[PLUGIN: counter-example-mw]` to verify the plugin loaded successfully. |
| 151 | + |
| 152 | +### Redis Connection Issues |
| 153 | + |
| 154 | +Verify Redis is running and accessible: |
| 155 | + |
| 156 | +```bash |
| 157 | +docker compose ps redis |
| 158 | +docker compose exec redis redis-cli PING |
| 159 | +``` |
| 160 | + |
| 161 | +Should return `PONG` if Redis is working correctly. |
| 162 | + |
| 163 | +### Port Conflicts |
| 164 | + |
| 165 | +If ports 8080 or 6379 are already in use, modify the port mappings in `docker-compose.yml`: |
| 166 | + |
| 167 | +```yaml |
| 168 | +ports: |
| 169 | + - "8081:8080" # Change external port to 8081 |
| 170 | +``` |
| 171 | +
|
| 172 | +## Additional Resources |
| 173 | +
|
| 174 | +- [KrakenD Plugin Documentation](https://www.krakend.io/docs/extending/) |
| 175 | +- [Injecting Redis in plugins](https://www.krakend.io/docs/enterprise/extending/injecting-redis-in-plugins/) |
| 176 | +- [KrakenD Redis Service configuration](https://www.krakend.io/docs/enterprise/service-settings/redis-connection-pools/) |
| 177 | +- [Go Redis Client](https://github.com/redis/go-redis) |
0 commit comments