Prometheus是一个开源的监控和告警工具,可以用于收集、存储和查询各种指标数据,以下是配置Prometheus的步骤:
1、下载并安装Prometheus
访问Prometheus官方网站(https://prometheus.io/)下载适合你操作系统的Prometheus二进制文件,解压下载的文件,并将解压后的文件夹移动到合适的位置。
在Prometheus的配置文件prometheus.yml中,你可以设置Prometheus的各种参数,如监听地址、数据存储路径等,以下是一个示例配置:
“`yaml
global:
scrape_interval: 15s # 设置抓取间隔为15秒
evaluation_interval: 15s # 设置评估间隔为15秒
scrape_configs:
job_name: ‘prometheus’ # 设置作业名称为’prometheus’
static_configs:
targets: [‘localhost:9090’] # 设置目标地址为本地9090端口
“`
在命令行中,进入Prometheus的根目录,运行以下命令启动Prometheus:
“`bash
./prometheus config.file=prometheus.yml
“`
4、验证Prometheus是否正常运行
访问Prometheus的Web界面(默认地址为http://localhost:9090),如果看到类似以下的页面,说明Prometheus已经成功运行:
“`
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 27
“`
5、添加监控目标
为了让Prometheus能够收集其他应用程序或服务的指标数据,你需要在prometheus.yml文件中添加监控目标,如果你想要监控一个名为myapp的服务,可以将其添加到scrape_configs部分:
“`yaml
scrape_configs:
job_name: ‘myapp’ # 设置作业名称为’myapp’
static_configs:
targets: [‘myapp:9100’] # 设置目标地址为myapp服务的9100端口
“`
6、重启Prometheus以应用新的配置
在命令行中,再次运行以下命令重启Prometheus:
“`bash
./prometheus config.file=prometheus.yml web.listenaddress="0.0.0.0:9090" storage.tsdb.path="/data/prometheus" storage.tsdb.minblockduration=2h storage.tsdb.maxblockduration=2h storage.tsdb.retention=1d web.enablelifecycle "true" web.console.templates="[ {"path": "/etc/prometheus/consoles/*.tpl"} ]" web.console.libraries="[ {"library": "console_libraries", "version": "1.0"} ]" web.enableadminapi "true" web.listenaddress="0.0.0.0:9090" web.externalurl="http://mydomain.com/prometheus" web.routeprefix="/" storage.tsdb.minblockduration=2h storage.tsdb.maxblockduration=2h storage.tsdb.retention=1d web.enablelifecycle "true" web.console.templates="[ {"path": "/etc/prometheus/consoles/*.tpl"} ]" web.console.libraries="[ {"library": "console_libraries", "version": "1.0"} ]" web.enableadminapi "true" web.listenaddress="0.0.0.0:9090" web.externalurl="http://mydomain.com/prometheus" web.routeprefix="/" storage.tsdb.minblockduration=2h storage.tsdb.maxblockduration=2h storage.tsdb.retention=1d web.enablelifecycle "true" web.console.templates="[ {"path": "/etc/prometheus/consoles/*.tpl"} ]" web.console.libraries="[ {"library": "console_libraries", "version": "1.0"} ]" web.enableadminapi "true" web.listenaddress="0.0.0.0:9090" web.externalurl="http://mydomain.com/prometheus" web.routeprefix="/" storage.tsdb.minblockduration=2h storage.tsdb.maxblockduration=2h storage.tsdb.retention=1d web.enablelifecycle "true" web.console.templates="[ {"path": "/etc/prometheus/consoles/*