如何在 CentOS 7 上使用 Docker 安装和配置 Prometheus
本文将介绍如何安装 Prometheus 服务器以收集和查询指标,以及如何安装 Grafana——一个基于 Web 的图形化仪表盘构建器。Prometheus 是一款开源的监控工具,带有时间序列数据库。它涵盖了监控和生成指标及图表集合的诸多方面,以便在仪表板上显示数据并发出警报。

前提条件
要完成本文,我们需要以下资源:已安装 CentOS 7、具有 sudo 权限的用户以及已安装 Docker。
安装 Prometheus
我们将使用 Docker 安装 Prometheus,因此请确保已在 CentOS 7 机器上安装 Docker。Prometheus 组件的 Docker 容器镜像位于 Docker Hub 的 prom 组织下。我们可以使用位于 `/etc/Prometheus/Prometheus.yml` 的演示配置,无需任何选项即可启动 Docker 镜像。
我们有多种方法可以覆盖默认配置文件。可以将主机系统中的自定义配置文件作为 Docker 数据卷导入容器,或者选择使用我们自己的配置构建 Docker 容器。这里为了演示,我们将选择从主机系统传递配置文件。
创建最小 Prometheus 配置
$ nano ~/prometheus.yml # A scrape configuration scraping a Node Exporter and the Prometheus server # itself. scrape_configs: # Scrape Prometheus itself every 5 seconds. - job_name: 'prometheus' scrape_interval: 5s target_groups: - targets: ['localhost:9090'] # Scrape the Node Exporter every 5 seconds. - job_name: 'node' scrape_interval: 5s target_groups: - targets: ['192.168.1.100:9100']
使用以下命令,通过此外部配置文件启动 Prometheus Docker 容器
$ docker run -d -p 9090:9090 -v ~/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus -config.file=/etc/prometheus/prometheus.yml -storage.local.path=/prometheus -storage.local.memory-chunks=10000
此命令很长,一行中包含多个命令和选项。
我们将逐一查看上述命令中使用的每个选项。
The -d option starts the Prometheus container to start in detached mode, meaning that the container will be started in the background and will not be terminated by pressing CTRL+C. The -p 9090:9090 option allows the Prometheus's web port (9090) and makes it reachable via the external IP address of the host system. The -v [...] option mounts the prometheus.yml configuration file from the host filesystem from the location within the container where Prometheus expects it (/etc/prometheus/prometheus.yml) The -config.file option is instruct the Dockers accordingly to the location of the Prometheus configuration file within the container. The -storage.local.path option configures the metrics storage location within the container. Finally, the -storage.local.memory-chunks option adjusts Prometheus's memory usage to the host system's very small amount of RAM (only 512MB) and small number of stored time series in this tutorial (just under 1000) It instructs Prometheus to keep only 10000 sample chunks in memory (roughly 10 chunks per series), instead of the default of 1048576. This is a value you will definitely need to tune when running Prometheus on a machine with more RAM and when storing more time serie Refer to Prometheus's storage documentation for detailed usage.
由于这是我们第一次运行该命令,它将从 Docker Hub 下载内容。
我们可以使用 `docker ps` 命令查看本地计算机上可用的 Docker 容器。
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 55e31548c9a0 prom/prometheus "/bin/prometheus -con" 26 seconds ago Up 24 seconds 0.0.0.0:9090->9090/tcp cocky_fermat我们可以使用从 docker ps 命令获取的容器 ID 来查看 Prometheus 服务器的日志,以下是查看日志的命令。
$ docker logs 55e31548c9a0 time="2016-07-11T19:56:44Z" level=info msg="Starting prometheus (version=0.20.0, branch=master, revision=aeab25c)" source="main.go:73" time="2016-07-11T19:56:44Z" level=info msg="Build context (go=go1.6.2, user=root@77050118f904, date=20160616-08:38:14)" source="main.go:74" time="2016-07-11T19:56:44Z" level=info msg="Loading configuration file /etc/prometheus/prometheus.yml" source="main.go:206" time="2016-07-11T19:56:44Z" level=warning msg="The 'target_groups' option for scrape configurations is deprecated, use 'static_configs' instead" source="config.go:468" time="2016-07-11T19:56:44Z" level=warning msg="The 'target_groups' option for scrape configurations is deprecated, use 'static_configs' instead" source="config.go:468" time="2016-07-11T19:56:44Z" level=info msg="Loading series map and head chunks..." source="storage.go:341" time="2016-07-11T19:56:44Z" level=info msg="0 series loaded." source="storage.go:346" time="2016-07-11T19:56:44Z" level=info msg="Starting target manager..." source="targetmanager.go:74" time="2016-07-11T19:56:44Z" level=warning msg="No AlertManagers configured, not dispatching any alerts" source="notifier.go:174" time="2016-07-11T19:56:44Z" level=info msg="Listening on :9090" source="web.go:241"
要查找已存储的存储卷,我们可以运行以下命令
$ docker inspect 55e31548c9a0
[
{
"Id": "55e31548c9a0273bd91340bf08f5eb55996c8fe9a648b819b348d1a5af08990b",
"Created": "2016-07-11T19:56:43.814310728Z",
"Path": "/bin/prometheus",
"Args": [
"-config.file=/etc/prometheus/prometheus.yml",
"-storage.local.path=/prometheus",
"-storage.local.memory-chunks=10000"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4803,
"ExitCode": 0,
…
…
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4bbd72d2b2defe67b5223fe7f89919b9dd952e13f1d273ac871e8af1486b92c4",
"EndpointID": "b35f6fe49c02b8a6593e4e8549f1ab46a6bdc3b94f0279315f49a30725832e74",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
...
...
We can find that in the section
"Mounts": [
{
"Source": "/root/prometheus.yml",
"Destination": "/etc/prometheus/prometheus.yml",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Name": "1dd33b889f2fe804ceceb90d4100767a14a21b09ba880972f25a2bf452d31150",
"Source": "/var/lib/docker/volumes/1dd33b889f2fe804ceceb90d4100767a14a21b09ba880972f25a2bf452d31150/_data",
"Destination": "/prometheus",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
...
...
从输出结果可以看出,指标存储在以下位置:
/var/lib/docker/volumes/1dd33b889f2fe804ceceb90d4100767 a14a21b09ba880972f25a2bf452d31150/_data
登录 Prometheus 控制面板
该目录已自动创建并映射到容器中的 /Prometheus 目录。
可以看到,容器已启动并运行,现在可以通过 http://your_ip:9090/ 或 http://192.168.1.226:9090/status 查看 Prometheus 服务器状态(注意:此示例中的 IP 地址可能与您的实际配置不同,请使用您的主机 IP 地址)。

我们需要点击状态,然后点击目标。我们可以看到节点状态为"不健康",并且节点导出器尚未启动。

设置节点导出器
在这里我们将安装 Prometheus 节点导出器。节点导出器是一个服务器,它会公开主机运行的 Prometheus 指标,并显示主机的文件系统、网络设备、处理器、内存使用情况等信息。
由于我们需要使用 Docker 管理所有进程,我们将采用变通方法,通过 Docker 提供近似的指标。
要在端口 9100 上启动 Node Exporter,请使用以下命令(通过 Docker)。
$ docker run -d -p 9100:9100 -v "/proc:/host/proc" -v "/sys:/host/sys" -v "/:/rootfs" --net="host" prom/node-exporter -collector.procfs /host/proc -collector.sysfs /host/proc -collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)" Unable to find image 'prom/node-exporter:latest' locally latest: Pulling from prom/node-exporter 385e281300cc: Already exists a3ed95caeb02: Pull complete e418e02f5f37: Already exists 061d745f0f4c: Pull complete Digest: sha256:a2ae701df7f3ace9f60b890415db9310dd8c28b5446002c4e8ebd102242d076e Status: Downloaded newer image for prom/node-exporter:latest 49c08ad640f10556fedc517532386d11fbf9a9b49fcf4c14c463ef5dab4a0737
节点导出器使用的选项如下
As most of the metircs are gathered from /proc and /sys from the Linux file system, and the /host dircotory using the Docker’s –v option -collector.procfs and –collector.sysfs. They are used to instruct the Node Exporter to look for the /proc and /sys folders for a non-standard location Dockers –v flag is used to mount the entire root (/) file system into the contain as (/rootfs) -collector.filesystem.ignored-mount-points is used to ignore any other filesystem container which are belong to the host system. --net=host is used to place the container in the same network as the host so that it can read the /proc/net/dev
正如我们所见,Prometheus 服务器将自动开始抓取 Node Exporter 的数据。
http://192.168.1.226:9090/status
http://192.168.1.226:9100/metrics

设置 Grafana
Prometheus 服务器安装完毕后,我们现在将设置 Grafana。 Grafana 是一个图形界面,它使用 Promethus 作为后端来查询数据并生成图表。
Grafana 将数据存储在基于 SQL 的数据库中,支持 SQLite3、MySQL 和 PostgreSQL。本教程使用 SQLite3 数据库进行数据存储。
使用管理员密码以 Docker 容器的形式启动 Grafana
$ docker run -d -p 3000:3000 -e "GF_SECURITY_ADMIN_PASSWORD=password" -v ~/grafana_db:/var/lib/grafana grafana/grafana Unable to find image 'grafana/grafana:latest' locally latest: Pulling from grafana/grafana 5c90d4a2d1a8: Pull complete b1a9a0b6158e: Pull complete acb23b0d58de: Pull complete Digest: sha256:34ca2f9c7986cb2d115eea373083f7150a2b9b753210546d14477e2276074ae1 Status: Downloaded newer image for grafana/grafana:latest 5a2041049010fa0be8b23906cb2d864de7ff9a71e4930f686bc07eff24667d5a
上述命令将从 Docker Hub 下载 Grafana Docker 镜像,并将其存储在本地系统的 ~/grafana_db 目录下。这将自动在 /var/lib/grafana/grafana.db 创建并初始化 SQLite3 数据库。
The –e option will pass the environment variables to process the Docker container and GF-SERCURITY_ADMIN_PASSWORD variable to the desired dashboard administrator password for admin user. The Grafana is currently running on port 3000
要登录 Grafana,请打开链接 http://192.168.1.226:3000 。 IP 地址可能因系统而异。

由于尚未添加监控系统,我们将从 Grafana 的数据源图标中添加 http://192.168.1.226:9090,并添加 Prometheus 服务器。

由于我们已使用 Docker 配置了 Prometheus 服务器、Node Exporter 和 Grafana,它们都运行在同一台机器上,这仅用于演示目的。您可以从 Prometheus 和 Grafana 的文档中了解更多信息。
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:/linux/