通过Grafana展示nginx容器指标

目标

本测试由三部分组成:

1)Nginx指标收集,由nginx-prometheus-exporter转化为prometheus可以处理的指标格式,这部分我们上一节已经完成,本节将复用我们做好的nginx-exporter:v0.2镜像。

2)配置Prometheus对指标进行收集(镜像https://hub.docker.com/r/prom/prometheus

3)配置Grafana对指标进行展示(镜像https://hub.docker.com/r/grafana/grafana

测试1:配置Prometheus对指标进行收集

首先,在宿主机上创建prometheus.yml文件,内容如下:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:
     - job_name: 'prometheus'
       static_configs:
       - targets: ['localhost:9090']
     - job_name: 'nginx_exporter'
       static_configs:
       - targets: ['192.168.2.31:9113']
         labels:
           group: 'services'

启动Prometheus容器:

然后访问宿主机 http://192.168.2.31:9090/targets,获得如下界面即代表Prometheus和Nginx指标均可获取:

使用Nginx指标名称能在Prometheus绘制出简单图表

测试2:配置Grafana对指标进行展示

1)准备dashboard.json

首先我们需要定制一个适合展示prometheus收集回来的nginx的指标参数的dashboards仪表板模板,最快的方式是上官网的选择现成的,参考https://grafana.com/grafana/dashboards/

实际工作中,我们通常使用的nginx-vts-exporter能获得更多的指标。

这里我们自己定义一个适合nginx-prometheus-exporter指标的json文件,并命名为dashboard.json:

2)准备provisioning文件

参考:https://grafana.com/docs/grafana/latest/administration/provisioning/

2-1)定义存放dashboard模板的目录

创建一个dashboard.yaml文件:

2-2)定义数据源

创建一个datasouce.yaml文件:

3)启动Grafana容器

现在宿主机上三个容器是:

访问宿主机3000端口,打开Grafana,正常可以看到如下仪表板:

Last updated

Was this helpful?