一:安装promethues

1.1:环境及版本:

CentOS Linux release 7.9.2009 (Core) prometheus-2.24.1.linux-amd64

1.2:下载地址:https://github.com/prometheus/prometheus/releases/download/v2.24.1/prometheus-2.24.1.linux-amd64.tar.gz

1.3:解压并创建配置文件

tar -zxvf prometheus-2.24.1.linux-amd64.tar.gz -C /opt cd /opt/prometheus-2.24.1.linux-amd64 注:promethues有多种发现类型,本实例只配置静态文件的形式 mkdir targets

1.3.1:配置文件-1:prometheus.yml

注意这个文件的位置:/opt/prometheus-2.24.1.linux-amd64/prometheus.yml 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: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  # All nodes
  - job_name: 'nodes'
    file_sd_configs:
    - files:
      - targets/nodes-*.yaml
      refresh_interval: 2m

注:其实在原文件中添加下面的几行就可以了 就是添加了一个JOB

  # All nodes
  - job_name: 'nodes'
    file_sd_configs:
    - files:
      - targets/nodes-*.yaml
      refresh_interval: 2m

注:file_sd_configs 是文件发现的意思 进入targets目录,创建节点配置文件

1.3.2:配置文件-2:nodes-linux.yaml

cat nodes-linux.yaml

- targets:
  - 192.168.0.5:9100
  labels:
    app: node-exporter
    job: node

注:192.168.0.5是被监控的一台主机,客户端上要安装 node-exporter ,暴露端口是:9100

1.4:启动服务

启动服务有多种方法,这里介绍使用screen启动的方法,首先要安装一个screen yum install screen -y 注:ubuntu默认应该已经安装过了 建议先切换到:/opt/prometheus-2.24.1.linux-amd64/ 这个目录然后: 开启一个screen窗口: screen -L -S prometheus 然后执行启动命令: ./prometheus --config.file="prometheus.yml" 然后使用键盘组合键退出screen窗口 ctrl+a,松手后再按字母d键即可 查看启动情况:

[root@aly-prometheus prometheus-2.24.1.linux-amd64]# netstat -nltp|grep prometheus
tcp6       0      0 :::9090                 :::*                    LISTEN      27367/./prometheus

可以看到9090端口已经启动,现在就可以登录 http://IP:9090 端口访问他的UI了

二:安装grafana

官网参考地址:https://grafana.com/grafana/download

2.1:环境及版本:

CentOS Linux release 7.9.2009 (Core) grafana-7.4.0-1.x86_64

2.2:下载地址

wget https://dl.grafana.com/oss/release/grafana-7.4.0-1.x86_64.rpm sudo yum install grafana-7.4.0-1.x86_64.rpm

2.3:启动

systemctl start grafana-server

三:安装node_exporter

3.1:环境及版本:

CentOS Linux release 7.9.2009 (Core) node_exporter-1.0.1.linux-amd64

3.2:下载地址

wget https://github.com/prometheus/node_exporter/releases/download/v1.1.0/node_exporter-1.1.0.linux-amd64.tar.gz

3.3:解压并拷贝文件:

tar -zxvf node_exporter-1.1.0.linux-amd64.tar.gz #注:在cp前面加\的目的是把别名进行替换成原来的命令,即如果目录存在就直接替换掉 cp node_exporter-1.1.0.linux-amd64/node_exporter /usr/local/bin/ rm node_exporter-1.1.0.linux-amd64/* -rf

3.4:创建Unit文件

#centos系统 cat /usr/lib/systemd/system/node_exporter.service #ubuntu系统 cat /lib/systemd/system/node_exporter.service

[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter \
        --collector.ntp \
        --collector.mountstats \
        --collector.systemd \
        --collector.tcpstat
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
Restart=always

StandardInput=/var/log/node_exporter.log
StandardOutput=/var/log/node_exporter.log
StandardError=/var/log/node_exporter.log

[Install]
WantedBy=multi-user.target

3.5:启动服务

systemctl daemon-reload systemctl restart node_exporter systemctl enable node_exporter

3.6:验证查看端口有没有启动成功

ss -nltp|grep 9100

3.7:Grafana参考模板:8919

四:安装mysqld_exporter

4.1:环境:

CentOS Linux release 7.9.2009 (Core) mysqld_exporter-0.12.1.linux-amd64.tar.gz

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

4.3:解压并拷贝文件:

tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz cp mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter /usr/local/bin/

4.4:创建exporter使用的数据库用户并授权:

CREATE USER 'mysql_exporter'@'localhost' IDENTIFIED BY 'Mysql_exporter_passw0rd';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_exporter'@'localhost';
FLUSH PRIVILEGES;

4.5:生成一个mysqld_exporter启动可以读取到的配置文件

cat /etc/mysql-export-user.cnf [client] user=mysql_exporter password=Mysql_exporter_passw0rd

4.6:创建Unit文件

cat /usr/lib/systemd/system/mysqld_exporter.service

[Unit]
Description=mysqld_exporter
Documentation=https://prometheus.io/
After=mysqld.target
[Service]
ExecStart=/usr/local/bin/mysqld_exporter  --config.my-cnf=/etc/mysql-export-user.cnf
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
Restart=always
[Install]
WantedBy=multi-user.target

4.7:启动服务

systemctl daemon-reload systemctl restart mysqld_exporter

4.8:验证查看端口有没有启动成功

ss -nltp|grep 9104

4.8:Grafana参考模板:7362