讓外部可以使用 HTTP 存取 Function

如何建立 HTTP Trigger

路徑

使用 --path=<path>=<path-type>=<fn-name>=<port-number> 來設定要比對的 URL 路徑以及將流量送往 Function 埠號 (建立 HTTP Trigger 之前必須設定 Function 開放埠號

路徑匹配

以下指令將匹配路徑 / 的流量送往 Function hello,並且設定 Ingress Classnginx

  # Demo only, do NOT create an http trigger with root path like this in production.
syntixi httptrigger create --name hello --path="/"=Prefix=hello=80 --ingress-class=nginx
  

存取 Ingress Controller 的 LoadBalancer IP 並測試 Function 是否正常運作

  INGRESS_SERVICE_LOAD_BALANCER_IP=$(kubectl -n ingress-nginx get svc ingress-nginx-controller -o jsonpath='{...ip}')
curl http://${INGRESS_SERVICE_LOAD_BALANCER_IP}/
  

路徑類型

每個路徑都需要有對應的路徑類型來做匹配。

  syntixi httptrigger create --name hello --path="/foo/bar"=Exact=hello=80
  

以下為二種支援的路徑類型:

  • Exact: 精確的匹配並且會區分大小寫
  • Prefix: 根據 URL 路徑前綴(以 / 分隔)進行匹配,匹配會區分大小寫並且對路徑中的元素逐個比對完成
Path TypePath(s)Request path(s)Matches?
Exact/foo/fooYes
Exact/foo/foo/No
Prefix/(all paths)Yes
Prefix/foo/foo, /foo/Yes
Prefix/foo//foo, /foo/Yes
Prefix/foo//fooYes, ignores trailing slash
Prefix/foo//foo/Yes, matches trailing slash
Prefix/aaa/bbb/aaa/bbb/cccYes, matches subpath
Prefix/aaa/bbb/aaa/bbbxyzNo, does not match string prefix
Mixed/foo (Prefix), /foo (Exact)/fooYes, prefers Exact

Host

我們強烈建議使用 HTTP Trigger 時要指定,這樣可以避免多個使用相同 URL 路徑的 Function 時發生衝突。

使用 --host 參數來指定 HTTP Trigger 目的主機或是域名

  syntixi ht create --name hello --path="/"=Exact=hello=80 --host example.com
curl -H 'Host: example.com' http://${INGRESS_SERVICE_LOAD_BALANCER_IP}/
  

TLS

使用 --tls 參數指定要使用的 TLS Config,這邊的範例假設您已使用 tls-cfg 為名建立 TLS config

  syntixi ht create --name hello --path="/"=Exact=hello=80  --host example.com --tls tls-cfg
curl --insecure -H 'Host: example.com' http://${INGRESS_SERVICE_LOAD_BALANCER_IP}/
  

支援的 Ingress Controller

Syntixi 使用 Kubernetes ingress 來實現終止 TLS 以及路徑匹配,未來將支援 Gateway API 以提供更多功能。

Was this page helpful?