文档简介:
功能概述
边缘节点开放OpenAPI,开发者可以调用OpenAPI获取边缘节点信息、边缘应用信息等。
目前边缘OpenAPI不支持更新操作,只支持查询操作。
边缘节点OpenAPI清单:
-
节点影子:
- 宿主机调用:https://localhost:30050/node/properties
- K8s服务间调用:https://baetyl-core.baetyl-edge-system/node/properties
-
节点状态:
- 宿主机调用:https://localhost:30050/node/stats
- K8s服务间调用:https://baetyl-core.baetyl-edge-system/node/stats
说明:节点信息相关的api通过baetyl-core应用暴露,容器内端口443,宿主机端口30050。
节点影子
节点影子可以看做边缘设备数据在云端的映射。通过节点影子,可以利用端云同步通道,将云端节点数据或属性同步至边缘设设备。边缘设备上的应用可以通过相关接口获取来自云端的数据,同时也可以利用接口对数据进行更新,然后同步至云端。简而言之,节点影子可以作为云边数据双向同步的一种方式。
同步的数据格式为键值对Json,且数据类型均为字符串,数据内容不限。例如当需要同步配置文件,可以指定属性名称为配置文件名,期望值为文件内容。设备端即可以接收来自云端同步的配置文件,使用配置文件对应用进行热更新。具体使用方式如下:
- 在云端节点页面点击节点影子标签,显示该节点现有属性为空,
-
边缘侧获取云端影子desire信息,主要有两种方式:
- 在边缘侧订阅baetyl-broker的$baetyl/node/props主题消息,获取云端影子desire信息
- 在边缘侧调用节点OpenAPI,获取云端影子desire信息
- 边缘侧修改report信息:在边缘侧获取到云端desire消息以后,边缘侧需要做出响应,调用OpenAPI反馈report值。只有到边缘report的值与云端desire的值相同时,云端才停止想边缘侧发送影子desire消息。否则在边缘侧可以一直订阅到云端desire值。
接收节点影子数据增量更新消息
为模拟应用订阅系统 MQTT broker,先将系统broker配置进行调整。注意,这里仅仅是为了便于操作,实际应用订阅主题推荐使用TLS连接系统 MQTT broker,下文会有介绍。
点击编辑,添加属性,如下图所示:
- 属性名称:conf.yml
- 期望值:value
确定后,即可在属性列表查看相关属性。
而后会通过端云协同将所有属性同步至边缘设备,且以增量更新的方式通知。具体为通过MQTT订阅相关主题,接收事件消息,消息内容即为需要更新的属性键值对。
说明:云端会一直下发期望值,边缘侧会一直收到消息,直到边缘侧上报值和云端期望值一样,云端才会停止发送消息。
通常是边缘运行应用订阅系统MQTT broker,在这里对节点系统broker应用配置进行修改,便于订阅主题。
点击节点应用部署,并进入系统broker应用(名称中有broker)进行配置修改,点击应用配置。
添加端口配置,配置1883->1883端口映射,并点击确定。
然后,切换到数据卷标签,并进入conf配置卷(名称中有conf)进行配置修改。
点击编辑,添加listeners。
内容为
listeners:
- address: 'tcp://0.0.0.0:1883'
session:
sysTopics:
- $link
- $baetyl
logger:
level: debug
encoding: console
改好后点击保存,并确定。
打开MQTT.Box,配置设备host。
确认已连接至设备MQTT broker,并订阅 $baetyl/node/props 主题
、
之后会接收到来自该主题的消息,内容为云端节点属性。
说明:
- 边缘应用订阅系统MQTT broker时通常是使用证书认证,实现免密码配置
- 边缘应用可以使用在/var/lib/baetyl/system/certs下证书连接baetyl-broker,连接的服务地址为:ssl://baetyl-broker.baetyl-edge-system:50010
- 此处为了便于演示,所以单独配置了用户名密码连接baetyl-broker
通过OpenAPI获取节点影子属性
边缘应用服务调用需要基于应用证书校验。应用证书的获取方式参考:证书管理。
且由于baetyl-core应用配置了端口映射30050->443,使用如下命令获取节点影子:
# version<2.3.4 curl -k https://localhost:30050/node/properties # version>=2.3.4 curl --cacert
ca.pem --cert crt.pem --key key.pem https://localhost:30050/node/properties
上述desire为云端期望数据。report为空,表示没有上报数据。
通过OpenAPI更新节点影子属性
类似于获取节点影子属性,使用证书以HTTPS协议可以对节点影子数据进行更新,接口同样为 https://baetyl-core.baetyl-edge-system/node/properties
同样以模拟方式,跳过对服务端证书校验,执行如下命令:
# version<2.3.4 curl -k -X PUT https://localhost:30050/node/properties -d '{"conf.yml":"value"}'
# version>=2.3.4 curl --cacert ca.pem --cert crt.pem --key key.pem -X PUT https://
localhost:30050/node/properties -d '{"conf.yml":"value"}'
指定需要更新的属性,接口会返回当前节点影子所有属性。也可以再次调用获取节点影子属性的api查看。
# version<2.3.4 curl -k https://localhost:30050/node/properties # version>=2.3.4 curl
--cacert ca.pem --cert crt.pem --key key.pem https://localhost:30050/node/properties
同时,在云端的节点影子可以看到边缘设备对属性的更新。
注意事项 节点影子功能依赖于系统应用baetyl-broker,如对系统应用baetyl-broker的配置更改影响了baetyl-broker服务的正常运行,将可能导致节点影子功能不可用。
节点状态
- 在边缘侧调用节点状态接口
# version<2.3.4 curl -k https://localhost:30050/node/stats # version>=2.3.4 curl
--cacert ca.pem --cert crt.pem --key key.pem https://localhost:30050/node/stats
- 节点状态返回样例
{ "name":"node-v234", "createTime":"2022-06-22T08:28:55.260431395Z", "report"
:{ "time":"2022-06-22T09:21:47.572487627Z", "sysapps":[ { "name":"baetyl-init-
zblt9lxq3", "version":"1655885684uxsem4" }, { "name":"baetyl-core-wlnwutiv6",
"version":"1655885684bstpt7" }, { "name":"baetyl-broker-j2sqaze3v", "version":"
1655889584ewsaqs" }, { "name":"baetyl-agent-xgaiskczn", "version":"1655885685hgzz7s"
} ], "core":{ "goVersion":"go1.13.5", "binVersion":"v2.3.4", "gitRevision":"git-2524dce"
}, "sysappstats":[ { "name":"baetyl-init-zblt9lxq3", "version":"1655885684uxsem4", "
deployType":"deployment", "status":"Running", "instances":{ "baetyl-init-7f55445ff4-tnp7j"
:{ "name":"baetyl-init-7f55445ff4-tnp7j", "appName":"baetyl-init-zblt9lxq3", "usage":{ "
cpu":"0.003", "memory":"10612736" }, "status":"Running", "ip":"10.42.0.76", "nodeName":"
pd-vm", "createTime":"2022-06-22T08:28:40Z", "containers":[ { "name":"baetyl-init", "usage"
:{ "cpu":"2835088n", "memory":"10364Ki" }, "state":"Running" } ] } } }, { "name":"baetyl-
core-wlnwutiv6", "version":"1655885684bstpt7", "deployType":"deployment", "status":"Running
", "instances":{ "baetyl-core-wlnwutiv6-798fcd98fc-72ksp":{ "name":"baetyl-core-wlnwutiv6
-798fcd98fc-72ksp", "appName":"baetyl-core-wlnwutiv6", "usage":{ "cpu":"0.005", "memory":"
11939840" }, "status":"Running", "ip":"10.42.0.77", "nodeName":"pd-vm", "createTime":"
2022-06-22T08:28:52Z", "containers":[ { "name":"baetyl-core", "usage":{ "cpu":"4073131n",
"memory":"11660Ki" }, "state":"Running" } ] } } }, { "name":"baetyl-broker-j2sqaze3v", "
version":"1655889584ewsaqs", "deployType":"deployment", "status":"Running", "instances":
{ "baetyl-broker-j2sqaze3v-b4c4f8fc8-kblfw":{ "name":"baetyl-broker-j2sqaze3v-b4c4f8fc8-kblfw
", "appName":"baetyl-broker-j2sqaze3v", "usage":{ "cpu":"0.001", "memory":"3088384" }, "status
":"Running", "ip":"10.42.0.80", "nodeName":"pd-vm", "createTime":"2022-06-22T09:19:56Z", "
containers":[ { "name":"baetyl-broker", "usage":{ "cpu":"366602n", "memory":"3016Ki" }, "state
":"Running" } ] } } }, { "name":"baetyl-agent-xgaiskczn", "version":"1655885685hgzz7s", "
deployType":"daemonset", "status":"Running", "instances":{ "baetyl-agent-xgaiskczn-75mdk":
{ "name":"baetyl-agent-xgaiskczn-75mdk", "appName":"baetyl-agent-xgaiskczn", "usage":{ "cpu
":"0.002", "memory":"4612096" }, "status":"Running", "ip":"10.211.55.5", "nodeName":"pd-vm"
, "createTime":"2022-06-22T08:29:08Z", "containers":[ { "name":"baetyl-agent", "usage":
{ "cpu":"1300406n", "memory":"4504Ki" }, "state":"Running" } ] } } } ], "node":{ "pd-vm":
{ "hostname":"pd-vm", "address":"10.211.55.5", "arch":"arm64", "kernelVer":"5.13.0-37-generic
", "os":"linux", "containerRuntime":"docker://20.10.13", "machineID":"3787c48ab74047b28638ef2ddc97be6c
", "bootID":"2e058c7a-072f-4d6c-baf1-ed81a948f15c", "systemUUID":"c4be2e07-1110-4801-a2af-e5f9a9aed71b",
"osImage":"Ubuntu 20.04.4 LTS", "role":"master", "labels":{ "baetyl-node":"true", "beta.kubernetes.
io/arch":"arm64", "beta.kubernetes.io/instance-type":"k3s", "beta.kubernetes.io/os":"linux", "
kubernetes.io/arch":"arm64", "kubernetes.io/hostname":"pd-vm", "kubernetes.io/os":"linux", "
node-role.kubernetes.io/control-plane":"true", "node-role.kubernetes.io/master":"true", "node
.kubernetes.io/instance-type":"k3s" } } }, "nodestats":{ "pd-vm":{ "ready":true, "usage":{ "
cpu":"0.211", "disk":"28374298624", "memory":"1637335040" }, "capacity":{ "cpu":"2", "disk":"
66842079232", "memory":"4104314880" }, "percent":{ "cpu":"0.1055", "disk":"0.42449754630637043
", "memory":"0.39893017175134476" }, "netio":{ "netBytesRecv":"1470", "netBytesSent":"1362", "
netPacketsRecv":"19", "netPacketsSent":"17" }, "extension":{ "diskPercent":0.42449754630637043,
"diskTotal":66842079232, "diskUsed":28374298624, "netBytesRecv":1470, "netBytesSent":1362, "
netPacketsRecv":19, "netPacketsSent":17 } } }, "nodeinsnum":{ "pd-vm":4 }, "modeinfo":"v1.22.5+k3s1
" }, "appMode":"kube", "desire":{ "nodeprops":{ "conf.yml":"value" } }, "cluster":false, "ready":1, "mode":"" }