Skip to content

Commit c205d44

Browse files
authored
Merge pull request #26 from krakend/sc-949/plugin-redis-inject-example
Plugins Redis injection example
2 parents 63d7d83 + f876fd7 commit c205d44

File tree

8 files changed

+437
-0
lines changed

8 files changed

+437
-0
lines changed

11.plugins-and-redis/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ARG KRAKEND_BUILDER
2+
ARG KRAKEND_VERSION
3+
4+
FROM krakend/builder-ee:$KRAKEND_BUILDER AS builder
5+
6+
WORKDIR /app
7+
COPY --chown=krakend:nogroup plugins /app
8+
9+
RUN cd counter-example-mw && make amd64
10+
11+
FROM krakend/krakend-ee:$KRAKEND_VERSION
12+
13+
WORKDIR /etc/krakend
14+
15+
COPY config/krakend .
16+
COPY LICENSE LICENSE
17+
COPY --from=builder --chown=krakend:nogroup /app/*.so /opt/krakend/plugins/
18+
19+
CMD [ "run", "-c", "/etc/krakend/krakend.json" ]

11.plugins-and-redis/README.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"$schema": "https://krakend.io/schema/krakend.json",
3+
"version": 3,
4+
"host": ["http://localhost:8080"],
5+
"echo_endpoint": true,
6+
"debug_endpoint": true,
7+
"plugin": {
8+
"folder": "/opt/krakend/plugins/",
9+
"pattern": ".so"
10+
},
11+
"extra_config": {
12+
"redis": {
13+
"connection_pools": [
14+
{
15+
"address": "redis:6379",
16+
"name": "local_instance",
17+
"db": 1
18+
}
19+
]
20+
}
21+
},
22+
"endpoints": [
23+
{
24+
"endpoint": "/",
25+
"backend": [
26+
{
27+
"url_pattern": "/__echo/"
28+
}
29+
],
30+
"extra_config": {
31+
"plugin/middleware": {
32+
"name": ["counter-example-mw"],
33+
"counter-example-mw": {
34+
"key_prefix": "foo-counter"
35+
}
36+
}
37+
}
38+
}
39+
]
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
krakend_ee:
3+
build:
4+
context: .
5+
args:
6+
- KRAKEND_BUILDER=2.13
7+
- KRAKEND_VERSION=2.13.0
8+
init: true
9+
ports:
10+
- "8080:8080"
11+
depends_on:
12+
- redis
13+
14+
redis:
15+
image: "redis:8"
16+
ports:
17+
- "6379:6379"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# KrakenD Version
2+
PLUGINAME := counter-example-mw
3+
CROSSCOMPILER := aarch64-linux-musl-gcc
4+
# Uncomment if using the linux-generic builder
5+
#CROSSCOMPILER := aarch64-linux-gnu-gcc
6+
7+
go.mod:
8+
go mod init counter-example-mw
9+
go mod tidy
10+
11+
amd64:
12+
GOARCH=amd64 \
13+
ARCH=amd64 \
14+
CGO_ENABLED=1 \
15+
go build -buildmode=plugin -o ../${PLUGINAME}-amd64.so .
16+
17+
arm64:
18+
CC=${CROSSCOMPILER} \
19+
GOHOSTARCH=amd64 \
20+
GOARCH=arm64 \
21+
ARCH=arm64 \
22+
CGO_ENABLED=1 \
23+
go build -buildmode=plugin -ldflags="-extld=${CROSSCOMPILER}" -o ../${PLUGINAME}-arm64.so .
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module counter-example-mw
2+
3+
go 1.25.7
4+
5+
require github.com/redis/go-redis/v9 v9.7.3
6+
7+
require (
8+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
9+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
10+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
2+
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
3+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
4+
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
5+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
6+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
8+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
9+
github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
10+
github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA=

0 commit comments

Comments
 (0)