#创作者激励# 领航计划(五): 玩转KubeEdge Router(核心) 原创
Piwriw.
发布于 2023-3-11 20:03
浏览
0收藏
领航计划(五): 玩转KubeEdge Router
什么是 Router
- router 是 KubeEdge 中的一种通过实现 CRDs 来实现与边缘端进行MQTT通信的模式
- 在 KubeEdge 中通过 Rule 和 RuleEndPoint 实现了几种通信模式
配置开启 router
云端(cloud)
- 开启router模式
# 如果你是通过pod安装的cloudcore 查看是否开启
#可以通过kubectl edit pod <cloudcore_name> -n kubeedge 修改
kubectl describe cm cloudcore -n kubeedge
.......
router:
enable: true
.......
边缘端(edge)
- 开启
eventbus
和servicebus
vi /etc/kubeedge/config/edgecore.yaml
metaManager:
contextSendGroup: hub
contextSendModule: websocket
enable: true
metaServer:
enable: true
server: 127.0.0.1:10550
tlsCaFile: /etc/kubeedge/ca/rootCA.crt
tlsCertFile: /etc/kubeedge/certs/server.crt
tlsPrivateKeyFile: /etc/kubeedge/certs/server.key
remoteQueryTimeout: 60
serviceBus:
enable: true
port: 9060
server: 127.0.0.1
timeout: 60
什么是 EventBus 和 ServiceBus
EventBus-边缘消息
-
- 通过MQTT接收外部客户端到消息
-
- 通过MQTT发送消息
ServiceBus-边缘消息
-
- 云端通过 beehive 发送消息
-
- EdgeHub接收消息然后发送给ServiceBus
-
- ServiceBus 提供http调用然后返回消息
三大消息传递
云到边 - eventbus
-
- 用户创建源和目标的规则
-
- 云端APP调用api发送消息,消息到边缘端后通过eventbus 发布到 MQTT Broker
-
- 边缘端应用收到订阅的相关消息
云到边 - servicebus
-
- 用户创建源和目标的规则
-
- 云端 App 调用云端 api 发送消息,到达 servicebus
-
- servicebus 调用 边缘端 App 的 http API
边的 eventbus ->云
-
- 用户创建源和目标的规则
-
- 边缘应用发布消息到 MQTT Broker,eventbus 订阅消息并发布到云端
-
- 云端应用通过配置 router 中定义的规则得到上述消息
我的最佳实践
环境准备
- KubeEdge、k8s 、node、cloud
- 在云边,准备 go version = 1.17 ,开启 Go Modules 和goproxy代理
- 在边缘端,打开
servicebus
和eventbus
,检查你的/etc/kubeedge/config/edgecore.yaml
eventbus->rest
1. 准备ruleendpoint
kubectl apply -f ruleEndpoint-eventbus.yaml
kubectl apply -f ruleEndpoint-rest.yaml
ruleEndpoint-rest.yaml:
apiVersion: rules.kubeedge.io/v1
kind: RuleEndpoint
metadata:
name: my-rest
labels:
description: test
spec:
ruleEndpointType: "rest"
properties: {}
ruleEndpoint-eventbus.yaml:
apiVersion: rules.kubeedge.io/v1
kind: RuleEndpoint
metadata:
name: my-eventbus
labels:
description: test
spec:
ruleEndpointType: "eventbus"
properties: {}
2. 准备 rule-eventbus-to-rest.yaml文件
- 需要注意修改
node_name
为你的边缘节点,targetResource
:用的你的服务器ip
rule-eventbus-to-rest.yaml:
apiVersion: rules.kubeedge.io/v1
kind: Rule
metadata:
name: my-rule-eventbus-rest
labels:
description: test
spec:
source: "my-eventbus"
sourceResource: {"topic": "test","node_name": "edge-02"}
target: "my-rest"
targetResource: {"resource":"http://yourip/myevents"}
3. 通过web测试
main.go:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
)
func main() {
r := gin.Default()
r.POST("/myevents", func(c *gin.Context) {
s, _ := ioutil.ReadAll(c.Request.Body)
fmt.Printf("edge say '%s' ", s)
})
r.Run()
}
go mod tidy
go run main.go
4. 在边缘端通过mosquitto
进行测试
mosquitto_pub -t 'default/test' -d -m '{"edgemsg":"edge send msg to cloud by eventbus"}'
5. 验证结果
rest->servicebus
1. 准备ruleendpoint
kubectl apply -f ruleEndpoint-rest.yaml
kubectl apply -f ruleEndpoint-servicebus.yaml
ruleEndpoinr-rest.yaml:
apiVersion: rules.kubeedge.io/v1
kind: RuleEndpoint
metadata:
name: my-rest
labels:
description: test
spec:
ruleEndpointType: "rest"
properties: {}
ruleEndpoint-servicebus.yaml:
apiVersion: rules.kubeedge.io/v1
kind: RuleEndpoint
metadata:
name: my-servicebus
labels:
description: test
spec:
ruleEndpointType: "servicebus"
properties: { "service_port": "8888" }
2. 准备rule-rest-to-servicebus.yaml
apiVersion: rules.kubeedge.io/v1
kind: Rule
metadata:
name: my-rule-rest-servicebus
labels:
description: test
spec:
source: "my-rest"
sourceResource: { "path": "/rest" }
target: "my-servicebus"
targetResource: { "path": "/servicebus" }
3. 部署web测试
go mod tidy
go run main.go
main.go:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.POST("/servicebus", func(c *gin.Context) {
s, _ := ioutil.ReadAll(c.Request.Body)
fmt.Printf("edge say '%s' ", s)
})
r.Run(":8888")
}
4.在云端发送进行测试,注意edge-02
是你边缘节点的名字
curl -H "Content-type: application/json" -X POST -d '{"msg":"cloud send"}' http://127.0.0.1:9443/edge-02/default/rest
5. 验证结果
MoreInfo
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
已于2023-3-12 14:31:31修改
赞
1
收藏
回复
相关推荐
老师,router enable: true 打开,但本机的9443端口始终没有监听,rest发送时,一直无法连接。请问是啥问题
可以给出详细的操作步骤和信息吗?
您好,我也遇到了一样的问题。
能详细讲一下如何通过kubectl edit pod <podname> -n kubeedge打开9443端口么?
我通过另一个命令kubectl edit cm cloudcore -n kubeedge将router的enabled变为true仍不能打开9443端口
添加port,address也无法打开9443
apiVersion: cloudcore.config.kubeedge.io/v1alpha2
kind: CloudCore
kubeAPIConfig:
kubeConfig: ""
master: ""
modules:
cloudHub:
advertiseAddress:
- 192.168.1.200
dnsNames:
-
nodeLimit: 1000
tlsCAFile: /etc/kubeedge/ca/rootCA.crt
tlsCertFile: /etc/kubeedge/certs/edge.crt
tlsPrivateKeyFile: /etc/kubeedge/certs/edge.key
unixsocket:
address: unix:///var/lib/kubeedge/kubeedge.sock
enable: true
websocket:
address: 0.0.0.0
enable: true
port: 10000
quic:
address: 0.0.0.0
enable: false
maxIncomingStreams: 10000
port: 10001
https:
address: 0.0.0.0
enable: true
port: 10002
cloudStream:
enable: true
streamPort: 10003
tunnelPort: 10004
dynamicController:
enable: false
router:
enable: true
iptablesManager:
enable: true
mode: internal
打开9443端口,主要是指的是关闭掉服务器防火墙,一般来说能正常使用就没问题