KubeEdge led部署

本文对 KubeEdge 的 led 灯示例进行测试。

KubeEdge 官方示例文件仓库为 https://github.com/kubeedge/examples ,将其下载到$GOPATH/src/github.com/kubeedge/ 目录,本文所用目录为 led-raspberrypi 。
本文所有修改见仓库 https://github.com/latelee/kube-examples/tree/master/led-raspberrypi

下面按测试步骤描述。

源码说明

sample-crds:crds 配置,指定了调度的节点、GPIO号,LED默认状态,等等。下称设备模型。
configuration:配置相关,需要读取 deviceProfile.json (运行时生成)和 config.yaml 文件。
light_mapper.go:主程序文件,主要匹配 crds 设备模型,并与真实硬件交互。与硬件交互主要使用github.com/stianeikeland/go-rpio/包,由于笔者没硬件环境,故其操作 GPIO 的代码注释掉,仅作示例。

Dockerfile:生成 docker 镜像文件,笔者扩展了 arm 平台(注:笔者去掉了硬件操作,故代码可适用不同平台)。
deployment.yaml:deployment 配置文件,指定调度节点、镜像和 configMap (如果不指定,生成不了 json 文件)。

理论上,不同的硬件,其操作不同,因此需要不同的 crds 为匹配。这也是为什么 crds 中要指定节点的原因。不过,笔者认为,实践中,可能存在批量操作,即同一批硬件,其硬件相同,功能相同,因此使用的程序也相同,如温度采集等。此情况下,可以通过节点的 label 来匹配调度的节点。当然,这不是本文关注的重点。

编译

笔者修改了 Makefile,如下:

# make led_light_mapper
.PHONY: default led_light_mapper
led_light_mapper:
        export GOARCH=amd64; export GOOS="linux"; export GOARM=""; export CGO_ENABLED=1; export CC=cc; \
    go build light_mapper.go
        docker build -t latelee/led-light-mapper-x86:v1.1 . -f Dockerfile 
        export GOARCH=arm; export GOOS="linux"; export GOARM=7; export CGO_ENABLED=1; export CC=arm-linux-gnueabihf-gcc; \
    go build light_mapper.go
        docker build -t latelee/led-light-mapper-arm:v1.1 . -f Dockerfile-arm

分别使用不同编译器编译,并修改 docker 镜像地址。接着合并镜像:


docker push latelee/led-light-mapper-x86:v1.1
docker push latelee/led-light-mapper-arm:v1.1

export DOCKER_CLI_EXPERIMENTAL=enabled

docker manifest create latelee/led-light-mapper:v1.1 latelee/led-light-mapper-x86:v1.1 latelee/led-light-mapper-arm:v1.1

docker manifest annotate latelee/led-light-mapper:v1.1 latelee/led-light-mapper-x86:v1.1 --os linux --arch x86_64
docker manifest annotate latelee/led-light-mapper:v1.1 latelee/led-light-mapper-arm:v1.1 --os linux --arch armv7l

docker manifest push latelee/led-light-mapper:v1.1

创建设备模型

kubeedge在部署时已经创建了crds了,此处查看:

# kubectl get crds
NAME                                           CREATED AT
clusterobjectsyncs.reliablesyncs.kubeedge.io   2020-02-20T08:28:32Z
devicemodels.devices.kubeedge.io               2019-12-31T08:41:34Z
devices.devices.kubeedge.io                    2019-12-31T08:41:34Z
objectsyncs.reliablesyncs.kubeedge.io          2020-02-20T08:28:32Z

再创建led的crds。

cd $GOPATH/src/github.com/kubeedge/examples/led-raspberrypi
cd sample-crds
vim led-light-device-instance.yaml 

修改 led-light-device-instance.yaml 文件,将节点改为 latelee.org.ttucon-2142ec
创建:

# kubectl apply -f .

查看

# kubectl get deviceModel
NAME        AGE
led-light   28h

# kubectl get device
NAME                    AGE
led-light-instance-01   28h

查看详情:

# kubectl describe devices.devices.kubeedge.io  led-light-instance-01

部署

修改 deployment.yaml,如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: led-light-mapper-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: led-light-mapper
  template:
    metadata:
      labels:
        app: led-light-mapper
    spec:
      nodeName: latelee.org.ttucon-2142ec #edge-node2
      hostNetwork: true
      containers:
      - name: led-light-mapper-container
        image: latelee/led-light-mapper:v1.1
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        volumeMounts:
        - name: config-volume
          mountPath: /opt/kubeedge/
      volumes:
      - name: config-volume
        configMap:
          name: device-profile-config-edge-node2
      restartPolicy: Always

部署:

# kubectl apply -f deployment.yaml 

等待调度完成。

测试

修改 LED 状态值,把OFF改为ON,:

vim led-light-device-instance.yaml 

再更新配置。

注:
如果此时修改led-light-device-model.yaml 的引脚号,再更新,是不成功的。

检查

在节点机器上,执行docker ps查看容器。再进入容器查看json文件:

# docker 4bc0f93a0174
/ # cat /opt/kubeedge/deviceProfile.json 
{"deviceInstances":[{"id":"led-light-instance-01","name":"led-light-instance-01","model":"led-light"}],"deviceModels":[{"name":"led-light","properties":[{"name":"power-status","dataType":"string","description":"Indicates whether the led light is ON/OFF","accessMode":"ReadWrite","defaultValue":"OFF"},{"name":"gpio-pin-number","dataType":"int","description":"Indicates whether the GPIO pin to which LED is connected","accessMode":"ReadOnly","defaultValue":18}]}],"protocols":[{"protocol_config":null}]}

查看日志(加-f):

docker logs -f 4bc0f93a0174
I0318 03:54:21.558189       1 light_mapper.go:242] Watching on the device twin values for device: led-light-instance-01
I0318 03:54:22.559353       1 light_mapper.go:272] Actual values are in sync with Expected value
I0318 03:54:22.559374       1 light_mapper.go:242] Watching on the device twin values for device: led-light-instance-01
I0318 03:54:23.560669       1 light_mapper.go:272] Actual values are in sync with Expected value
I0318 03:54:23.560695       1 light_mapper.go:242] Watching on the device twin values for device: led-light-instance-01
I0318 03:54:24.561883       1 light_mapper.go:248] Expected Value : ON
I0318 03:54:24.561909       1 light_mapper.go:252] Actual Value: OFF
I0318 03:54:24.561913       1 light_mapper.go:254] Equating the actual  value to expected value
I0318 03:54:24.561918       1 light_mapper.go:257] Turning ON the light
I0318 03:54:24.561922       1 light_driver.go:11] TurnON pin: 18
I0318 03:54:24.562033       1 light_mapper.go:242] Watching on the device twin values for device: led-light-instance-01
I0318 03:54:25.563141       1 light_mapper.go:248] Expected Value : ON
I0318 03:54:25.563164       1 light_mapper.go:252] Actual Value: OFF
I0318 03:54:25.563168       1 light_mapper.go:254] Equating the actual  value to expected value
I0318 03:54:25.563172       1 light_mapper.go:257] Turning ON the light
I0318 03:54:25.563195       1 light_driver.go:11] TurnON pin: 18
I0318 03:54:25.563281       1 light_mapper.go:242] Watching on the device twin values for device: led-light-instance-01

从日志中看到 GPIO 引脚的电平变化了。

排错

如果不使用 KubeEdge 调度的话,容器报错:

Error while reading from config map Error while reading from config map open /opt/kubeedge/deviceProfile.json: no such file or directory
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章