Prometheus的安装配置与grafana安装

    技术2022-07-10  192

    一、Prometheus与Grafana

    1、Prometheus简介

    Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是Google BorgMon监控系统的开源版本。2016年由Google发起Linux基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将Prometheus纳入其下第二大开源项目。Prometheus目前在开源社区相当活跃。Prometheus和Heapster(Heapster是K8S的一个子项目,用于获取集群的性能数据。)相比功能更完善、更全面。Prometheus性能也足够支撑上万台规模的集群。其架构图如下:

    Prometheus Server, 负责从 Exporter 拉取和存储监控数据,并提供一套灵活的查询语言(PromQL)供用户使用。Exporter, 负责收集目标对象(host, container…)的性能数据,并通过 HTTP 接口供 Prometheus Server 获取。可视化组件,监控数据的可视化展现对于监控方案至关重要。以前 Prometheus 自己开发了一套工具,不过后来废弃了,因为开源社区出现了更为优秀的产品 Grafana。Grafana 能够与 Prometheus 无缝集成,提供完美的数据展示能力。Alertmanager,用户可以定义基于监控数据的告警规则,规则会触发告警。一旦 Alermanager 收到告警,会通过预定义的方式发出告警通知。支持的方式包括 Email、PagerDuty、Webhook 等.

     

    2、Grafana简介

    Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。它主要有以下六大特点:

    1、展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式;2、数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;3、通知提醒:以可视方式定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值时通过Slack、PagerDuty等获得通知;4、混合展示:在同一图表中混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源;5、注释:使用来自不同数据源的丰富事件注释图表,将鼠标悬停在事件上会显示完整的事件元数据和标记;6、过滤器:Ad-hoc过滤器允许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询。

    3、安装环境

    接下来,讲述在CentOS7操作系统下,各个模块的安装过程。

    二、服务端prometheus安装 

    1、下载解压

    wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.linux-amd64.tar.gz tar zxvf prometheus-2.19.2.linux-amd64.tar.gz mv prometheus-2.19.2.linux-amd64 /usr/local/prometheus

    2、创建用户

    groupadd prometheus useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus chown prometheus.prometheus -R /usr/local/prometheus

    3、创建Systemd服务

    cat > /etc/systemd/system/prometheus.service <<EOF [Unit] Description=prometheus After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data Restart=on-failure [Install] WantedBy=multi-user.target EOF

    4、启动Prometheus

    systemctl daemon-reload systemctl start prometheus systemctl status prometheus systemctl enable prometheus

    5、访问WEB界面

       访问   http://172.17.10.248:9090

    说明:如果访问不通,记得关闭主机的防火墙哦,命令为:systemctl stop firewalld.service

    三、节点端node_exporter安装

    node_exporter的作用是用于机器系统数据收集,监控服务器CPU、内存、磁盘、I/O等信息。

    1、下载解压

    wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz mv node_exporter-1.0.1.linux-amd64 /usr/local/node_exporter

    2、创建用户(如果已创建可忽略此部)

    groupadd prometheus useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus chown prometheus.prometheus -R /usr/local/node_exporter

    3、创建Systemd服务

    cat > /etc/systemd/system/node_exporter.service <<EOF [Unit] Description=node_exporter After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target EOF

    4、启动node_exporter

    systemctl daemon-reload systemctl start node_exporter systemctl status node_exporter systemctl enable node_exporter

    5、验证启动成功

    curl 127.0.0.1:9100 curl 127.0.0.1:9100/metrics

    四、PushGateway 安装

    PushGateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是:

    Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙原因,导致 Prometheus 无法直接拉取各个 target 数据。在监控业务数据的时候,需要将不同数据汇总, 由 Prometheus 统一收集。

    由于以上原因,不得不使用 pushgateway,但在使用之前,有必要了解一下它的一些弊端:

    将多个节点数据汇总到 pushgateway, 如果 pushgateway 挂了,受影响比多个 target 大。Prometheus 拉取状态 up 只针对 pushgateway, 无法做到对每个节点有效。Pushgateway 可以持久化推送给它的所有监控数据。

    因此,即使你的监控已经下线,prometheus 还会拉取到旧的监控数据,需要手动清理 pushgateway 不要的数据。

    1、下载解压

    wget https://github.com/prometheus/pushgateway/releases/download/v1.2.0/pushgateway-1.2.0.linux-amd64.tar.gz tar zxvf pushgateway-1.2.0.linux-amd64.tar.gz mv pushgateway-1.2.0.linux-amd64 /usr/local/pushgateway

    2、创建用户(如果已创建可忽略此部)

    groupadd prometheus useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus chown prometheus.prometheus -R /usr/local/pushgateway

    3、创建Systemd服务

    cat > /etc/systemd/system/pushgateway.service <<EOF [Unit] Description=pushgateway After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/pushgateway/pushgateway Restart=on-failure [Install] WantedBy=multi-user.target EOF

    4、启动pushgateway

    systemctl daemon-reload systemctl start pushgateway systemctl status pushgateway systemctl enable pushgateway

    5、验证

    (1) 访问地址:

         http://172.17.10.248:9091/

    (2) 推送测试:

    echo "some_metric 3.14" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/some_job

    6、服务端配置

    要使Push Gateway正常工作,必须要在prometheus中配置(即:prometheus.yml)对应的job才行,下一章中讲述。

     

    五、服务端配置

    prometheus的服务端通过pull向各个node_exporter节点端抓取信息,需要在各个node上安装exporter。可以利用 Prometheus 的 static_configs 来拉取 node_exporter 的数据。

    编辑prometheus.yml文件,添加内容:

    - job_name: 'node'     static_configs:     - targets: ['XXX.XXX.XXX.XXX:9100']

    完整配置如下:

    [root@localhost prometheus]# cat prometheus.yml # 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: 'server248' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['172.17.10.248:9090'] - job_name: 'node247' static_configs: - targets: ['172.17.10.247:9100'] - job_name: 'node246' static_configs: - targets: ['172.17.10.246:9100'] - job_name: 'node244' static_configs: - targets: ['172.17.10.244:9100']

    重启prometheus,然后在Prometheus页面中的Targets中就能看到新加入的node:

    systemctl restart prometheus

    然后再次查询web界面如下:

    访问http://10.207.10.248:9090

    六、集成各服务监控

    1、 nginx

    https://github.com/hnlq715/nginx-vts-exporter

    2、 php https://github.com/bakins/php-fpm-exporter

    配置参考:https://blog.csdn.net/ffzhihua/article/details/88846642

    3、 mysql https://github.com/prometheus/mysqld_exporter

    (1)下载解压

    wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz tar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter

    (2)创建用户(如果已创建可忽略此部)

    groupadd prometheus useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus

    (3)修改所属

    chown prometheus.prometheus -R /usr/local/mysqld_exporter

    (4)创建Systemd服务

    cat > /etc/systemd/system/mysqld_exporter.service <<EOF [Unit] Description=mysqld_exporter After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf Restart=on-failure [Install] WantedBy=multi-user.target EOF

    (5)创建并填写.my.cnf文件内容

    vim .my.cnf [client] host=127.0.0.1 port=3306 user=root password=root

    (6)启动mysqld_exporter

    systemctl daemon-reload systemctl start mysqld_exporter systemctl status mysqld_exporter systemctl enable mysqld_exporter

    4、 zookeeper

    https://github.com/dabealu/zookeeper-exporter

    5、 kafkahttps://github.com/uepoch/kafka_exporter

    6、 redis https://github.com/oliver006/redis_exporter

    7、 postgresql

    https://github.com/wrouesnel/postgres_exporter

    8、greenplum

    https://gitee.com/inrgihc/greenplum_exporter

    9、 springboot

    https://blog.csdn.net/qq_33257527/article/details/88294016

    更多expoter参见官网:https://prometheus.io/docs/instrumenting/exporters/#exporters-and-integrations

     

    七、Grafana安装

    官方地址:https://grafana.com/grafana/download?platform=linux

    1、下载

    wget https://dl.grafana.com/oss/release/grafana-7.0.5-1.x86_64.rpm

    2、安装

    yum install -y grafana-7.0.5-1.x86_64.rpm

    3、配置

    配置文件位于/etc/grafana/grafana.ini,这里暂时保持默认配置即可

    4、启动

    systemctl enable grafana-server systemctl start grafana-server

    5、访问

    http://10.207.10.248:3000 用户名:admin 密码:admin

    八、告警配置

    请参考:https://blog.csdn.net/inrgihc/article/details/107636371

     

    九、结束语

    最后,为各位推荐个基于ansible的安装配置说明的英文博客,地址为:

    https://opensource.com/article/18/3/how-use-ansible-set-system-monitoring-prometheus

    Processed: 0.031, SQL: 9