安装
先创建一个copy出配置文件
docker cp d20038ac3ba1:/etc/prometheus/ /home/lys/prometheus
然后删除启动挂载
docker run --name lys-prometheus -d -p 9090:9090 -v /home/lys/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /home/lys/prometheus/host.json:/etc/prometheus/host.json quay.io/prometheus/prometheus
配置
prometheus.yml
global: 全局配置(如果有内部单独设定,会覆盖这个参数)
alerting: 告警插件定义。这里会设定alertmanager这个报警插件。
rule_files: 告警规则。 按照设定参数进行扫描加载,用于自定义报警规则,其报警媒介和route路由由alertmanager插件实现。
scrape_configs:采集配置。配置数据源,包含分组job_name以及具体target。又分为静态配置和服务发现
prometheus支持服务发现(也是运维最佳实践经常采用的):
文件服务发现
基于文件的服务发现方式不需要依赖其他平台与第三方服务,用户只需将 要新的target信息以yaml或json文件格式添加到target文件中 ,prometheus会定期从指定文件中读取target信息并更新
好处:
(1)不需要一个一个的手工去添加到主配置文件,只需要提交到要加载目录里边的json或yaml文件就可以了;
(2)方便维护,且不需要每次都重启prometheus服务端。
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
#通过配置file 获取target,这里以lys-web项目进行举例
- job_name: 'lys-other'
#metrics_path: /metrics
file_sd_configs:
- files:
- host.json
host.json
[
{
"targets": [
"0.0.0.0:9090"
],
"labels": {
"group": "lys",
"app": "web",
"hostname":"lys02"
}
},
{
"targets": [
"0.0.0.0:4000"
],
"labels": {
"group": "lys",
"app": "devops",
"hostname":"lys01"
}
}
]