如何在 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/1dd33b889f2fe804ceceb90d4100767a14a21b09ba880972f25a2bf452d31150/_data”
登录 Prometheus 仪表板
该目录已自动创建并映射到容器中的 /Prometheus 目录。
我们可以看到,容器已启动并正在运行,我们现在可以在 http://your_ip:9090/ 或 http://192.168.1.226:9090/status 检查 Prometheus 服务器(注意:IP 地址可能与此演示到您的设置不同,因此请使用您的主机 IP 地址)。
我们必须单击状态并单击目标。我们可以看到节点作为不健康状态,并看到节点导出器尚未启动。
设置节点导出器
这里我们将安装 Prometheus 节点导出器。节点导出器是公开主机机器的 Prometheus 指标的服务器,它在其中运行,显示机器的文件系统、网络设备、处理器、内存使用情况等。
由于我们的要求是使用 Docker 管理所有进程,因此我们将使用解决方法来提供 Docker 指标的近似值。
要使用 Docker 在端口 9100 上启动节点导出器,请使用以下命令。
$ 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 服务器将自动开始抓取节点导出器。
http://192.168.1.226:9090/status
http://192.168.1.226:9100/metrics
设置 Grafana
由于 Prometheus 服务器已安装并准备就绪,我们现在将设置 Grafana。Grafana 是一个带有仪表板的图形界面,它支持 Prometheus 作为后端来查询数据以生成图表。
Grafana 将数据存储在基于 SQL 的数据库中,Grafana 支持 SQLite3、MySQL 或 PostgreSQL。在本教程中,我们使用 SQLite3 数据库来存储数据库。
使用管理员密码启动 Grafana 作为 Docker 容器
$ 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 服务器、节点导出器和 Grafana,因为它们都在同一台机器上运行,这仅用于演示目的。我们可以从各自的文档中了解更多关于 Prometheus 和 Grafana 的信息。