{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/routing-urls-to-openfaas-functions","result":{"data":{"allGhostPost":{"edges":[{"node":{"title":"Routing URLs to OpenFaaS functions","html":"
OpenFaaS exposes functions under the path https://[gateway]/function/[function name]
. However, this might not always be suitable so you might want to route a different URL directly to a specific function.
One way to do so is running your own proxy that takes care of routing, but for some use-cases that may be a bit too much. What we will do in this post is create an Ingress
for each path that we want to be routed to a function. For example, say we have two functions:
sentimentanalysis
nodeinfo
and we want to route the following URLs:
https://sentiment.domain.tld/
→ function sentimentanalysis
https://sentiment.domain.tld/nodeinfo
→ function nodeinfo
For each one of those URLs, we will proxy the connections to the OpenFaaS gateway. OpenFaaS uses the first part of the URL path (/function/[function name]
) to identify the function, so we will also set the function name manually.
An Ingress config for the routes listed above would look like this:
sentiment.domain.tld, sentimentanalysis
---\napiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\n name: sentiment-domain-tld\n annotations:\n nginx.ingress.kubernetes.io/rewrite-target: \"/function/sentimentanalysis\"\n namespace: openfaas\nspec:\n rules:\n - host: sentiment.domain.tld\n http:\n paths:\n - path: /\n backend:\n serviceName: gateway\n servicePort: 8080
sentiment.domain.tld/nodeinfo, nodeinfo
---\napiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\n name: sentiment-domain-tld-nodeinfo\n annotations:\n nginx.ingress.kubernetes.io/rewrite-target: \"/function/nodeinfo\"\n namespace: openfaas\nspec:\n rules:\n - host: sentiment.domain.tld\n http:\n paths:\n - path: /nodeinfo\n backend:\n serviceName: gateway\n servicePort: 8080\n
The important bits to note are:
namespace: openfaas
we need access to OpenFaaS's gateway service, so our Ingresses need to be on its namespacenginx.ingress.kubernetes.io/rewrite-target: \"/function/nodeinfo\"
OpenFaaS needs to know what functions we are invoking—this is where we manually set the function name. The original path is preserved and appended.