树莓派上安装 docker


This article will guide you through creating a simple weekend project to read data from a DHT11 sensor and broadcast it to a Grafana instance, so you can visualize it and access it through your network from other devices.

本文将指导您创建一个简单的周末项目,以从DHT11传感器读取数据并将其广播到Grafana实例,以便您可以对其进行可视化并从其他设备通过网络进行访问。

(Summary)

For this project, we’ll need the following components:

对于此项目,我们需要以下组件:

(Hardware components)

  • Raspberry Pi 3B with Ubuntu Server 20.04 LTS
  • DHT11 module (3-pin version)

(Software components)

For this project, we’ll need Python 3, Flask webserver, Docker, Portainer, Prometheus, and Grafana.

对于此项目,我们需要Python 3Flask Web服务器, DockerPortainerPrometheusGrafana

(Structure)

Our project will be structured using a Raspberry Pi 3B running Ubuntu Server 20.04 as our host. We will plug the DHT11 to our GPIO interface and read from it using Python 3. While reading, we’ll broadcast our readings with a Flask web server to the port 5000. Using Docker, we’ll run Prometheus at port 5001 and Grafana at port 5002. Our data will flow through the pipeline, being collected, broadcasted, stored, and displayed on a modern and cool graphical interface.

我们的项目将使用运行Ubuntu Server 20.04的Raspberry Pi 3B作为主机来构建。 我们将DHT11插入我们的GPIO接口,并使用Python 3进行读取。在读取过程中,我们将通过Flask Web服务器将读数广播到端口5000。使用Docker,我们将在端口5001运行Prometheus,并在以下位置运行Grafana端口5002。我们的数据将通过管道流动,并在现代而凉爽的图形界面上进行收集,广播,存储和显示。

(First steps)

If you want to start afresh, flash your SD card with Ubuntu, and install the system. Now you should have a clean install with Python 3 in your Raspberry Pi. Do a ‘sudo apt-get update’ and a ‘sudo apt-get upgrade’ to be up to date with the latest software. Since our application will be accessible from our network, it should have a static IP address. Set it up using the following guide.

如果要重新启动,请用Ubuntu刷新SD卡,然后安装系统。 现在,您应该在Raspberry Pi中使用Python 3进行全新安装。 进行“ sudo apt-get更新”“ sudo apt-get升级”以使用最新软件。 由于可以从我们的网络访问我们的应用程序,因此它应该具有静态IP地址。 使用以下指南进行设置

(Installing DHT11 on Raspberry Pi)

First of all, let’s install the DHT 11 on our Raspberry Pi. If you’re using the module version, this is pretty simple. Hook up the 5V and GND to the GPIO of the Raspberry and choose an analog port for your data pin. The following schematic by Circuit Basics might help. If you’re using the 4-pin version, check this schematic.

首先,让我们在Raspberry Pi上安装DHT 11。 如果您使用的是模块版本,这非常简单。 将5V和GND连接至Raspberry的GPIO,并为您的数据引脚选择一个模拟端口。 以下由Circuit Basics提供的原理图可能会有所帮助。 如果您使用的是4针版本,请查看此原理图




树莓派docker配置 docker安装树莓派_python


Wiring schematic by Circuit Basics. 电路原理图的接线图

(Reading the DHT11 sensor)

As most tutorials state, you can use the Adafruit Python DHT library to easily connect and read from the sensor. You can follow the installation steps from the documentation above to install it in your python environment.

如大多数教程所述,您可以使用Adafruit Python DHT库轻松连接并从传感器读取数据。 您可以按照上述文档中的安装步骤进行操作,以将其安装在python环境中。

设置一个简单的Flask Web服务器 (Setting up a simple Flask webserver)

Since we want to read from our GPIO and be able to serve it to Grafana, we can use Prometheus. To send data to Prometheus, we can create a simple webserver using Flask. In the snippet below, we’ll read the data from our sensor (with PIN 2 from GPIO) and format it to Prometheus syntax whenever the endpoint “/metrics” is called (which it’s the default endpoint for Prometheus).

由于我们想读取GPIO并能够将其提供给Grafana ,因此我们可以使用Prometheus 。 要将数据发送到Prometheus,我们可以使用Flask创建一个简单的Web服务器。 在下面的代码段中,每当调用端点“ / metrics”(这是Prometheus的默认端点)时,我们将从传感器(使用GPIO的PIN 2)读取数据并将其格式化为Prometheus语法。

<script src=”https://gist.github.com/gmullernh/4b75e899120307f3872832f2f6260930.js"></script>

<script src =” https://gist.github.com/gmullernh/4b75e899120307f3872832f2f6260930.js “> </ script>

To run our webserver, run the following commands from your terminal:

要运行我们的网络服务器,请在您的终端上运行以下命令:

$ export FLASK_APP=app.py
$ flask run

More info on starting up a Flask app you can find here.

有关启动Flask应用程序的更多信息,请参见此处

(Optional)

If you want your service to run as a daemon task (a background task), you can create a shell script and add it to your services. Your script should be saved on a folder and set as executable by using the chmod command. After that add your service to the systemctl. For a step by step version, view this post.

如果希望您的服务作为守护程序任务(后台任务)运行,则可以创建一个Shell脚本并将其添加到您的服务中。 您的脚本应保存在文件夹中,并使用chmod命令设置为可执行文件。 之后,将您的服务添加到systemctl 。 有关逐步版本,请查看此文章

<script src=”https://gist.github.com/gmullernh/0c63dadce8840ce8a0230df88821aa6a.js"></script>

<script src =” https://gist.github.com/gmullernh/0c63dadce8840ce8a0230df88821aa6a.js “> </ script>

After all that, your service should be running as a daemon service, so if you reboot your system, it will start again automatically.

毕竟,您的服务应作为守护程序服务运行,因此,如果您重新启动系统,它将自动重新启动。

(It’s Docker time)

Since we want to use multiple services as Grafana and Prometheus, the easiest way is to use them through Docker. To install Docker, follow the instructions from the Docker documentation. Remember to use the ARM64 variant when needed. It’s a pretty straight forward process. At the end of the process, you should have a Docker installation ready to go.

由于我们要使用Grafana和Prometheus等多种服务,因此最简单的方法是通过Docker使用它们。 要安装Docker,请遵循Docker文档中的说明 。 请记住在需要时使用ARM64变体。 这是一个非常简单的过程。 在该过程的最后,您应该已准备好进行Docker安装。

(Setting up Portainer)

Managing containers through the command line is fine, but sometimes, using a web interface (especially on your local network) it’s way more awesome. Here comes Portainer to the rescue. Portainer is a simplified, but powerful, docker management layer, with a Web GUI. To install it, follow the instructions on its installation page.

通过命令行管理容器很好,但是有时候,使用Web界面(尤其是在您的本地网络上)会更加出色。 Portainer来了。 Portainer是具有Web GUI的简化但功能强大的docker管理层。 要安装它,请按照其安装页面上的说明进行操作。


树莓派docker配置 docker安装树莓派_Python_02

A screenshot of Portainer’s container management screen.

Portainer的容器管理屏幕的屏幕截图。

(Setting up Prometheus)

Since we’re using Docker and Portainer, spinning up Prometheus it’s easy. Prometheus allows us to scrap metric information from our webserver in a time-series fashion. Simply create a new container in the “Add Container” in Portainer and let’s create a new instance of Prometheus.

由于我们使用的是Docker和Portainer,因此简化Prometheus变得很容易。 Prometheus允许我们以时间序列的方式从Web服务器中删除指标信息。 只需在Portainer的“添加容器”中创建一个新容器,然后创建一个Prometheus新实例。


树莓派docker配置 docker安装树莓派_Web_03

Creating a new container on Portainer

在Portainer上创建一个新容器

Name your container and use Prometheus:latest as your image. You can change the default port for Prometheus (which is 9090) to something closer to our webserver port, like 5001 on our host (leave the container port as 9090). For more information about Prometheus on docker, you can check the docs.

为您的容器命名,并使用Prometheus:latest作为图像。 您可以将Prometheus的默认端口(即9090)更改为与我们的Web服务器端口更接近的端口,例如主机上的5001(将容器端口保留为9090)。 有关docker上Prometheus的更多信息,您可以检查docs

(The configuration file)

Prometheus uses a file called “prometheus.yml” to set its configurations and define the scrapped services. You can use this base configuration file and change the “your.raspberry.ip.address” with your Raspberry Pi IP Address and the webserver port, eg. 192.168.0.103:5000. For more on configuration, check Prometheus’ documentation.

Prometheus使用一个名为“ prometheus.yml”的文件来设置其配置并定义报废的服务。 您可以使用此基本配置文件,并使用Raspberry Pi IP地址和Web服务器端口(例如)更改“ your.raspberry.ip.address”192.168.0.103:5000 。 有关配置的更多信息,请参阅Prometheus的文档

<script src=”https://gist.github.com/gmullernh/a3f4b848ddf9d370b99b4c0789236572.js"></script>

<script src =” https://gist.github.com/gmullernh/a3f4b848ddf9d370b99b4c0789236572.js “> </ script>

Now save it on the host machine and bind it to our container, so it can be consumed by Prometheus.

现在将其保存在主机上并将其绑定到我们的容器,以便Prometheus可以使用它。


树莓派docker配置 docker安装树莓派_Python_04

Add a new bind to /etc/prometheus/prometheus.yml 向/etc/prometheus/prometheus.yml添加新的绑定


Now, click on “Deploy the container” and we should be ready to go.

现在,单击“ 部署容器 ”,我们应该准备好了。


树莓派docker配置 docker安装树莓派_Docker_05

Give Portainer a few minutes to spin the container up and it should appear as running on your Container panel.

给Portainer几分钟时间来旋转容器,它应该在“ 容器”面板上显示为正在运行。

(Setting up Grafana)

Now it’s time for Grafana. Let’s use the same strategy used for Prometheus. Create a new container on Portainer, using grafana/grafana:latest and map it to a port like 5002 on your host. Grafana doesn’t require a configuration file, so it’s pretty simple to start it up. Deploy the container and wait a few minutes. The container should appear as “Running” on your Portainer.

现在是Grafana的时候了。 让我们使用与普罗米修斯相同的策略。 使用grafana / grafana:latest在Portainer上创建一个新容器,并将其映射到主机上的端口5002。 Grafana不需要配置文件,因此启动它非常简单。 部署容器并等待几分钟。 该容器在Portainer上应显示为“正在运行”。


树莓派docker配置 docker安装树莓派_Web_06

Creating a Grafana container. 创建一个Grafana容器。

Visit http://your.raspberry.ip.address:5002 (change your.raspberry.ip.address with your Raspberry Pi IP Address) to access Grafana. You should be greeted with a login screen. Use “admin” for the username and password, and you will be in.

访问http:// your.raspberry.ip.address :5002(用Raspberry Pi IP地址更改your.raspberry.ip.address)以访问Grafana。 您应该会看到一个登录屏幕。 使用“ admin”作为用户名和密码

(Setting up the Data Source)

Now it’s time to set up our Prometheus data source. After entering in Grafana, on the sidebar, you will find a gear icon, inside it, you will find the Data Sources item. Click on it, and then on Add data source.

现在是时候设置我们的Prometheus数据源了。 在Grafana中输入后,在侧栏上将找到一个齿轮图标,在其内部,您将找到“ 数据源”项。 单击它,然后单击“ 添加数据源”


树莓派docker配置 docker安装树莓派_Python_07

Grafana’s screen. Grafana的屏幕。

Choose Prometheus and let’s configure it. Choose a name, enter the URL of your Prometheus instance (your Raspberry Pi IP address) and the port of the service. Click on Save and Test and if everything is working fine, you’ll see a green notification indicating success.

选择Prometheus并进行配置。 选择一个名称,输入Prometheus实例的URL(您的Raspberry Pi IP地址)和服务的端口。 单击“ 保存并测试” ,如果一切正常,您将看到绿色通知,指示成功。


树莓派docker配置 docker安装树莓派_Python_08

Data source configuration. 数据源配置。

(Creating the dashboard)

After we’ve set the data source, we can create a new dashboard, add a new panel, and add the measurements to it. Our first panel will be responsible for showing the current temperature using the field “local_temp”, as defined in our Flask webserver snippet.

设置数据源后,我们可以创建一个新的仪表板,添加一个新面板,并向其中添加度量。 我们的第一个面板将负责使用Flask网络服务器代码段中定义的字段“ local_temp ”显示当前温度。


树莓派docker配置 docker安装树莓派_Docker_09

Creating a new panel inside the dashboard. 在仪表板内创建一个新面板。

Our second panel will be responsible for showing the humidity level, by reading the field “local_humidity” set in our Flask snippet. After both panels are finished, you’ll be able to see the values changing over time.

我们的第二个面板将通过读取Flask代码片段中设置的“ local_humidity”字段来显示湿度水平。 两个面板完成后,您将可以看到这些值随时间变化。


树莓派docker配置 docker安装树莓派_Docker_10

Your simple dashboard with temperature and humidity. 您具有温度和湿度的简单仪表板。

(The first steps on IoT and Data Visualization)

This is a simple weekend project that may help you familiarize yourself with reading data from sensors and broadcasting it to data visualization tools over the internet. Much more can be improved over this project in terms of security, functionality, and reliability. I hope it gets you motivated to start tinkering with it and give me a shout in case of any doubts.

这是一个简单的周末项目,可以帮助您熟悉从传感器读取数据并将其通过Internet广播到数据可视化工具的过程。 在安全性,功能性和可靠性方面,可以对该项目进行很多改进。 我希望它能激发您开始修改它的动力,并在有任何疑问的情况下大声疾呼。


翻译自: https://levelup.gitconnected.com/setting-up-a-local-weather-station-using-dht11-docker-prometheus-and-grafana-on-a-raspberry-pi-a5f928addc34

树莓派上安装 docker