Node_exporter安装(监控节点)

下载安装包

Download

解压安装包

# tar -xvf node_exporter-0.18.1.linux-amd64.tar.gz
# mv node_exporter-0.18.1.linux-amd64 /usr/local/node_exporter

加入环境变量

# echo "export PATH=$PATH:/usr/local/node_exporter" >> /etc/profile
# source /etc/profile

启动node_exporter

# node_exporter &

Prometheus安装

下载安装包

Download

解压安装包

# tar -xvf prometheus-2.13.1.linux-amd64.tar.gz
# mv prometheus-2.13.1.linux-amd64 /usr/local/prometheus

加入环境变量

# echo "export PATH=$PATH:/usr/local/prometheus" >>/etc/profile
# source /etc/profile

配置参数(/usr/local/prometheus/prometheus.yml)

global:
  scrape_interval:     15s
  evaluation_interval: 15s
  
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus
          
  - job_name: t-luhxdb1
    static_configs:
      - targets: ['10.0.139.161:9100']
        labels:
          instance: db1
          
  - job_name: t-luhxdb2
    static_configs:
      - targets: ['10.0.139.162:9100']
        labels:
          instance: db2

  - job_name: t-luhxdb3
    static_configs:
      - targets: ['10.0.139.163:9100']
        labels:
          instance: db3

启动Prometheus

# prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

Grafana安装

下载安装包

Download Grafana

安装grafana

# rpm -ivh grafana-6.4.3-1.x86_64.rpm

安装字体包

# yum install urw-fonts freetype* fontconfig -y

开启dashboard(/etc/grafana/provisioning/dashboards/sample.yaml)

# # config file version
apiVersion: 1

providers:
- name: 'default'
   orgId: 1
   folder: ''
   folderUid: ''
   type: file
   options:
     path: /var/lib/grafana/dashboards

开启插件参数(/etc/grafana/grafana.ini)

# plugins = /var/lib/grafana/plugins

Grafana Lab下载Dashboard模板并上传至/var/lib/grafana/dashboards

Download Dashboard
修改文件属主

# chown -R grafana.grafana 1-node-exporter-0-16-0-18-for-prometheus_rev1.json

Grafana Lab下载piechat插件并上传至/var/lib/grafana/plugins

# unzip  grafana-piechart-panel-v1.3.9-0-gec46c48.zip
# mv  grafana-piechart-panel-gec46c48 /var/lib/grafana/plugins/grafana-piechart-panel
# chown -R grafana.grafana /var/lib/grafana/plugins/grafana-piechart-panel

启动Grafana

# systemctl start grafana-server

查看监控

Dashboard

MySQL数据库监控

下载mysqld_exporter

Download

创建MySQL用户

root@(none) 10:38:  grant replication client,process on *.* to 'exporter'@'localhost' identified by 'Abcd123#';
root@(none) 10:38:  grant select on performance_schema.* to 'exporter'@'localhost';
root@(none) 10:39:  flush privileges;

设置环境变量

# echo "export DATA_SOURCE_NAME='exporter:Abcd123#@(localhost:33006)/'" >> /etc/profile
# source /etc/profile

启动mysqld_exporter

# /usr/local/mysqld_exporter/mysqld_exporter &

添加prometheus参数

- job_name: t-luhxdb3
   static_configs:
   - targets: ['10.0.139.163:9100']
     labels:
         instance: db3

Consul服务发现

通过Consul可以动态注册监控服务,无需每次添加监控对象都重启Prometheus

下载Consul

Donwload

解压并以WEB UI方式启动Consul

# unzip consul_1.0.0_linux_amd64.zip
# ./consul agent -server -ui -bootstrap-expect 1 -data-dir /tmp/consul -client 0.0.0.0 &

查看启动状态

# ./consul members
Node             Address            Status  Type    Build  Protocol  DC   Segment
t-luhx01-v-szzb  10.0.139.161:8301  alive   server  1.0.0  2         dc1  <all>

HTTP方式注册服务

# curl -X PUT -d '{"id":"mysql","name":"mysql","address":"10.0.139.163","port":9104,"tags":["test"],"checks":[{"http":"http://10.0.139.163.9104/","interval":"5s"}]}' http://localhost:8500/v1/agent/service/register

配置文件方式注册服务(/etc/consul.d/mysql.json)

  "service":{  
    "id": "mysql",  
    "name": "mysql",  
    "address": "10.0.139.162",  
    "port": 9104,  
    "tags": ["test"],  
    "checks": [  
        {  
            "http": "http://10.0.139.162:9104",  
            "interval": "5s"  
        }  
    ]  
  }  
}

修改Prometheus参数

global:
  scrape_interval:     15s
  evaluation_interval: 15s
  
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus
  - job_name: 'consul'
    consul_sd_configs:p
      - server: 'localhost:8500'
        services: ['redis', 'mysql', 'linux']

指定文件启动

# ./consul agent -server -ui -bootstrap-expect 1 -data-dir /tmp/consul -config-dis /etc/consul.d -client 0.0.0.0 &

修改配置文件重新加载

# consul reload