cloudalchemy.prometheus
过时
此角色已过时,推荐使用 prometheus-community/ansible 集合。
Ansible 角色:prometheus
描述
使用 ansible 部署 Prometheus 监控系统。
升级注意事项
从 <= 2.4.0 版本升级到 >= 2.4.1 时,请关闭你的 prometheus 实例。更多信息请参见 2.4.1 发布说明。
要求
- Ansible >= 2.7(可能在以前的版本上也能工作,但我们无法保证)
- 部署机器上需要安装 jmespath。如果你在 Python 虚拟环境中使用 Ansible,请通过 pip 在该虚拟环境中安装 jmespath。
- Mac 部署主机上需要安装 gnu-tar(使用
brew install gnu-tar
安装)
角色变量
所有可以重写的变量存储在 defaults/main.yml 文件中以及下面的表格中。
名称 | 默认值 | 描述 |
---|---|---|
prometheus_version |
2.27.0 | Prometheus 包版本。也接受 latest 作为参数。仅支持 prometheus 2.x |
prometheus_skip_install |
false | 设置为 true 时跳过 Prometheus 安装任务。 |
prometheus_binary_local_dir |
"" | 允许使用本地包代替 GitHub 上分发的包。参数为存储 prometheus 和 promtool 二进制文件的目录。此参数会覆盖 prometheus_version 。 |
prometheus_config_dir |
/etc/prometheus | Prometheus 配置目录路径。 |
prometheus_db_dir |
/var/lib/prometheus | Prometheus 数据库目录路径。 |
prometheus_read_only_dirs |
[] | Prometheus 被允许读取的额外路径(适用于配置目录以外的 SSL 证书) |
prometheus_web_listen_address |
"0.0.0.0:9090" | Prometheus 监听的地址。 |
prometheus_web_config |
{} | Prometheus 的 web 配置 YAML 用于配置 TLS 和认证。 |
prometheus_web_external_url |
"" | Prometheus 可用的外部地址。在反向代理后面时很有用。例如 http://example.org/prometheus |
prometheus_storage_retention |
"30d" | 数据保留期限 |
prometheus_storage_retention_size |
"0" | 根据大小的数据保留期限 |
prometheus_config_flags_extra |
{} | 启动时传递给 prometheus 二进制文件的额外配置标志 |
prometheus_alertmanager_config |
[] | 指向 alertmanager 的配置。应通过 YAML 格式的列表指定。与官方 |
prometheus_alert_relabel_configs |
[] | 警报重标记规则。应通过 YAML 格式的列表指定。与官方 |
prometheus_global |
{ scrape_interval: 60s, scrape_timeout: 15s, evaluation_interval: 15s } | Prometheus 全局配置。与 官方配置 兼容 |
prometheus_remote_write |
[] | 远程写入。与 官方配置 兼容 |
prometheus_remote_read |
[] | 远程读取。与 官方配置 兼容 |
prometheus_external_labels |
environment: "{{ ansible_fqdn | default(ansible_host) |
prometheus_targets |
{} | 将被抓取的目标。更好的例子请参见我们的 演示网站 |
prometheus_scrape_configs |
defaults/main.yml#L58 | Prometheus 抓取作业,格式与 官方文档 相同 |
prometheus_config_file |
"prometheus.yml.j2" | 用于在 ansible 模板中提供自定义 prometheus 配置文件的变量 |
prometheus_alert_rules |
defaults/main.yml#L81 | 将被复制到 {{ prometheus_config_dir }}/rules/ansible_managed.rules 的所有警报规则。警报规则也可以通过位于 {{ prometheus_config_dir }}/rules/ 的其他文件提供,这些文件必须以 *.rules 结尾 |
prometheus_alert_rules_files |
defaults/main.yml#L78 | ansible 查找包含警报规则的文件的文件夹列表,这些文件将被复制到 {{ prometheus_config_dir }}/rules/ 。文件必须以 *.rules 结尾 |
prometheus_static_targets_files |
defaults/main.yml#L78 | ansible 查找包含自定义静态目标配置文件的文件夹列表,这些文件将被复制到 {{ prometheus_config_dir }}/file_sd/ 。 |
prometheus_scrape_configs
和 prometheus_targets
之间的关系
简短版本
prometheus_targets
仅是一个映射,用于创建位于 "{{ prometheus_config_dir }}/file_sd" 目录中的多个文件。文件名由映射中的顶级键和 .yml
后缀组成。这些文件存储 file_sd 抓取目标数据,并且需要在 prometheus_scrape_configs
中读取。
长版本
描述 Prometheus 抓取内容的部分 prometheus.yml 配置文件存储在 prometheus_scrape_configs
中。此变量使用与 prometheus 文档 中描述的相同配置选项。
同时,prometheus_targets
是我们采用 prometheus 抓取类型 file_sd
的方式。它定义文件的映射及其内容。顶级键是文件的基本名称,必须在 prometheus_scrape_configs
中有自己的抓取作业,值是这些文件的内容。
这意味着你可以使用自定义 prometheus_scrape_configs
并将 prometheus_targets
设置为 {}
。但是当你在 prometheus_targets
中设置任何内容时,它需要映射到 prometheus_scrape_configs
。如果没有映射,则会在预检查中出现错误。
示例
让我们看一下我们的默认配置,展示所有功能。默认的 prometheus_targets
为:
prometheus_targets:
node: # 这是基本文件名。文件位于 "{{ prometheus_config_dir }}/file_sd/<<BASENAME>>.yml" 中。
- targets: #
- localhost:9100 # 所有这些都是 file_sd 格式的目标部分。
labels: #
env: test #
这样的配置将在 {{ prometheus_config_dir }}/file_sd
目录中创建一个名为 node.yml
的文件。
接下来,此文件需要被加载到抓取配置中。以下是修改过的默认 prometheus_scrape_configs
:
prometheus_scrape_configs:
- job_name: "prometheus" # 自定义抓取作业,这里使用 `static_config`
metrics_path: "/metrics"
static_configs:
- targets:
- "localhost:9090"
- job_name: "example-node-file-servicediscovery"
file_sd_configs:
- files:
- "{{ prometheus_config_dir }}/file_sd/node.yml" # 此行加载从 `prometheus_targets` 创建的文件。
示例
Playbook
---
- hosts: all
roles:
- cloudalchemy.prometheus
vars:
prometheus_targets:
node:
- targets:
- localhost:9100
- demo.cloudalchemy.org:9100
labels:
env: demosite
演示网站
Prometheus 组织提供了一个完整监控解决方案的演示网站,基于 Prometheus 和 Grafana。包含代码和运行实例链接的库 在 GitHub 上可用。
定义警报规则文件
警报规则在 prometheus_alert_rules
变量中定义。格式几乎与 Prometheus 2.0 文档 中定义的相同。
由于模板引擎的相似性,所有模板都应包装在 {% raw %}
和 {% endraw %}
语句中。示例请参见 defaults/main.yml 文件。
本地测试
本地测试此角色的首选方式是使用 Docker 和 molecule (v2.x)。你需要在系统上安装 Docker。请参见“开始使用”以获取适合你的系统的 Docker 包。 我们使用 tox 简化在多个 ansible 版本上的测试过程。要安装 tox,请执行:
pip3 install tox
要在所有 ansible 版本上运行测试(警告:这可能需要一些时间):
tox
要在只包含默认测试场景的自定义环境上运行自定义 molecule 命令:
tox -e py35-ansible28 -- molecule test -s default
有关 molecule 的更多信息,请访问他们的 文档。
如果你希望在远程 docker 主机上运行测试,只需在运行 tox 测试前指定 DOCKER_HOST
变量。
CircleCI
结合 molecule 和 CircleCI,我们可以测试新的 PR 在使用多个 ansible 版本和多个操作系统时的行为。这还允许我们为不同的角色配置创建测试场景。结果是我们有一个相当大的测试矩阵,这将比本地测试花费更多时间,因此请耐心等待。
贡献
请参见 贡献者指南。
故障排除
请参见 故障排除。
许可证
此项目基于 MIT 许可证。有关更多详细信息,请参见 许可证。
Prometheus monitoring system configuration and management
ansible-galaxy install cloudalchemy.prometheus