Ubuntu 20.04 部署Prmoetheus+grafana+mysql+mysqld_exporter+node_exporter
Prometheus简介
Prometheus是一个功能强大的开源监控系统,可从您的服务中收集指标并将其存储在时间序列数据库中。它通过Grafana等工具提供多维数据模型,灵活的查询语言和多样化的可视化可能性。
默认情况下,Prometheus仅导出有关其自身的指标(例如,它收到的请求数,内存消耗等)。但是,您可以通过安装导出程序,生成其他指标的可选程序来大大扩展Prometheus 。
出口商 - Prometheus团队维护的官方出版物以及社区贡献的出口商 - 提供从基础设施,数据库和Web服务器到消息系统,API等所有内容的信息。
一些最受欢迎的选择包括:
node_exporter - 这将生成有关基础结构的度量标准,包括当前CPU,内存和磁盘使用情况,以及I / O和网络统计信息,例如从磁盘读取的字节数或服务器的平均负载。
blackbox_exporter - 这会生成从HTTP和HTTPS等探测协议派生的指标,以确定端点可用性,响应时间等。
mysqld_exporter - 收集与MySQL服务器相关的度量标准,例如执行的查询数,平均查询响应时间和集群复制状态。
rabbitmq_exporter - 输出有关RabbitMQ消息传递系统的指标,包括发布的消息数,准备发送的消息数以及队列中所有消息的大小。
nginx-vts-exporter - 提供有关使用Nginx VTS模块的Nginx Web服务器的指标,包括打开的连接数,发送的响应数(按响应代码分组)以及发送或接收的请求的总大小(以字节为单位) 。
一、prometheus
1、下载地址:https://prometheus.io/download/prometheus-2.35.0.linux-amd64.tar.gz
github下载地址:https://github.com/prometheus/prometheus/tags
tar xf prometheus-2.35.0.linux-amd64.tar.gz -C /usr/local/ mv prometheus-2.35.0.linux-amd64 prometheus
2、vim /etc/systemd/system/prometheus.service
[Unit] Description=prometheus Wants=network-online.target After=network-online.target [Service] Type=simple ExecStart=/usr/local/prometheus/prometheus \ --config.file /usr/local/prometheus/prometheus.yml \ # --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/usr/local/prometheus/consoles \ --web.console.libraries=/usr/local/prometheus/console_libraries [Install] WantedBy=multi-user.target
3、修改prmoetheus.yml文件
在global设置中,定义抓取指标的默认时间间隔。请注意,除非单个导出器自己的设置覆盖全局变量,否则Prometheus会将这些设置应用于每个导出器。 scrape_interval值告诉Prometheus 每15秒从其出口商那里收集指标,这对于大多数出口商而言足够长。 使用scrape_configs指令将Prometheus本身添加到导出器列表中。 使用job_name在标签和图表上标记出口商,因此请务必在此处选择描述性内容。 使用static_configs和targets指令来确定导出器的运行位置。
sudo vim /usr/local/prometheus/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: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'node_exporter' scrape_interval: 5s static_configs: - targets: ['localhost:9100'] # - job_name: 'nm_mch-app_node' # file_sd_configs: #- files: ['/usr/local/prometheus/targets/node/node.yml'] #refresh_interval: 5s - job_name: 'mysqld_exporter' scrape_interval: 5s static_configs: - targets: ['localhost:9104']
注:安装哪个服务就在后面添加哪个服务,如上所示!
4、重载加载并启动服务
sudo systemctl daemon-reload sudo systemctl start prometheus.service sudo systemctl enable prometheus.service sudo systemctl status prometheus.service
二、mysqld_exporter及安装mysql
1、下载地址:https://prometheus.io/download/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/ mv mysqld_exporter-0.14.0.linux-amd64 mysqld_exporter
2、sudo vim /usr/local/mysqld_exporter/.my.cnf
[client] user=mysqld_exporter password=Password
3、sudo vim /etc/systemd/system/mysqld_exporter.service
[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
4、重载加载并启动服务
sudo systemctl daemon-reload sudo systemctl start mysqld_exporter.service sudo systemctl enable mysqld_exporter.service sudo systemctl status mysqld_exporter.service
5、安装mysql
注:因为mysqld_exporter要从mysql中取数据
sudo apt install mysql-server sudo apt install mysql-client sudo apt-get install libmysqlclient-dev
6、sudo cat /etc/mysql/debian.cnf
[client] host = localhost user = debian-sys-maint >>> 用户名 password = lncvxxxxxxxx2Zrd >>> 密码
7、登录mysql
sudo mysql -u debian-sys-maint -p
CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY 'Password' WITH MAX_USER_CONNECTIONS 2; >>>创建用户 GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost'; >>>赋权限 FLUSH PRIVILEGES; >>>刷新权限
8、重启mysql
sudo systemctl restart mysql sudo systemctl status mysql
三、node_exporter
1、下载地址:https://prometheus.io/download/node_exporter-1.3.1.linux-amd64.tar.gz
tar xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/ mv node_exporter-1.3.1.linux-amd64 node_exporter
2、sudo vim /etc/systemd/system/node_exporter.service
[Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] Type=simple ExecStart=/usr/local/node_exporter/node_exporter [Install] WantedBy=multi-user.target
3、重载加载并启动服务
sudo systemctl daemon-reload sudo systemctl start node_exporter.service sudo systemctl enable node_exporter.service sudo systemctl status node_exporter.service
四、grafana-server
1、下载地址:https://grafana.com/grafana/download?edition=oss
wget https://dl.grafana.com/oss/release/grafana-8.0.4.linux-amd64.tar.gz tar xf grafana-8.0.4.linux-amd64.tar.gz -C /usr/local/
2、sudo vim /lib/systemd/system/grafana-server.service
[Unit] Description=Grafana After=network-online.target [Service] User=root group=root Type=simple Restart=on-failure WorkingDirectory=/usr/local/grafana-8.0.4/bin/ ExecStart=/usr/local/grafana-8.0.4/bin/grafana-server --config=/usr/local/grafana-8.0.4/conf/defaults.ini [Install] WantedBy=multi-user.target
3、重新加载并启动服务
sudo systemctl daemon-reload sudo systemctl start grafana-server.service sudo systemctl enable grafana-server.service sudo systemctl status grafana-server.service
五、登录grafana查看效果
登录地址:http://本机IP:3000
1、添加Prometheus来源
2、添加node_porter模板
3、Copy ID to Clipboard 至Grafana.com dashboard URL or ID ,如输入:11074,点击Load,选择prometheus
地址:https://grafana.com/grafana/dashboards/
3、即可显示node_exporter主机信息。
4、添加mysqld_exporter模板
需要从Grafana的存储库中填写仪表板的URL。将链接粘贴到“ URL”部分,https://grafana.com/grafana/dashboards/7362
将Prometheus数据源名称更改为“ Prometheus ”,然后单击“ 导入 ”,即可查看MySQL相关监控信息!
5、Prometheus访问
访问地址:本地IP:9090
至此,部署完成!