k8s还能这么玩?快速上手物联网应用的容器开发

Shifu 是一个 Kubernetes 原生的IoT设备虚拟化框架。Shifu 希望帮助IoT应用开发者以即插即用的方式实现IoT设备的虚拟化、监视、管控和自动化。本文通过在本地集群中部署Shifu,完成通过MQTT、Http等方式接入物联设备,体验了用容器开发的路径完成物联网应用开发。

Shifu官网:https://shifu.run/zh-Hans/ Github仓库:https://github.com/Edgenesis/shifu

创建集群并安装Shifu

# 启动Docker
$ open -a docker

# 创建集群
$ sudo kind create cluster --image="kindest/node:v1.24.0"

# 提前准备镜像导入集群
$ sudo docker pull quay.io/brancz/kube-rbac-proxy:v0.12.0 
$ sudo docker pull edgehub/shifu-controller:v0.1.1
$ sudo docker pull nginx:1.21
$ sudo kind load docker-image quay.io/brancz/kube-rbac-proxy:v0.12.0 edgehub/shifu-controller:v0.1.1 nginx:1.21

# 安装shifu
$ git clone https://github.com/Edgenesis/shifu.git
$ cd shifu
$ sudo kubectl apply -f pkg/k8s/crd/install/shifu_install.yml

# 跑一个应用程序
$ sudo kubectl run --image=nginx:1.21 nginx

设备接入

MQTT

测试MQTT服务器

# shellA
$ mosquitto_sub -h 82.157.170.202 -t topic0

# shellB
$ mosquitto_pub -h 82.157.170.202 -t topic0 -m "哈哈哈"

接入设备

$ sudo docker pull edgehub/deviceshifu-http-mqtt:v0.1.1
$ sudo kind load docker-image edgehub/deviceshifu-http-mqtt:v0.1.1
$ sudo kubectl apply -f examples/my_mqtt/mqtt_deploy

读取数据

$ sudo kubectl exec -it nginx -- bash

$ curl http://deviceshifu-mqtt.deviceshifu.svc.cluster.local/mqtt_data

连接温度计和LED

  • 先将两个设备连接到电脑
  • 然后启起来两个转HTTP的服务 进行演示
  • 然后部署至Shifu
  • 应用开发后进行联动

连接设备至电脑

  • 温度计使用串口服务器通过网线连接至电脑
  • LED使用RS485转USB的芯片连接至电脑

本地启动HTTP服务

$ cd api_thermometer
$ uvicorn --host 0.0.0.0 --port 23330 main:app

$ cd api_led
$ uvicorn --host 0.0.0.0 --port 23331 main:app

本地验证

$ curl http://localhost:23330/temperature
$ curl http://localhost:23330/humidity
$ curl http://localhost:23331/setfloat\?value\=123.4

接入设备

  • 修改http_thermometer/deployment/http_edgedevice.yaml中ip地址。
  • 修改http_led/deployment/http_edgedevice.yaml中ip地址。
$ sudo docker pull edgehub/deviceshifu-http-http:v0.1.1
$ sudo kind load docker-image edgehub/deviceshifu-http-http:v0.1.1
$ sudo kubectl apply -f examples/my_http_led/deployment
$ sudo kubectl apply -f examples/my_http_thermometer/deployment

与设备交互

$ sudo kubectl exec -it nginx -- bash

$ curl http://my-thermometer.deviceshifu.svc.cluster.local/temperature
$ curl http://my-thermometer.deviceshifu.svc.cluster.local/humidity
$ curl http://my-led.deviceshifu.svc.cluster.local/setfloat?value=23.4

应用开发

将温度和湿度读取,然后在LED上面间歇显示出来。

$ sudo docker build -t yangxijie/connection:v0.0.1 .
$ sudo docker images | grep connection
yangxijie/connection  v0.0.1  a9526147ddad  2 minutes ago  125MB
$ sudo kind load docker-image yangxijie/connection:v0.0.1
$ sudo kubectl run --image=yangxijie/connection:v0.0.1 connection-name

西门子PLC

接入设备

$ sudo docker pull edgehub/deviceshifu-http-http:v0.1.1
$ sudo docker pull edgehub/plc-device:v0.0.1
$ sudo kind load docker-image edgehub/deviceshifu-http-http:v0.1.1 edgehub/plc-device:v0.0.1
$ sudo kubectl apply -f examples/my_plc/plc-deployment

与设备交互

$ sudo kubectl run nginx --image=nginx:1.21 -n deviceshifu 
$ sudo kubectl exec -it nginx -n deviceshifu -- bash

$ curl "deviceshifu-plc/sendsinglebit?rootaddress=Q&address=0&start=0&digit=1&value=1"; echo

海康威视摄像头

接入设备

获取摄像头ip地址,修改rtsp/camera-deployment/deviceshifu-camera-deployment.yaml中的ip地址

$ sudo docker pull edgehub/deviceshifu-http-http:v0.1.1
$ sudo docker pull edgehub/camera-python:v0.0.1
$ sudo kind load docker-image edgehub/deviceshifu-http-http:v0.1.1 edgehub/camera-python:v0.0.1
$ sudo kubectl apply -f examples/my_rtsp/camera-deployment

与设备交互

# 集群中使用curl交互
$ sudo kubectl exec -it nginx -- bash

$ curl http://deviceshifu-camera.deviceshifu.svc.cluster.local/info

# 本机浏览器访问
$ sudo kubectl port-forward svc/deviceshifu-camera -n deviceshifu 8080:80
# 输入`localhost:8080/info`查看信息
# 输入`localhost:8080/capture`获取图片
# 输入`localhost:8080/move/{up|down|left|right}`操控相机位置
# 输入`localhost:8080/stream?timeout=0`获取实时视频流

本文由边无际授权发布

你可能感兴趣的:(物联网,golang,软件架构)