title: 快速入门 description: 在本演练中,我们将部署 Octopus 并通过其管理一类虚拟设备并执行以下任务:使用 k3d 搭建 k3s 集群、部署 Octopus、部署设备模型和设备控制器、创建 DeviceLink、管理设备。 keywords:

  • Octopus中文文档
  • Octopus 中文文档
  • 边缘计算
  • IOT
  • edge computing
  • Octopus中文
  • Octopus 中文
  • Octopus
  • Octopus教程
  • Octopus中国
  • rancher
  • Octopus 中文教程
  • 快速入门

前置条件

已有 k3s 集群或 Kubernetes 集群。

使用步骤

在本演练中,我们将部署 Octopus 并通过其管理一类虚拟设备并执行以下任务:

  1. 使用 k3d 搭建 k3s 集群
  2. 部署 Octopus
  3. 部署设备模型和设备控制器
  4. 创建 DeviceLink
  5. 管理设备

1. 使用 k3d 搭建 k3s 集群(可选)

k3d是快速搭建容器化 k3s 集群的工具。 您可以使用 Docker 在单台计算机上启动多节点 k3s 集群。如果您已有 k3 集群或 Kubernetes 集群,请跳过此步骤。

  1. 运行以下指令,启动具有 3 个 worker 节点的本地 k3s 集群。

    ```shell script curl -fL https://octopus-assets.oss-cn-beijing.aliyuncs.com/k3d/cluster-k3s-spinup.sh | bash -

    1. :::note 说明
    2. 如果安装成功,则应该看到以下日志,请使用`CTRL+C`键以停止本地集群。
    3. :::
    4. ```logs
    5. [INFO] [0604 17:09:41] creating edge cluster with v1.17.2
    6. INFO[0000] Created cluster network with ID d5fcd8f2a5951d9ef4dba873f57dd7984f25cf81ab51776c8bac88c559c2d363
    7. INFO[0000] Created docker volume k3d-edge-images
    8. INFO[0000] Creating cluster [edge]
    9. INFO[0000] Creating server using docker.io/rancher/k3s:v1.17.2-k3s1...
    10. INFO[0008] SUCCESS: created cluster [edge]
    11. INFO[0008] You can now use the cluster with:
    12. export KUBECONFIG="$(k3d get-kubeconfig --name='edge')"
    13. kubectl cluster-info
    14. [WARN] [0604 17:09:50] default kubeconfig has been backup in /Users/guangbochen/.kube/rancher-k3s.yaml_k3d_bak
    15. [INFO] [0604 17:09:50] edge cluster's kubeconfig wrote in /Users/guangbochen/.kube/rancher-k3s.yaml now
    16. [INFO] [0604 17:09:50] waiting node edge-control-plane for ready
    17. INFO[0000] Adding 1 agent-nodes to k3d cluster edge...
    18. INFO[0000] Created agent-node with ID 3197e431b1a060fbb591b4c315c4949f1b472213312ff8e04c898e3353e05bdc
    19. [INFO] [0604 17:10:01] waiting node edge-worker for ready
    20. INFO[0000] Adding 1 agent-nodes to k3d cluster edge...
    21. INFO[0000] Created agent-node with ID d9bb3e589e745797f3b189962d14de77cfc6afe86d1b6af93a43d808a9c72b5c
    22. [INFO] [0604 17:10:13] waiting node edge-worker1 for ready
    23. INFO[0000] Adding 1 agent-nodes to k3d cluster edge...
    24. INFO[0000] Created agent-node with ID bc69aa9867aa2081df0cf425661ae002142bd667d3d618bc5a5b34bc092d7562
    25. [INFO] [0604 17:10:25] waiting node edge-worker2 for ready
    26. [WARN] [0604 17:10:37] please input CTRL+C to stop the local cluster
  2. 打开一个新终端,并配置KUBECONFIG以访问本地 k3s 集群。 shell script export KUBECONFIG="$(k3d get-kubeconfig --name='edge')"

  3. 运行kubectl get node命令, 检查本地 k3s 集群的节点是否正常。 shell script kubectl get node NAME STATUS ROLES AGE VERSION edge-control-plane Ready master 3m46s v1.17.2+k3s1 edge-worker2 Ready <none> 3m8s v1.17.2+k3s1 edge-worker Ready <none> 3m33s v1.17.2+k3s1 edge-worker1 Ready <none> 3m21s v1.17.2+k3s1

2. 部署 Octopus

两种部署 Octopus 的方法,为方便起见,我们将通过一份 all-in-one的 YAML 文件来部署。 安装程序 YAML 文件位于 Github 上的deploy/e2e目录下:

```shell script kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/deploy/e2e/all_in_one.yaml

  1. 预期结果:
  2. ```log
  3. namespace/octopus-system created
  4. customresourcedefinition.apiextensions.k8s.io/devicelinks.edge.cattle.io created
  5. role.rbac.authorization.k8s.io/octopus-leader-election-role created
  6. clusterrole.rbac.authorization.k8s.io/octopus-manager-role created
  7. rolebinding.rbac.authorization.k8s.io/octopus-leader-election-rolebinding created
  8. clusterrolebinding.rbac.authorization.k8s.io/octopus-manager-rolebinding created
  9. service/octopus-brain created
  10. service/octopus-limb created
  11. deployment.apps/octopus-brain created
  12. daemonset.apps/octopus-limb created

安装后,我们可以验证 Octopus 的状态,如下所示:

```shell script kubectl get all -n octopus-system NAME READY STATUS RESTARTS AGE pod/octopus-limb-w8vcf 1/1 Running 0 14s pod/octopus-limb-862kh 1/1 Running 0 14s pod/octopus-limb-797d8 1/1 Running 0 14s pod/octopus-limb-8w462 1/1 Running 0 14s pod/octopus-brain-65fdb4ff99-zvw62 1/1 Running 0 14s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/octopus-brain ClusterIP 10.43.92.81 8080/TCP 14s service/octopus-limb ClusterIP 10.43.143.49 8080/TCP 14s

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/octopus-limb 4 4 4 4 4 14s

NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/octopus-brain 1/1 1 1 14s

NAME DESIRED CURRENT READY AGE replicaset.apps/octopus-brain-65fdb4ff99 1 1 1 14s

  1. ### 3. 部署设备模型和设备控制器
  2. 接下来我们会使用设备模拟器进行测试(不需要将其连接到真实的物理设备)。
  3. 首先,我们需要将设备描述为 Kubernetes 中的一种资源。 此描述过程即为对设备进行建模。 Kubernetes 中,描述资源的最佳方法是使用[CustomResourceDefinitions](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#customresourcedefinitions),因此**定义 Octopus 的设备模型实际上是在定义 CustomResourceDefinition**, 可快速浏览一下下列的`DummySpecialDevice`模型(假设这是一个智能风扇):
  4. :::note 说明
  5. 下列 YAML 可通过[code-generator](https://github.com/kubernetes/code-generator)动态生成,无需手动编辑。
  6. :::
  7. ```yaml
  8. apiVersion: apiextensions.k8s.io/v1
  9. kind: CustomResourceDefinition
  10. metadata:
  11. annotations:
  12. controller-gen.kubebuilder.io/version: v0.2.5
  13. devices.edge.cattle.io/description: dummy device description
  14. devices.edge.cattle.io/device-property: ""
  15. devices.edge.cattle.io/enable: "true"
  16. devices.edge.cattle.io/icon: ""
  17. labels:
  18. app.kubernetes.io/name: octopus-adaptor-dummy
  19. app.kubernetes.io/version: master
  20. name: dummyspecialdevices.devices.edge.cattle.io
  21. spec:
  22. group: devices.edge.cattle.io
  23. names:
  24. kind: DummySpecialDevice
  25. listKind: DummySpecialDeviceList
  26. plural: dummyspecialdevices
  27. singular: dummyspecialdevice
  28. scope: Namespaced
  29. versions:
  30. - name: v1alpha1
  31. schema:
  32. openAPIV3Schema:
  33. description: DummySpecialDevice is the Schema for the dummy special device
  34. API.
  35. properties:
  36. ...
  37. spec:
  38. description: DummySpecialDeviceSpec defines the desired state of DummySpecialDevice.
  39. properties:
  40. gear:
  41. description: Specifies how fast the dummy special device should be.
  42. enum:
  43. - slow
  44. - middle
  45. - fast
  46. type: string
  47. "on":
  48. description: Turn on the dummy special device.
  49. type: boolean
  50. protocol:
  51. description: Protocol for accessing the dummy special device.
  52. properties:
  53. location:
  54. type: string
  55. required:
  56. - location
  57. type: object
  58. required:
  59. - "on"
  60. - protocol
  61. type: object
  62. status:
  63. description: DummySpecialDeviceStatus defines the observed state of DummySpecialDevice.
  64. properties:
  65. gear:
  66. description: Reports the current gear of dummy special device.
  67. enum:
  68. - slow
  69. - middle
  70. - fast
  71. type: string
  72. rotatingSpeed:
  73. description: Reports the detail number of speed of dummy special device.
  74. format: int32
  75. type: integer
  76. type: object
  77. type: object
  78. ...
  79. status:
  80. ...

虚拟设备适配器(Dummy Adaptor)的安装 YAML 文件位于adaptors/dummy/deploy/e2e目录下,即 all_in_one.yaml, 它包含了设备模型和设备适配器,我们可以通过以下指令将其直接部署到 k3s 集群中:

```shell script kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/adaptors/dummy/deploy/e2e/all_in_one.yaml

  1. 预期结果:

customresourcedefinition.apiextensions.k8s.io/dummyspecialdevices.devices.edge.cattle.io created customresourcedefinition.apiextensions.k8s.io/dummyprotocoldevices.devices.edge.cattle.io created clusterrole.rbac.authorization.k8s.io/octopus-adaptor-dummy-manager-role created clusterrolebinding.rbac.authorization.k8s.io/octopus-adaptor-dummy-manager-rolebinding created daemonset.apps/octopus-adaptor-dummy-adaptor created

kubectl get all -n octopus-system NAME READY STATUS RESTARTS AGE pod/octopus-limb-w8vcf 1/1 Running 0 2m27s pod/octopus-limb-862kh 1/1 Running 0 2m27s pod/octopus-limb-797d8 1/1 Running 0 2m27s pod/octopus-limb-8w462 1/1 Running 0 2m27s pod/octopus-brain-65fdb4ff99-zvw62 1/1 Running 0 2m27s pod/octopus-adaptor-dummy-adaptor-6xcdz 1/1 Running 0 21s pod/octopus-adaptor-dummy-adaptor-mmk5l 1/1 Running 0 21s pod/octopus-adaptor-dummy-adaptor-xnjrf 1/1 Running 0 21s pod/octopus-adaptor-dummy-adaptor-srsjz 1/1 Running 0 21s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/octopus-brain ClusterIP 10.43.92.81 8080/TCP 2m27s service/octopus-limb ClusterIP 10.43.143.49 8080/TCP 2m27s

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/octopus-limb 4 4 4 4 4 2m27s daemonset.apps/octopus-adaptor-dummy-adaptor 4 4 4 4 4 21s

NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/octopus-brain 1/1 1 1 2m27s

NAME DESIRED CURRENT READY AGE replicaset.apps/octopus-brain-65fdb4ff99 1 1 1 2m27s

  1. 请注意,还需要授予 Octopus 管理 `DummySpecialDevice`/`DummyProtocolDevice`的权限:
  2. ```shell script
  3. $ kubectl get clusterrolebinding | grep octopus
  4. octopus-manager-rolebinding 2m49s
  5. octopus-adaptor-dummy-manager-rolebinding 43s

4. 创建 DeviceLink

前面我们提到过 DeviceLink 是 Octopus 自定义的一个 k8s 资源对象(简称 dl),用户可通过编辑 DeviceLink 的 YAML 文件来进行配置与和管理设备连接。

接下来,我们将通过 DeviceLink YAML 来连接一个虚拟设备。 DeviceLink 由 3 部分组成:Adaptor、Model 和 Device spec。

  • Adaptor - 适配器定义了要使用的适配器(即协议)以及实际设备应连接的节点。
  • Model - 模型描述了设备的模型,它是设备模型的TypeMeta CRD。
  • Device Spec - 设备参数描述了如何连接到设备及其所需的设备属性或状态,这些参数由设备模型的 CRD 来定义。

假设有一个名为 living-room-fan 的设备可以通过 edge-worker 节点连接,我们可以使用以下 YAML 来测试其工作方式。

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: edge.cattle.io/v1alpha1
  3. kind: DeviceLink
  4. metadata:
  5. name: living-room-fan
  6. namespace: default
  7. spec:
  8. adaptor:
  9. node: edge-worker # select the node that the device will be connect to
  10. name: adaptors.edge.cattle.io/dummy
  11. model:
  12. apiVersion: "devices.edge.cattle.io/v1alpha1"
  13. kind: "DummySpecialDevice"
  14. template:
  15. metadata:
  16. labels:
  17. device: living-room-fan
  18. spec: # specify device specs
  19. protocol:
  20. location: "living_room"
  21. gear: slow
  22. "on": true
  23. EOF

DeviceLink 包含了几种状态,如果我们发现其PHASEDeviceConnectedSTATUSHealthy的状态下,我们就可以使用设备模型的 CRD 对象来查询其状态(即此处的 dummyspecialdevice):

```shell script kubectl get devicelink living-room-fan -n default NAME KIND NODE ADAPTOR PHASE STATUS AGE living-room-fan DummySpecialDevice edge-worker adaptors.edge.cattle.io/dummy DeviceConnected Healthy 10s

  1. 查看虚拟设备上报的状态或信息:
  2. ```shell script
  3. kubectl get dummyspecialdevice living-room-fan -n default -w
  4. NAME GEAR SPEED AGE
  5. living-room-fan slow 10 32s
  6. living-room-fan slow 11 33s
  7. living-room-fan slow 12 36s

5. 管理设备

用户可以使用修改设备属性来管理其设备,例如,假设我们要关闭风扇,可以将其on(开关属性)配置设置为 "on":false

```shell script kubectl patch devicelink living-room-fan -n default —type merge —patch ‘{“spec”:{“template”:{“spec”:{“on”:false}}}}’

  1. 日志显示 `devicelink.edge.cattle.io/living-room-fan is patched`,查询其状态,`GEAR``SPEED`值均显示为空值(表示已关闭)。

kubectl get devicelink living-room-fan -n default NAME KIND NODE ADAPTOR PHASE STATUS AGE living-room-fan DummySpecialDevice edge-worker adaptors.edge.cattle.io/dummy DeviceConnected Healthy 89s

kubectl get dummyspecialdevice living-room-fan -n default NAME GEAR SPEED AGE living-room-fan 117s ```