Assuming there are two source IPs, namely 192.168.1.1 and 192.168.2.2, both simultaneously sending requests to the same KrakenD service and JSON file (as shown below), how can one redirect to different backend hosts based on different source IPs when the URL of the "endpoint" is the same?
{
"version": 3,
"endpoints": [
{
"endpoint": "/api",
"method": "POST",
"backend": [
{
"host": "{{ if eq .Request.RemoteAddr \"192.168.1.1\" }}backend1{{ else }}backend2{{ end }}",
"url_pattern": "/api",
"encoding": "json",
"timeout": "3000ms"
}
]
}
]
}
Thanks
While you cannot use templating expressions like that directly inside the
hostfield, you could use Lua to dynamically change the hostname in a similar way that is described in this article: https://www.krakend.io/blog/canary-releases/The idea is to have something like this in your endpoint configuration:
and the following script in your
alternativeBackend.luascript:Instead of choosing it randomly, like in this example, you could implement your own custom logic based on the client IP.
Hope it helps.