KubeEdge temperature 部署

本文对官方示例 temperature 进行实验。

功能说明

本示例主要是演示在云端获取边缘端的设备状态。

编译

本文对官方示例进行了修改。此处给出修改描述,详情参考修改后的源码。
1、参考 led 示例,新加 Makefile,修改 Dockerfile。
2、修改源码,去掉与硬件操作相关的函数,将采集的温度值 temperature 累加以观察其变化。
3、制作镜像,提交镜像。
3、修改 crds 和 deployment.yaml 文件,指定调度节点名称。

实验

部署:

kubectl apply -f crds/
kubectl apply -f  deployment.yaml 

查看pod:

# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
temperature-mapper-77fb74f5-vzztl   1/1     Running   0          5m10s

在边缘商查看日志:

# docker logs ecc3ba6a29dc
2020-03-22T09:11:51.777 [    main] INFO  Sensor = DHT11: Temperature = 74*C, Humidity = 85% (retried 1 times)
2020-03-22T09:11:56.778 [    main] INFO  Sensor = DHT11: Temperature = 75*C, Humidity = 85% (retried 1 times)
2020-03-22T09:12:01.778 [    main] INFO  Sensor = DHT11: Temperature = 76*C, Humidity = 85% (retried 1 times)

在云端观察:

kubectl get device temperature1 -oyaml -w

输出示例:
apiVersion: devices.kubeedge.io/v1alpha1
kind: Device
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"devices.kubeedge.io/v1alpha1","kind":"Device","metadata":{"annotations":{},"labels":{"description":"temperature","manufacturer":"test"},"name":"temperature1","namespace":"default"},"spec":{"deviceModelRef":{"name":"temperature-model"},"nodeSelector":{"nodeSelectorTerms":[{"matchExpressions":[{"key":"","operator":"In","values":["latelee1"]}]}]}},"status":{"twins":[{"desired":{"metadata":{"type":"string"},"value":""},"propertyName":"temperature-status"}]}}
  creationTimestamp: "2020-03-22T09:04:18Z"
  generation: 77
  labels:
    description: temperature
    manufacturer: test
  name: temperature1
  namespace: default
  resourceVersion: "29280"
  selfLink: /apis/devices.kubeedge.io/v1alpha1/namespaces/default/devices/temperature1
  uid: e9869339-6d9b-4bf3-bf9f-c6191efeedc7
spec:
  deviceModelRef:
    name: temperature-model
  nodeSelector:
    nodeSelectorTerms:
    - matchExpressions:
      - key: ""
        operator: In
        values:
        - latelee1
status:
  twins:
  - desired:
      metadata:
        type: string
      value: ""
    propertyName: temperature-status
    reported:
      metadata:
        timestamp: "1584868316781"
        type: string
      value: 75C   // !!!! 此值会变化

源码研究

流程:
1、连接 mqtt:connectToMqtt,故需要边缘端开启 mqtt 服务,并监听 1883 端口。
2、采集温度:ReadDHTxxWithContextAndRetry,本例注释。
3、将温度值发布到mqtt:publishToMqtt。
4、之后进入 KubeEdge 系统,在云端可查看状态。

其它说明:
发布主题指定如下

deviceTwinUpdate := "$hw/events/device/" + "temperature" + "/twin/update"

temperature 为设备名称。需要与 Device 中的 metadata.name 保持一致。此名称也是 kubectl get device 查看到的名称。可修改使其不一致,观察效果。

发布消息前先创建消息体:createActualUpdateMessage,结构体为 DeviceTwinUpdate,具体如下:

map[string]*MsgTwin{"temperature-status": {Actual: &TwinValue{Value: &actualValue}, Metadata: &TypeMetadata{Type: "Updated"}}}

查看device.yaml:

status:
  twins:
    - propertyName: temperature-status
      desired:
        metadata:
          type: string
        value: ''

目前看,部分对应起来,深层理论待研究。

你可能感兴趣的:(kubeedge)