65.9K
CodeProject 正在变化。 阅读更多。
Home

IoT Starter Raspberry Pi Compose

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2018年2月13日

CPOL

3分钟阅读

viewsIcon

14231

在树莓派上使用 Linux 和 .NET Core Compose 微服务,立即启动物联网计划

引言

该项目基于 API First Design 开发微服务,目标是树莓派和 Linux。 为了加速物联网计划,IoT 启动器开发了一个可定制的、随时可运行的Thing设备。 以下列出的章节提供了全面的路径

  1. IoT.Starter.Pi.Core:在该项目中,针对树莓派和 Linux,使用 API First Design 策略开发一个由 SwaggerHub 自动生成的 ASP.NET Core Web 服务器。

  2. IoT.Starter.Pi.Thing:IoT Starter Pi Thing 包含任何Thing设备需要包含的基本内容,这意味着未来的项目预计将从这里开始。

  3. IoT.Starter.Pi.Lirc:此任务为IoT.Starter.Pi.Thing提供红外 (IR) 输出功能。

  4. IoT.Starter.Pi.Lumi:作为第三部分的补充,API 和相应的 Web 服务被扩展,以考虑红外遥控器及其各自的代码。

正如我们在之前的章节中体验到的,每次 API 更改后,SwaggerHub 都会自动为home-web生成更新的代码,其中包含相应的 Web 服务。Thing设备还具有home-uinginx-proxy镜像,分别处理用户界面和 SSL 安全性。 它们都在单独的 Docker 容器中运行,协同工作并通过树莓派、Linux 和 .NET Core 共享公共资源。

该解决方案在配备 Windows 10 和 Visual Studio 2017 的快速 x64 机器上构建得很快且高效。 docker-compose 的build命令遵循每个dockerfile的步骤,生成以下镜像,最终将其推送到 DockerHub 注册表

在树莓派端,我们体验到手动运行每个容器仍然有点耗时。现在的任务是使用 docker-composer 的up命令将IoT.Starter.Pi.Thing更进一步。 相同的 *docker-compose.yml* 文件用于编排 x64 机器上的build阶段,也被重用于加载镜像、创建容器、建立关系,并在树莓派端运行它们。

安装 docker-composer

请注意,仅安装 docker 无法在 RPI 上运行docker-compose,如下所示

pi@zuni:~ $ docker version
Client:
 Version:       18.02.0-ce
 API version:   1.36
 Go version:    go1.9.3
 Git commit:    fc4de44
 Built: Wed Feb  7 21:24:08 2018
 OS/Arch:       linux/arm
 Experimental:  false
 Orchestrator:  swarm

Server:
 Engine:
  Version:      18.02.0-ce
  API version:  1.36 (minimum version 1.12)
  Go version:   go1.9.3
  Git commit:   fc4de44
  Built:        Wed Feb  7 21:20:13 2018
  OS/Arch:      linux/arm
  Experimental: false

pi@zuni:~ $ docker-compose version
-bash: docker-compose: command not found

在 RPI 上安装docker-compose的最简单方法是使用命令

sudo apt-get install docker-compose

根据下面的日志,此路径导致在 RPI 上安装docker-compose版本 1.8.0

pi@zuni:~ $ sudo apt-get install docker-compose
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libyaml-0-2 python-backports.ssl-match-hostname python-cached-property 
  python-cffi-backend python-chardet python-cryptography python-docker 
  python-dockerpty python-docopt

...

The following NEW packages will be installed:
  docker-compose libyaml-0-2 python-backports.ssl-match-hostname 
  python-cached-property python-cffi-backend python-chardet 
  python-cryptography python-docker python-dockerpty
  python-docopt python-enum34 python-funcsigs python-functools32 
  python-idna python-ipaddress python-jsonschema python-mock 
  python-openssl python-pbr python-pkg-resources
  python-pyasn1 python-requests python-setuptools python-six 
  python-texttable python-urllib3 python-websocket python-yaml python3-pkg-resources
0 upgraded, 29 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,860 kB of archives.
After this operation, 8,564 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

...  

Setting up python-openssl (16.2.0-1) ...
Setting up python-docker (1.9.0-1) ...
Setting up docker-compose (1.8.0-2) ...

pi@zuni:~ $ docker-compose version
docker-compose version 1.8.0, build unknown
docker-py version: 1.9.0
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.1.0f  25 May 2017

不幸的是,这个旧版本与最新的 RPI 二进制文件存在问题,并且不可靠。 如果它已经安装,您可以将其删除,如下所示

pi@zuni:~ $ sudo apt-get remove docker-compose
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libyaml-0-2 python-backports.ssl-match-hostname python-cached-property 
  python-cffi-backend python-chardet python-cryptography python-docker 
  python-dockerpty python-docopt
  python-enum34 python-funcsigs python-functools32 python-idna 
  python-ipaddress python-jsonschema python-mock python-openssl 
  python-pbr python-pkg-resources python-pyasn1
  python-requests python-setuptools python-six python-texttable 
  python-urllib3 python-websocket python-yaml python3-pkg-resources
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  docker-compose
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 403 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 35540 files and directories currently installed.)
Removing docker-compose (1.8.0-2) ...
Processing triggers for man-db (2.7.6.1-2) ...

考虑到确实需要docker-compose,JC Berthon 发表了一篇文章,描述了一种“简单方法”来自己构建它。 我在 RPI 2 model B 上成功地尝试了它,经过长时间的运行,最新的docker-compose在 RPI 上构建完成。 生成的二进制文件被添加到项目存储库中的 *home/Docker* 文件夹中。

下一步是将新的docker-compose二进制文件复制到*/usr/local/bin* 并调整其设置,如下所示

$ cp docker-compose /usr/local/bin
$ chown root:root /usr/local/bin/docker-compose
$ chmod 0755 /usr/local/bin/docker-compose

$ ls -l /usr/local/bin
total 7052
-rwxr-xr-x 1 root root 7218664 Feb 12 19:06 docker-compose

最后,确认已安装的docker-compose版本

pi@lumi:~ $ docker-compose version
docker-compose version 1.19.0-rc2, build dfcb02c
docker-py version: 2.7.0
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016

一个已知的问题是,这个相同的二进制文件与 RPI Zero 一起返回错误。 需要进一步检查才能弄清楚在这种情况下发生了什么。

使用 docker-compose 的操作

正如 Viktor Adam 在他的教程中所示,Git 存储库中的 *docker-compose.yml* 文件允许一个基本的自动化,以便在每次更改时更新微服务。

$ cd /to/your/cloned/folder
$ git pull
$ docker-compose pull
$ docker-compose up -d

IoT.Starter.Pi.Thing存储库被克隆到 *RPI* 文件夹中,如下所示

git clone git@github.com:josemotta/IoT.Starter.Pi.Thing.git

现在,使用 docker-compose 的pull从 DockerHub 获取最新的镜像非常简单

docker-compose -f lumi-compose.yml pull

请在下面的实际操作中看到,所有三个镜像都已更新到存储在 DockerHub 上的latest版本

root@lumi:~/IoT.Starter.Pi.Thing/home# docker-compose -f lumi-compose.yml pull
Pulling home.ui (josemottalopes/home-ui:latest)...
latest: Pulling from josemottalopes/home-ui
0d9fbbfaa2cd: Already exists
b015fdc7d33a: Already exists
60aaa226f085: Already exists
01963091a185: Already exists
63ffb7955a88: Pull complete
b0ac7ac16eca: Pull complete
Digest: sha256:784b490276b9402cd276f2c211223041779983ac2fdc8ff25b775df6717028cb
Status: Downloaded newer image for josemottalopes/home-ui:latest
Pulling io.swagger (josemottalopes/home-web-ir:latest)...
latest: Pulling from josemottalopes/home-web-ir
0d9fbbfaa2cd: Already exists
b015fdc7d33a: Already exists
60aaa226f085: Already exists
01963091a185: Already exists
f5f67e021814: Pull complete
b640e21d6d61: Pull complete
b81fd5b12fb6: Pull complete
b0409530900f: Pull complete
6e417e6af42e: Pull complete
db68a213ab98: Pull complete
9d099602a010: Pull complete
0ff2feee3ab1: Pull complete
Digest: sha256:b378ca3b49b49e9e14111cf165eaf182ffd97b8f8c8720722cebde13a2ed13e8
Status: Downloaded newer image for josemottalopes/home-web-ir:latest
Pulling ssl.proxy (josemottalopes/nginx-proxy:latest)...
latest: Pulling from josemottalopes/nginx-proxy
cd8b673adb84: Already exists
db611fab629f: Already exists
6fa9759daa34: Already exists
cff4b2a22797: Pull complete
1a7a23dc7a01: Pull complete
5bedaf887e96: Pull complete
54c0a006ff47: Pull complete
Digest: sha256:3a57fb01ca10858cbfc7f1b78e7fc4ea33d772a5d4ead67ff9dcbc84b24a417d
Status: Downloaded newer image for josemottalopes/nginx-proxy:latest

只需要再一个命令即可创建并启动所有三个容器

docker-compose -f lumi-compose.yml up -d

请参阅下面的日志

root@lumi:~/IoT.Starter.Pi.Thing/home# docker-compose -f lumi-compose.yml up -d
Creating home-web-ir ... done
Creating home-web-ir ...
Creating home-ui ...

root@lumi:~# docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                            NAMES
a45f1330c167        josemottalopes/home-web-ir   "dotnet IO.Swagger.d…"   23 minutes ago      Up 23 minutes       0.0.0.0:32779->5010/tcp          home-web-ir
24b675650f1a        josemottalopes/home-ui       "dotnet Home.UI.dll"     23 minutes ago      Up 23 minutes       0.0.0.0:32777->80/tcp            home-ui
dbb14cc24bf2        josemottalopes/nginx-proxy   "nginx -g 'daemon of…"   23 minutes ago      Up 23 minutes       80/tcp, 0.0.0.0:32778->443/tcp   ssl-proxy

停止并从内存中删除所有容器也很简单

docker-compose -f lumi-compose.yml down

结果如下所示,所有三个镜像都已停止并删除

root@lumi:~/IoT.Starter.Pi.Thing/home# docker-compose -f lumi-compose.yml down
Stopping home-web-ir ... done
Stopping home-ui     ... done
Stopping ssl-proxy   ... done
Removing home-web-ir ... done
Removing home-ui     ... done
Removing ssl-proxy   ... done

root@lumi:~/IoT.Starter.Pi.Thing/home# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

请注意,上面使用了 *lumi-compose.yml* 来处理 IoT 启动器的红外版本。 如果您不需要红外支持并且不需要安装 Lirc,请改用 *thing-compose.yml* 文件,并且相应的命令将是

# pull latest changes at DockerHub
docker-compose -f thing-compose.yml pull

# load and start all three docker images
docker-compose -f thing-compose.yml up -d

# stop and remove containers from memory
docker-compose -f thing-compose.yml down

结论

IoT.Starter.Pi.Thing 存储库已根据最新的改进进行了修订,并已准备好立即开始您的物联网计划。

玩得开心!

历史

© . All rights reserved.