# KubeEdge
https://iothub.org.cn/docs/kubeedge/
https://iothub.org.cn/docs/kubeedge/edge/iot/
物联网平台(IoT)部署在云上,网关部署在边缘节点,设备通过边缘网关接入物联网平台。
云边协同设计:
物联网平台选择 ThingsBoard,边缘网关选择 EMQX、Node-RED,MQTTX 工具模拟终端设备。
IoT平台设计:
使用 Kubernetes 在云端部署 ThingsBoard 集群
使用 KubeEdge 在边缘节点部署 EMQX、Node-Red,实现边缘网关
MQTTX 工具模拟终端设备,通过 MQTT 协议接入边缘网关
物联网平台(IoT)具体实现:
主机名 | IP地址 | 角色 |
---|---|---|
k8s-master | 192.168.202.201 | master |
k8s-node1 | 192.168.202.202 | node |
k8s-node2 | 192.168.202.203 | node |
edge-1 | 192.168.202.211 | edge |
信息 | 版本 | 备注 |
---|---|---|
K8s | v1.23.12 | |
centos | 7.8 | # cat /etc/redhat-release |
KubeEdge | v1.13.4 | |
ThingsBoard | v3.5.1 | |
EMQX | 4.4.17 | |
Node-RED | latest | |
部署 Kubernetes 集群参考
# 部署 Kubernetes 集群
https://iothub.org.cn/docs/kubernetes/pro/deploy-kubernetes/
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 27d v1.23.12
k8s-node1 Ready <none> 27d v1.23.12
k8s-node2 Ready <none> 10d v1.23.12
部署 KubeEdge 参考
# 部署 KubeEdge
https://iothub.org.cn/docs/kubeedge/deploy/deploy/
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
edge-1 Ready agent,edge 4d4h v1.23.17-kubeedge-v1.13.4
k8s-master Ready control-plane,master 27d v1.23.12
k8s-node1 Ready <none> 27d v1.23.12
k8s-node2 Ready <none> 10d v1.23.12
[root@k8s-master ~]# kubectl get all -n kubeedge
NAME READY STATUS RESTARTS AGE
pod/cloud-iptables-manager-592m5 1/1 Running 3 (3d22h ago) 4d5h
pod/cloud-iptables-manager-pg4pl 1/1 Running 3 (3d22h ago) 4d5h
pod/cloudcore-5959c5986f-8hsc4 1/1 Running 3 (3d22h ago) 4d5h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/cloudcore NodePort 10.110.71.216 <none> 10000:30976/TCP,10001:31372/TCP,10002:31922/TCP,10003:30163/TCP,10004:31927/TCP 4d5h
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/cloud-iptables-manager 2 2 2 2 2 <none> 4d5h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/cloudcore 1/1 1 1 4d5h
NAME DESIRED CURRENT READY AGE
replicaset.apps/cloudcore-5959c5986f 1 1 1 4d5h
部署 ThingsBoard 参考
# ThingsBoard 单机部署
https://iothub.org.cn/docs/iot/deploy/deploy-single/
# ThingsBoard 集群部署
https://iothub.org.cn/docs/iot/deploy/deploy-cluster/
备注:考虑测试环境资源有限,部署单机代替ThingsBoard集群
gateway-emqx.yaml
[root@k8s-master kubeedge]# vim gateway-emqx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway-emqx
spec:
replicas: 1
selector:
matchLabels:
app: emqx
template:
metadata:
labels:
app: emqx
spec:
nodeName: edge-1 #调度到指定机器
containers:
- name: emqx
image: emqx/emqx:4.4.17
ports:
- containerPort: 1883
hostPort: 1884
- containerPort: 18083
hostPort: 18084
- containerPort: 8081
hostPort: 8082
- containerPort: 8083
hostPort: 8085
- containerPort: 8883
hostPort: 8884
- containerPort: 8084
hostPort: 8086
[root@k8s-master iot]# kubectl apply -f gateway-emqx.yaml
deployment.apps/gateway-emqx created
[root@k8s-master iot]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/gateway-emqx-6fcb56cb4f-n8z5s 1/1 Running 0 12s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/gateway-emqx 1/1 1 1 12s
NAME DESIRED CURRENT READY AGE
replicaset.apps/gateway-emqx-6fcb56cb4f 1 1 1 12s
访问EMQX
# 访问地址
http://192.168.202.211:18084
账号:admin
初始密码:public
修改密码:1qaz2wsx
# 参考
$ docker run -d --name emqx -p 18083:18083 -p 1883:1883 emqx/emqx:latest
$ docker run -d --name emqx -p 1883:1883 -p 8081:8081 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqx/emqx:v4.0.0
gateway-node-red.yaml
[root@k8s-master kubeedge]# vim gateway-node-red.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway-node-red
spec:
replicas: 1
selector:
matchLabels:
app: nodered
template:
metadata:
labels:
app: nodered
spec:
nodeName: edge-1 #调度到指定机器
hostNetwork: true # 使用主机网络
containers:
- name: nodered
image: nodered/node-red:latest
部署 Node-RED 到边缘节点
[root@k8s-master iot]# kubectl apply -f gateway-node-red.yaml
deployment.apps/gateway-node-red created
[root@k8s-master iot]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/gateway-5847c4f88c-bqx5j 1/1 Running 0 30s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/gateway 1/1 1 1 31s
NAME DESIRED CURRENT READY AGE
replicaset.apps/gateway-5847c4f88c 1 1 1 30s
访问 Node-RED
# 访问地址
http://192.168.202.211:1880
# 参考
# docker run --network host --restart=always -v node_red_data:/data --name nodered nodered/node-red:latest
将设备遥测发布到ThingsBoard服务器节点,请向以下主题发送publish消息:
Topic: v1/gateway/telemetry
Message:
{
"Device A": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
},
{
"ts": 1483228801000,
"values": {
"temperature": 43,
"humidity": 82
}
}
],
"Device B": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
}
]
}
Node-RED 连接 EMQX,接收 IoT 设备遥测数据
Topic: v1/gateway/telemetry
v1/gateway/telemetry
{
"Device A": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
},
{
"ts": 1483228801000,
"values": {
"temperature": 43,
"humidity": 82
}
}
],
"Device B": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
}
]
}
ThingsBoard
Node-RED
# KubeEdge
https://iothub.org.cn/docs/kubeedge/
https://iothub.org.cn/docs/kubeedge/edge/iot/