使用prometheus和grafana搭建系统监控看板

概述

本文主要介绍如何使用GrafanaPrometheus对Linux服务器和mysql性能进行监控。

下面示例运行的操作系统是 ubuntu 18.04。

安装

prometheus

我们可以直接在官网下载编译好的可执行文件:https://prometheus.io/download/ ,但是很慢,所以建议在国内的镜像站下载,比如清华大学的镜像站:https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/

# 下载
wget https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/2.18.1%20_%202020-05-07/prometheus-2.18.1.linux-amd64.tar.gz

# 解压
tar xzvf prometheus-2.18.1.linux-amd64.tar.gz

# 挪位置
mv prometheus-2.18.1.linux-amd64 /usr/local/prometheus

# 在 /usr/local/prometheus 下面创建一个子目录存放 exporter
mkdir exporters

node exporter

exporter 都没有镜像站,只能官网小水管慢慢下载了。

# 下载
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.1/node_exporter-1.0.0-rc.1.linux-amd64.tar.gz

# 解压
tar xzvf node_exporter-1.0.0-rc.1.linux-amd64.tar.gz

# 把可执行文件挪到之前创建的 exporters 目录,方便集中管理
mv node_exporter /usr/local/prometheus/exporters

# 运行会监听9100端口
/usr/local/prometheus/exporters/node_exporter

mysqld exporter

# 下载
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

# 解压
tar xzvf mysqld_exporter-0.12.1.linux-amd64.tar.gz

# 把可执行文件挪到之前创建的 exporters 目录,方便集中管理
mv mysqld_exporter /usr/local/prometheus/exporters

# 设置连接数据库的环境变量(或者也可以创建配置文件~/.my.cnf)
export DATA_SOURCE_NAME='user:password@(hostname:3306)/'

# 运行会监听9104端口
/usr/local/prometheus/exporters/mysqld_exporter

启动 prometheus

编辑 prometheus.yml 加上上面两个exporter

# 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=` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100']

  - job_name: 'mysqld'
    static_configs:
    - targets: ['localhost:9104']

运行:prometheus --config.file="prometheus.yml"prometheus默认的监听端口是9090。

grafana

grafana 的安装可以参考官网:https://grafana.com/grafana/download

推荐使用deb包安装:

sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_7.0.0_amd64.deb
sudo dpkg -i grafana_7.0.0_amd64.deb

然后加入开机启动,并启动服务:

# 加入开机启动
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server

# 启动服务
sudo /bin/systemctl start grafana-server

grafana 默认监听的端口是3000,默认的管理员账户和密码都是admin

可以通过nginx配置个域名指向3000端口:

server {
    server_name  [my-domain];

    charset utf-8;

    location / {
        default_type text/html;
        proxy_pass  http://127.0.0.1:3000;
    }

    access_log  /var/log/nginx/[my-domain].access.log  main;
    error_log   /var/log/nginx/[my-domain].error.log;
}

第一次登陆grafana后会提示你修改密码。默认是没有dashboard,需要自己去下载。数据源也需要自己设定一下,可以选择 prometheus,然后URL填写 http://localhost:9090 即可。

dashboard 可以去 https://grafana.com/grafana/dashboards 找自己喜欢的导入,导入填写对应的id就可以了。主机监控推荐这个:https://grafana.com/grafana/dashboards/8919 ,数据库监控推荐:https://grafana.com/grafana/dashboards/7362,不过这个已经很久没更新了,可以去 https://github.com/percona/grafana-dashboards 获取最新的json。

你可能感兴趣的:(使用prometheus和grafana搭建系统监控看板)