通过Prometheus 采集 mysql exporter 与 node exporter ,通过 Grafana 作为展示
基于docker 方式部署,本文仅作为本地测试展示
cd /opt/node_exporter wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz -O node_exporter.tar.gz tar -zxf node_exporter.tar.gz
cat > /etc/systemd/system/node_exporter.service << EOF [Unit] Description=node_exporter Documentation=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus # 系统用户名称 ExecStart=/opt/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target EOF
systemctl enable node_exporter systemctl start node_exporter
curl 127.0.0.1:9100/metrics
进行验证是否生效
CREATE USER 'exporter'@'%' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%'; flush privileges;
version: '2.1' services: mysqld-exporter: image: prom/mysqld-exporter container_name: mysqld-exporter environment: - DATA_SOURCE_NAME=exporter:123456@(192.168.5.242:3306)/ ports: - "9104:9104" restart: "always"
docker-compose up -d
curl 127.0.0.1:9104/metrics
global: scrape_interval: 15s # 默认情况下抓取目标的频率 evaluation_interval: 15s # 执行 rules 的时间间隔 scrape_configs: - job_name: 'prometheus' scrape_interval: 5s static_configs: - targets: ['localhost:9090'] - job_name: 'mysql' static_configs: - targets: ['192.168.5.242:9104'] - targets: ['192.168.5.242:9100']
- job_name:任务名称 - honor_labels: 用于解决拉取数据标签有冲突,当设置为 true, 以拉取数据为准,否则以服务配置为准 - params:数据拉取访问时带的请求参数 - scrape_interval: 拉取时间间隔 - scrape_timeout: 拉取超时时间 - metrics_path: 拉取节点的 metric 路径 - scheme: 拉取数据访问协议 - sample_limit: 存储的数据标签个数限制,如果超过限制,该数据将被忽略,不入存储;默认值为0,表示没有限制 - relabel_configs: 拉取数据重置标签配置 - metric_relabel_configs:metric 重置标签配置
version: '3' services: prometheus: user: "root" container_name: prometheus image: prom/prometheus:v2.2.1 command: - --log.level=error - --storage.tsdb.path=/data/prometheus - --config.file=/etc/prometheus/prometheus.yml ports: - "9090:9090" volumes: - ./config/prometheus.yml:/etc/prometheus/prometheus.yml:ro - ./data:/data restart: on-failure
version: '3' services: grafana: user: "0" container_name: grafana image: grafana/grafana:6.0.1 environment: GF_LOG_LEVEL: info GF_PATHS_PROVISIONING: /etc/grafana/provisioning GF_PATHS_CONFIG: /etc/grafana/grafana.ini volumes: - ./data/grafana:/var/lib/grafana ports: - "3000:3000" restart: on-failure
这样一个基础的监控平台就搭建完成了。