{"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:

  1. sentimentanalysis
  2. nodeinfo

and we want to route the following URLs:

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:

","published_at":"2018-12-10T09:01:11.000+02:00","slug":"routing-urls-to-openfaas-functions","tags":[],"plaintext":"OpenFaaS exposes functions under the path https://[gateway]/function/[function\nname]. However, this might not always be suitable so you might want to route a\ndifferent URL directly to a specific function.\n\nOne way to do so is running your own proxy that takes care of routing, but for\nsome use-cases that may be a bit too much. What we will do in this post is\ncreate an Ingress for each path that we want to be routed to a function. For\nexample, say we have two functions:\n\n 1. sentimentanalysis \n 2. nodeinfo\n\nand we want to route the following URLs:\n\n * https://sentiment.domain.tld/ → function sentimentanalysis\n * https://sentiment.domain.tld/nodeinfo → function nodeinfo\n\nFor each one of those URLs,  we will proxy the connections to the OpenFaaS\ngateway. OpenFaaS uses the first part of the URL path (/function/[function name]\n) to identify the function, so we will also set the function name manually. \n\nAn Ingress config for the routes listed above would look like this:\n\nsentiment.domain.tld, sentimentanalysis\n\n---\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\n\nsentiment.domain.tld/nodeinfo, nodeinfo\n\n---\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\n\nThe important bits to note are:\n\n * namespace: openfaas we need access to OpenFaaS's gateway service, so our\n Ingresses need to be on its namespace\n * nginx.ingress.kubernetes.io/rewrite-target: \"/function/nodeinfo\" OpenFaaS\n needs to know what functions we are invoking—this is where we manually set\n the function name. The original path is preserved and appended.","meta_description":null}}]}},"pageContext":{"slug":"routing-urls-to-openfaas-functions","prev":"preventing-loading-progress-bar-flashes-with-vue","next":"how-to-use-a-remote-docker-server-to-speed-up-your-workflow-written-version"}},"staticQueryHashes":["3649515864"]}