Serverless Functions
Serverless functions 由两个插件组成:serverless-pre-function 和 serverless-post-function。这些插件允许在函数挂钩的执行阶段的开始和结束时执行用户 定义的逻辑。
编写函数提示
Serverless 插件中只允许使用 Lua 函数,不允许使用其他 Lua 代码。
例如,匿名函数是合法的:
return function()
ngx.log(ngx.ERR, 'one')
end
闭包也是合法的:
local count = 1
return function()
count = count + 1
ngx.say(count)
end
但除函数以外的代码是非法的 :
local count = 1
ngx.say(count)
示例
以下示例演示了如何在不同场景下配置 serverless-pre-function 和 serverless-post-function 插件。
在阶段前后记录信息
以下示例演示了如何配置 serverless 插件以执行自定义逻辑,在 rewrite 阶段前后将信息记录到错误日志中。
创建一个路由如下:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H 'X-API-KEY: ${ADMIN_API_KEY}' \
-d '{
"id": "serverless-pre-route",
"uri": "/anything",
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions" : [
"return function()
ngx.log(ngx.ERR, \"serverless pre function\");
end"
]
},
"serverless-post-function": {
"phase": "rewrite",
"functions" : [
"return function(conf, ctx)
ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path);
end"
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- name: serverless-pre-route
uris:
- /anything
plugins:
serverless-pre-function:
phase: rewrite
functions:
- |
return function()
ngx.log(ngx.ERR, "serverless pre function")
end
serverless-post-function:
phase: rewrite
functions:
- |
return function(conf, ctx)
ngx.log(ngx.ERR, "match uri ", ctx.curr_req_matched and ctx.curr_req_matched._path)
end
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: serverless-functions-plugin-config
spec:
plugins:
- name: serverless-pre-function
config:
phase: rewrite
functions:
- |
return function()
ngx.log(ngx.ERR, "serverless pre function")
end
- name: serverless-post-function
config:
phase: rewrite
functions:
- |
return function(conf, ctx)
ngx.log(ngx.ERR, "match uri ", ctx.curr_req_matched and ctx.curr_req_matched._path)
end
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: serverless-pre-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: serverless-functions-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: serverless-pre-route
spec:
ingressClassName: apisix
http:
- name: serverless-pre-route
match:
paths:
- /anything
upstreams:
- name: httpbin-external-domain
plugins:
- name: serverless-pre-function
config:
phase: rewrite
functions:
- |
return function()
ngx.log(ngx.ERR, "serverless pre function")
end
- name: serverless-post-function
config:
phase: rewrite
functions:
- |
return function(conf, ctx)
ngx.log(ngx.ERR, "match uri ", ctx.curr_req_matched and ctx.curr_req_matched._path)
end
应用配置:
kubectl apply -f serverless-functions-ic.yaml
❶ 将 serverless pre-function 逻辑挂钩到 rewrite 阶段。
❷ 定义一个 Lua 函数,在错误日志中记录一条 serverless pre function 消息。
❸ 将 serverless post-function 逻辑挂钩到 rewrite 阶段。
❹ 定义一个 Lua 函数,在错误日志中记录匹配的 URI。conf 和 ctx 可以像其他插件一样作为前两个参数传递,其中 conf 是插件配置,ctx 是请求上下文。
发送请求到路由:
curl -i "http://127.0.0.1:9080/anything"
你应该收到 HTTP/1.1 200 OK 响应,并在错误日志中看到以下条目:
2024/05/09 15:07:09 [error] 51#51: *3963 [lua] [string "return function() ngx.log(ngx.ERR, "serverles..."]:1: func(): serverless pre function, client: 172.21.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
2024/05/09 15:16:58 [error] 50#50: *9343 [lua] [string "return function(conf, ctx) ngx.log(ngx.ERR, "..."]:1: func(): match uri /anything, client: 172.21.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
第一个条目由 pre-function 添加,第二个条目由 post-function 添加。
注册自定义变量
以下示例演示了如何使用 serverless 插件注册自定义内置变量,并在日志中使用新创建的变量。
由于 Ingress Controller 不支持配置路由标签,因此无法使用它完成此示例。
启动一个示例 rsyslog 服务器:
docker run -d -p 514:514 --name example-rsyslog-server rsyslog/syslog_appliance_alpine
创建一个带有 serverless 函数的服务以注册自定义变量 a6_route_labels,启用日志插件以稍后记录自定义变量,并配置上游:
- Admin API
- ADC
curl "http://127.0.0.1:9180/apisix/admin/services" -X PUT \
-H 'X-API-KEY: ${ADMIN_API_KEY}' \
-d '{
"id":"srv_custom_var",
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions": [
"return function()
local core = require \"apisix.core\"
core.ctx.register_var(\"a6_route_labels\", function(ctx)
local route = ctx.matched_route and ctx.matched_route.value
if route and route.labels then
return route.labels
end
return nil
end);
end"
]
},
"syslog": {
"host" : "172.0.0.1",
"port" : 514,
"flush_limit" : 1
}
},
"upstream": {
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: srv-custom-var
plugins:
serverless-pre-function:
phase: rewrite
functions:
- |
return function()
local core = require("apisix.core")
core.ctx.register_var("a6_route_labels", function(ctx)
local route = ctx.matched_route and ctx.matched_route.value
if route and route.labels then
return route.labels
end
return nil
end)
end
syslog:
host: 172.0.0.1
port: 514
flush_limit: 1
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
❶ functions: 注册一个自定义变量 a6_route_labels 并从匹配路由的 labels 属性中获取变量值。
❷ host 和 port: 替换为你的 syslog 服务器的地址。
❸ flush_limit: 设置为 1 以立即将日志推送到 syslog 服务器。
接下来,通过配置插件元数据,使用新变量更新所有 syslog 实例的日志格式:
- Admin API
- ADC
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/syslog" -X PUT \
-H 'X-API-KEY: ${ADMIN_API_KEY}' \
-d '{
"log_format": {
"host": "$host",
"client_ip": "$remote_addr",
"labels": "$a6_route_labels"
}
}'
plugin_metadata:
syslog:
log_format:
host: "$host"
client_ip: "$remote_addr"
labels: "$a6_route_labels"
将配置同步到网关:
adc sync -f adc.yaml
❶ $host 和 $remote_addr:NGINX 变量。
❷ $a6_route_labels: 自定义变量。
最后,在服务中创建一个路由:
- Admin API
- ADC
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H 'X-API-KEY: ${ADMIN_API_KEY}' \
-d '{
"id":"route_custom_var",
"uri":"/get",
"service_id": "srv_custom_var",
"labels": {
"key": "test_a6_route_labels"
}
}'
# 其他配置
services:
- name: srv-custom-var
routes:
- name: route-custom-var
uris:
- /get
labels:
key: test_a6_route_labels
将配置同步到网关:
adc sync -f adc.yaml
❶ service_id: 对应之前创建的服务。
❷ labels: 要与自定义变量一起记录的路由信息。
要验证变量注册,请发送请求到路由:
curl "http://127.0.0.1:9080/get"
你应该在 syslog 服务器中看到类似于以下的日志条目:
{
"host":"127.0.0.1",
"route_id":"route_custom_var",
"client_ip":"172.19.0.1",
"labels":{
"key":"test_a6_route_labels"
},
"service_id":"srv_custom_var"
}
这验证了自定义变量已注册,并且它成功记录了路由中的 labels 信息。