KubeEdge使用文档

KubeEdge将Kubernetes的能力延伸到了边缘场景中,为云和边缘之间的网络,应用部署和元数据同步提供基础架构支持。KubeEdge在使用上与Kubernetes保持完全一致,除此之外还扩展了对边缘设备的管理与控制。本节将通过一个简单的例子向用户演示如何通过KubeEdge完成设备边云协同任务。

1. 准备工作

选用示例:KubeEdge Counter Demo

计数器是一个伪设备,用户无需任何额外的物理设备即可运行此演示。计数器在边缘侧运行,用户可以从云侧在Web中对其进行控制,也可以从云侧在Web中获得计数器值。原理图如下:

详细文档参考:https://github.com/kubeedge/examples/tree/master/kubeedge-counter-demo

1)本示例要求KubeEdge版本必须是v1.2.1+,此次选择最新版的KubeEdge v1.8.0

  1. [root@ke-cloud ~]# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. ke-cloud Ready master 13h v1.20.2
  4. ke-edge1 Ready agent,edge 64s v1.19.3-kubeedge-v1.8.0
  5. 说明:本文接下来的验证将使用边缘节点ke-edge1进行,如果你参考本文进行相关验证,后续边缘节点名称的配置需要根据你的实际情况进行更改。

2)确保k8s apiserver开启了以下配置:

  1. --insecuret-port=8080
  2. --insecure-bind-address=0.0.0.0

可以通过修改/etc/kubernetes/manifests/kube-apiserver.yaml文件,并重启k8s-apiserver组件的pod来进行更改。

3)下载示例代码:

  1. [root@ke-cloud ~]# git clone https://github.com/kubeedge/examples.git $GOPATH/src/github.com/kubeedge/examples

2. 创建device model和device

1)创建device model

  1. [root@ke-cloud ~]# cd $GOPATH/src/github.com/kubeedge/examples/kubeedge-counter-demo/crds
  2. [root@ke-cloud crds~]# kubectl create -f kubeedge-counter-model.yaml

2)创建device

根据你的实际情况修改matchExpressions:

  1. [root@ke-cloud ~]# cd $GOPATH/src/github.com/kubeedge/examples/kubeedge-counter-demo/crds
  2. [root@ke-cloud crds~]# vim kubeedge-counter-instance.yaml
  3. apiVersion: devices.kubeedge.io/v1alpha1
  4. kind: Device
  5. metadata:
  6. name: counter
  7. labels:
  8. description: 'counter'
  9. manufacturer: 'test'
  10. spec:
  11. deviceModelRef:
  12. name: counter-model
  13. nodeSelector:
  14. nodeSelectorTerms:
  15. - matchExpressions:
  16. - key: 'kubernetes.io/hostname'
  17. operator: In
  18. values:
  19. - ke-edge1
  20. status:
  21. twins:
  22. - propertyName: status
  23. desired:
  24. metadata:
  25. type: string
  26. value: 'OFF'
  27. reported:
  28. metadata:
  29. type: string
  30. value: '0'
  31. [root@ke-cloud crds~]# kubectl create -f kubeedge-counter-instance.yaml

3. 部署云端应用

1)修改代码

云端应用web-controller-app用来控制边缘端的pi-counter-app应用,该程序默认监听的端口号为80,此处修改为8089,如下所示:

  1. [root@ke-cloud ~]# cd $GOPATH/src/github.com/kubeedge/examples/kubeedge-counter-demo/web-controller-app
  2. [root@ke-cloud web-controller-app~]# vim main.go
  3. package main
  4. import (
  5. "github.com/astaxie/beego"
  6. "github.com/kubeedge/examples/kubeedge-counter-demo/web-controller-app/controller"
  7. )
  8. func main() {
  9. beego.Router("/", new(controllers.TrackController), "get:Index")
  10. beego.Router("/track/control/:trackId", new(controllers.TrackController), "get,post:ControlTrack")
  11. beego.Run(":8089")
  12. }

2)构建镜像

注意:构建镜像时,请将源码拷贝到GOPATH对应的路径下,如果开启了go mod请关闭。

  1. [root@ke-cloud web-controller-app~]# make all
  2. [root@ke-cloud web-controller-app~]# make docker

3)部署web-controller-app

  1. [root@ke-cloud ~]# cd $GOPATH/src/github.com/kubeedge/examples/kubeedge-counter-demo/crds
  2. [root@ke-cloud crds~]# kubectl apply -f kubeedge-web-controller-app.yaml

4. 部署边缘端应用

边缘端的pi-counter-app应用受云端应用控制,主要与mqtt服务器通信,进行简单的计数功能。

1)修改代码与构建镜像

需要将Makefile中的GOARCH修改为amd64才能运行该容器。

  1. [root@ke-cloud ~]# cd $GOPATH/src/github.com/kubeedge/examples/kubeedge-counter-demo/counter-mapper
  2. [root@ke-cloud counter-mapper~]# vim Makefile
  3. .PHONY: all pi-execute-app docker clean
  4. all: pi-execute-app
  5. pi-execute-app:
  6. GOARCH=amd64 go build -o pi-counter-app main.go
  7. docker:
  8. docker build . -t kubeedge/kubeedge-pi-counter:v1.0.0
  9. clean:
  10. rm -f pi-counter-app
  11. [root@ke-cloud counter-mapper~]# make all
  12. [root@ke-cloud counter-mapper~]# make docker

2)部署Pi Counter App

  1. [root@ke-cloud ~]# cd $GOPATH/src/github.com/kubeedge/examples/kubeedge-counter-demo/crds
  2. [root@ke-cloud crds~]# kubectl apply -f kubeedge-pi-counter-app.yaml
  3. 说明:为了防止Pod的部署卡在`ContainerCreating`,这里直接通过docker savescpdocker load命令将镜像发布到边缘端
  4. [root@ke-cloud ~]# docker save -o kubeedge-pi-counter.tar kubeedge/kubeedge-pi-counter:v1.0.0
  5. [root@ke-cloud ~]# scp kubeedge-pi-counter.tar root@192.168.1.56:/root
  6. [root@ke-edge1 ~]# docker load -i kubeedge-pi-counter.tar

5. 体验Demo

现在,KubeEdge Demo的云端部分和边缘端的部分都已经部署完毕,如下:

  1. [root@ke-cloud ~]# kubectl get pods -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. kubeedge-counter-app-758b9b4ffd-f8qjj 1/1 Running 0 26m 192.168.1.66 ke-cloud <none> <none>
  4. kubeedge-pi-counter-c69698d6-rb4xz 1/1 Running 0 2m 192.168.1.56 ke-edge1 <none> <none>

我们现在开始测试一下该Demo运行效果:

1)执行ON命令 在web页面上选择ON,并点击Execute,可以在edge节点上通过以下命令查看执行结果:

  1. [root@ke-edge1 ~]# docker logs -f counter-container-id

2)查看counter STATUS 在web页面上选择STATUS,并点击Execute,会在Web页面上返回counter当前的status,如下所示:

3)执行OFF命令 在web页面上选择OFF,并点击Execute,可以再edge节点上通过以下命令查看执行结果:

  1. [root@ke-edge1 ~]# docker logs -f counter-container-id

6. 其它

1)更过多的KubeEdge官方示例请参考 https://github.com/kubeedge/examples

Name Description
LED-RaspBerry-Pi Controlling a LED light with Raspberry Pi using KubeEdge platform
Data Analysis @ Edge Analyzing data at edge by using Apache Beam and KubeEdge
Security@Edge Security at edge using SPIRE for identity management

Bluetooth-CC2650-demo |Controlling a CC2650 SensorTag bluetooth device using KubeEdge platform | Play Music @Edge through WeChat | Play music at edge based on WeChat and KubeEdge | Play Music @Edge through Web | Play music at edge based on Web and KubeEdge | Collecting temperature @Edge | Collecting temperature at edge based KubeEdge | Control pseudo device counter and collect data | Control pseudo device counter and collect data based KubeEdge Play Music @Edge through Twitter| Play music at edge based on Twitter and KubeEdge. Control Zigbee @Edge through cloud | Face detection at cloud using OpenCV and using it to control zigbee on edge using Kubeedge.

2)使用EdgeMesh做边缘服务发现

https://github.com/kubeedge/edgemesh

3)自定义云边消息路由

https://kubeedge.io/en/docs/developer/custom_message_deliver/