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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章