Vagrant环境下配置node_exporter、mysqld_exporter、prometheus、grafana

零、Vagrant环境

PS E:vagrantprometheus> dir

    目录: E:vagrantprometheus

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2021/8/1     12:24                .vagrant
d-----         2021/5/31     15:37                mysqld_exporter
-a----          2021/8/1     22:04        3798472 golang-github-prometheus-node_exporter-0.18.1-6.el7.x86_64.rpm
-a----          2021/8/2      0:39            129 grafana_start.sh
-a----          2021/8/1     17:53            266 mysql.sql
-a----          2021/8/1     19:49        7242382 mysqld_exporter-0.13.0.linux-amd64.tar.gz
-a----          2021/8/1     20:34            162 mysqld_exporter_start_deamon.sh
-a----          2021/8/1     23:50           1102 prometheus.yml
-a----          2021/8/1     17:58            118 prometheus_start.sh
-a----          2021/8/1     23:52           4668 Vagrantfile

    目录: E:vagrantprometheusmysqld_exporter

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2021/5/31     15:36          11357 LICENSE
-a----         2021/5/31     15:30       14955898 mysqld_exporter
-a----         2021/5/31     15:36             65 NOTICE

一、Vagrant

file: Vagrant

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.

  # 预装 Guest Additions Version: 5.2.44
  config.vm.box = "generic/centos7"
  config.vm.hostname = "prometheus"


  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network "public_network", ip: "192.168.1.13", bridge: "Intel(R) Wi-Fi 6 AX200 160MHz"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"
  config.vm.synced_folder ".", "/vagrant"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false
  
    # Customize the amount of memory on the VM:
    vb.memory = "4096"
    vb.cpus = "4"
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL

      # 关闭防火墙
      systemctl stop firewalld
      systemctl disable firewalld

      # 关闭selinux
      sed -i 's/enforcing/permissive/' /etc/selinux/config  # 永久
      setenforce 0  # 临时

      # 安装docker
      yum install -y docker vim wget
      systemctl enable docker
      systemctl start docker

      # 拉取相关镜像
      # docker pull mysql
      # docker pull prom/prometheus
      # docker pull prom/mysqld-exporter
      # docker pull grafana/grafana

      # 1、node_exporter:
      # 链接:https://github.com/prometheus/node_exporter#rhelcentosfedora

      # 使用yum安装
      # curl -Lo /etc/yum.repos.d/_copr_ibotty-prometheus-exporters.repo https://copr.fedorainfracloud.org/coprs/ibotty/prometheus-exporters/repo/epel-7/ibotty-prometheus-exporters-epel-7.repo
      # yum -y install node_exporter

      # 使用rpm安装
      rpm -ivh /vagrant/golang-github-prometheus-node_exporter-0.18.1-6.el7.x86_64.rpm

      # 启动node_exporter
      systemctl enable node_exporter
      systemctl start node_exporter

      # 2、MySQL8

      # 安装 & 启动
      rpm -ivh https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
      yum install -y mysql-community-server
      systemctl enable mysqld
      systemctl start mysqld

      # 3、mysqld_exporter

      # 下载到/home/vagrant
      # wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz


  SHELL
end

二、node_exporter

注:已经设置开机启动
https://prometheus.io/download/
https://github.com/prometheus/node_exporter

三、MySQL8

注:已经设置开机启动

# 查找mysql初始密码
$ sudo grep 'temporary password' /var/log/mysqld.log

四、mysqld_exporter

https://prometheus.io/download/
https://github.com/prometheus/mysqld_exporter

# 使用初始密码登录
mysql -uroot -p$PASSWORD

file: mysql.sql

SET GLOBAL validate_password.policy=LOW;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'redhat.com';
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

file: mysqld_exporter_start_deamon.sh

export DATA_SOURCE_NAME='exporter:XXXXXXXX@(prometheus:3306)/'
chmod u+x /vagrant/mysqld_exporter/mysqld_exporter
nohup /vagrant/mysqld_exporter/mysqld_exporter &

五、Prometheus

https://github.com/prometheus/prometheus
https://prometheus.io/docs/prometheus/latest/installation/

file: 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: ['192.168.1.13:9090']

  - job_name: 'agent_node'
    static_configs:
    - targets: ['192.168.1.13:9100']

  - job_name: 'agent_mysql8'
    static_configs:
    - targets: ['192.168.1.13:9104']

file: prometheus_start.sh

docker run -d 
    -p 9090:9090 
    -v /vagrant/prometheus.yml:/etc/prometheus/prometheus.yml 
    prom/prometheus

六、Grafana

https://grafana.com/docs/grafana/latest/installation/docker/

file: grafana_start.sh

docker run -d 
  -p 3000:3000 
  -e "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource" 
  grafana/grafana

配置操作

  1. 添加prometheus数据源
  2. 添加 https://grafana.com/grafana/dashboards/7362
  3. 添加 https://grafana.com/grafana/dashboards/5984
  4. 添加 https://grafana.com/grafana/dashboards/10180

七、视频参考

https://www.bilibili.com/video/BV16J411z7SQ

原文地址:https://www.cnblogs.com/echo1937/p/15088216.html