• 初试 Prometheus + Grafana 监控系统搭建并监控 Mysql


    转载自:https://cloud.tencent.com/developer/article/1433280

    文章目录1、Prometheus & Grafana 介绍1.1、Prometheus 介绍1.2、Grafana 介绍2、环境、软件准备3、Prometheus 安装配置3.1、 二进制源码安装3.2、 Docker 镜像安装3.3、源码编译安装4、MySQL Server Exporter 安装配置5、Grafana 安装并配置 DataSources5.1、HomeBrew 安装5.2、二进制包安装5.3、配置 DataSources6、仪表盘配置(MySQL monitoring using Prometheus)

    1、Prometheus & Grafana 介绍

    1.1、Prometheus 介绍

    Prometheus 是一套开源的系统监控、报警、时间序列数据库的组合,最初有 SoundCloud 开发的,后来随着越来越多公司使用,于是便独立成开源项目。我们常用的 Kubernetes 容器集群管理中,通常会搭配 Prometheus 一起来进行监控。Prometheus 基本原理是通过 Http 协议周期性抓取被监控组件的状态,而输出这些被监控的组件的 Http 接口为 Exporter,现在各个公司常用的 Exporter 都已经提供了,可以直接安装使用,如 haproxy_exporterblockbox_exportermysqld_exporternode_exporter 等等,更多支持的组件可查看 这里

    Prometheus 官网架构图如下:

    img

    Prometheus 具有以下特点:

    • 是一个时间序列由指标名称和键值对定义的多维数据模型
    • 支持灵活的查询语言
    • 不依赖分布式存储,单个服务器节点就可以
    • 时间序列集合通过 Http 上的 Pull 模型来进行
    • 支持通过中间网关推送时间
    • 通过服务发现或静态配置来发现目标
    • 支持多种模式的图形和仪表盘

    1.2、Grafana 介绍

    Grafana 是一个可视化仪表盘,它拥有美观的图标和布局展示,功能齐全的仪表盘和图形编辑器,默认支持 CloudWatch、Graphite、Elasticsearch、InfluxDB、Mysql、PostgreSQL、Prometheus、OpenTSDB 等作为数据源。我们可以将 Prometheus 抓取的数据,通过 Grafana 优美的展示出来,非常直观。

    2、环境、软件准备

    本次演示环境,我是在本机 MAC OS 上操作,以下是安装的软件及版本:

    • go: go1.10.3 darwin/amd64
    • prometheus: 2.3.2.darwin-amd64
    • node_exporter: 0.16.0.darwin-amd64
    • mysqld_exporter: 0.11.0.darwin-amd64
    • grafana: 5.2.2.darwin-amd64

    注意:这里 prometheusnode_exportermysqld_exporter 都是 GO 语言开发,其运行环境依赖 GO,所以本机需要安装好 GO 环境,这里忽略 GO 的安装过程,着重介绍下其他几个的安装配置过程。实际应用中,如果运行在 Linux 环境下,以上各个软件也要安装系统对应版本。

    3、Prometheus 安装配置

    Prometheus 安装方式有多种,可以二进制源码安装、Docker 启动安装或源码编译安装。

    3.1、 二进制源码安装

    首先,下载 Prometheus 二进制源码安装包,可以访问 Prometheus download 页面下载指定版本。

    $ cd ~/tmp
    $ wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.darwin-amd64.tar.gz
    $ tar -zxvf prometheus-2.3.2.darwin-amd64.tar.gz
    $ tree prometheus-2.3.2.darwin-amd64
    	prometheus-2.3.2.darwin-amd64
    	|-- LICENSE
    	|-- NOTICE
    	|-- console_libraries
    	|   |-- menu.lib
    	|   `-- prom.lib
    	|-- consoles
    	|   |-- index.html.example
    	|   |-- node-cpu.html
    	|   |-- node-disk.html
    	|   |-- node-overview.html
    	|   |-- node.html
    	|   |-- prometheus-overview.html
    	|   `-- prometheus.html
    	|-- prometheus
    	|-- prometheus.yml
    	`-- promtool
    

    启动 Prometheus,使用默认配置文件 prometheus.yml 启动。

    $ cd prometheus-2.3.2
    $ ./prometheus
    level=info ts=2018-07-31T07:09:59.112915066Z caller=main.go:222 msg="Starting Prometheus" version="(version=2.3.2, branch=HEAD, revision=71af5e29e815795e9dd14742ee7725682fa14b7b)"
    level=info ts=2018-07-31T07:09:59.113018589Z caller=main.go:223 build_context="(go=go1.10.3, user=root@5258e0bd9cc1, date=20180712-14:08:00)"
    level=info ts=2018-07-31T07:09:59.11303192Z caller=main.go:224 host_details=(darwin)
    level=info ts=2018-07-31T07:09:59.11304525Z caller=main.go:225 fd_limits="(soft=4864, hard=9223372036854775807)"
    level=info ts=2018-07-31T07:09:59.113726479Z caller=main.go:533 msg="Starting TSDB ..."
    level=info ts=2018-07-31T07:09:59.114182265Z caller=web.go:415 component=web msg="Start listening for connections" address=0.0.0.0:9090
    level=info ts=2018-07-31T07:09:59.118617867Z caller=main.go:543 msg="TSDB started"
    level=info ts=2018-07-31T07:09:59.118678076Z caller=main.go:603 msg="Loading configuration file" filename=prometheus.yml
    level=info ts=2018-07-31T07:09:59.120520499Z caller=main.go:629 msg="Completed loading of configuration file" filename=prometheus.yml
    level=info ts=2018-07-31T07:09:59.120546718Z caller=main.go:502 msg="Server is ready to receive web requests."
    

    此时,Prometheus 服务就在本地启动起来了,浏览器访问 http://localhost:9090 访问 Web 页面。

    img

    Prometheus 默认提供一些 metric 指标来供我们选择监控,通过地址 http://localhost:9090/metric 可以看到所有指标列表。

    img

    例如,我们选择了 go_gc_duration_seconds_count 这个监控 go 执行 gc 持续时间秒数指标,点击 “Execute” 后,使用默认的 Promdash 页面展示,一段时间后,监控结果如下:

    img

    3.2、 Docker 镜像安装

    Docker 镜像启动很简单,只需要一条命令即可,使用默认配置的话,可以直接使用如下命令启动。

    $ docker run --name prometheus -d -p 127.0.0.1:9090:9090 prometheus/prometheus
    

    我们也可以使用自己的 prometheus.yml 配置文件,并且挂载数据目录到主机,来启动镜像。

    $ docker run --name prometheus -d -p 127.0.0.1:9090:9090 
    -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml 
    -v /tmp/prometheus-data:/prometheus-data 
    prom/prometheus
    

    3.3、源码编译安装

    源码编译安装,我们需要本地有一个运行的 GO 环境,而且版本不低于 1.10.x

    $ mkdir -p $GOPATH/src/github.com/prometheus
    $ cd $GOPATH/src/github.com/prometheus
    $ git clone https://github.com/prometheus/prometheus.git
    $ cd prometheus
    $ make build
    $ ./prometheus --config.file=your_config.yml
    

    4、MySQL Server Exporter 安装配置

    现在,Prometheus 服务本地已经启动了,接下来,我们需要安装并运行 Exporter,它的主要作用是持续输出监控的组件信息并格式化,同时提供 Http 接口供 Prometheus 服务来抓取。Exporter 也是通过 GO 语言编写的,Prometheus GitHub 已经为我们提供了很多实用的 Exporter,直接拿来使用即可。

    首先,下载 mysql_exporter 二进制文件到本地。

    $ cd ~/tmp
    $ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.darwin-amd64.tar.gz
    $ tar -zxvf mysqld_exporter-0.11.0.darwin-amd64.tar.gz
    $ tree mysqld_exporter-0.11.0.darwin-amd64
    	mysqld_exporter-0.11.0.darwin-amd64
    	├── LICENSE
    	├── NOTICE
    	└── mysqld_exporter 
    

    接着,需要配置要监控的数据库,为 mysql_exporter 创建一个指定账户用来连接数据库(当然,也可以直接用 root 或者其他有权限的账户,不过建议创建一个专有账户)。

    CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'password' WITH MAX_USER_CONNECTIONS 3;
    GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
    

    然后,需要配置一下数据库连接认证信息并启动 Exporter,有两种方式配置:

    • 使用环境变量方式 $ export DATA_SOURCE_NAME='exporter:password@(localhost:3306)/' $ ./mysqld_exporter
    • 使用配置文件方式
    $ vim ~/.my.cnf
    [client]
       host=localhost
       port=3306
       user=exporter
       password=password	
    
    $ ./mysqld_exporter
    

    注意:mysqld_exporter 默认使用 ~/.my.cnf 作为数据库认证文件,如果我们指定其他路径文件,需要启动时指定路径,例如:./mysqld_exporter --config.my-cnf="<file_path>/.my.cnf"

    查看日志输出,Exporter 启动成功。

    $ ./mysqld_exporter 
    INFO[0000] Starting mysqld_exporter (version=0.11.0, branch=HEAD, revision=5d7179615695a61ecc3b5bf90a2a7c76a9592cdd)  source="mysqld_exporter.go:206"
    INFO[0000] Build context (go=go1.10.3, user=root@3d3ff666b0e4, date=20180629-15:01:12)  source="mysqld_exporter.go:207"
    INFO[0000] Enabled scrapers:                             source="mysqld_exporter.go:218"
    INFO[0000]  --collect.slave_status                       source="mysqld_exporter.go:222"
    INFO[0000]  --collect.info_schema.tables                 source="mysqld_exporter.go:222"
    INFO[0000]  --collect.global_status                      source="mysqld_exporter.go:222"
    INFO[0000]  --collect.global_variables                   source="mysqld_exporter.go:222"
    INFO[0000] Listening on :9104                            source="mysqld_exporter.go:232"
    

    此时,本地浏览器访问 http://localhost:9104/metrics 可以看到 mysql 相关的所有监控指标列表。

    img

    现在,Prometheus 和 mysqld_exporter 服务都已经启动起来了,那么接下来就需要将二者关联起来,让 Prometheus 来定时抓取 Exporter 提供的数据。我们需要修改 Prometheus 的配置文件 prometheus.yml 增加 mysql 相关 exporter job。

    $ cd ~/tmp/prometheus-2.3.2.darwin-amd64
    $ vim 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:
    scrape_configs:
    
      - job_name: 'prometheus'
        static_configs:
        - targets: ['localhost:9090']
          labels:
              instance: prometheus
      
      - job_name: 'mysql'
        static_configs:
         - targets: ['localhost:9104']
           labels:
              instance: mysql
    

    说明一下:增加了一个 job_name 为 mysql 的任务,targets 为指向 mysqld_exporter 提供的 Http 接口服务,labels 为该 job 配置一个标签,方便下边 Grafana 页面显示,下边会讲到。

    修改完成后,重启 Prometheus 服务。此时,我们再次访问 http://localhost:9090/,点击 “Status” 下 “Targets” 可以查看所有的 Targets 了,这里就有默认的 prometheus 和新配置的 mysql 啦!

    img

    此时,我们就可以在下拉列表中选择 mysql 相关的指标监控了,例如选择 mysql_global_status_uptime 指标,一段时间后,监控页面如下:

    img

    不过,大家有没有觉得 Prometheus 自带的图形并不美观,而且功能单一,此时,Grafana 该上台了,我们要使用 Grafana 作为 Prometheus 的展示仪表盘,功能强大而且界面美观。

    5、Grafana 安装并配置 DataSources

    Grafana 安装也很方便,根据官网文档 Grafana Install,里面有针对各个系统提供的安装方法。参照 Mac 上安装方案,可以采用 HomeBrew 安装或者二进制源码安装方式。

    5.1、HomeBrew 安装

    # 安装 Grafana
    $ brew update
    $ brew install grafana
    
    # 安装 homebrew/services 服务
    $ brew tap homebrew/services
    # 启动 Grafana 服务
    $ brew services start grafana
    

    5.2、二进制包安装

    $ cd ~/tmp
    $ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.2.darwin-amd64.tar.gz 
    $ tar -zxvf grafana-5.2.2.darwin-amd64.tar.gz
    $ cd grafana-5.2.2
    $ ./bin/grafana-server web
    INFO[07-31|17:58:14] Starting Grafana                         logger=server version=5.2.2 commit=aeaf7b2 compiled=2018-07-25T19:17:28+0800
    INFO[07-31|17:58:14] Config loaded from                       logger=settings file=/Users/wanyang3/tmp/grafana-5.2.2/conf/defaults.ini
    INFO[07-31|17:58:14] Path Home                                logger=settings path=/Users/wanyang3/tmp/grafana-5.2.2
    INFO[07-31|17:58:14] Path Data                                logger=settings path=/Users/wanyang3/tmp/grafana-5.2.2/data
    INFO[07-31|17:58:14] Path Logs                                logger=settings path=/Users/wanyang3/tmp/grafana-5.2.2/data/log
    INFO[07-31|17:58:14] Path Plugins                             logger=settings path=/Users/wanyang3/tmp/grafana-5.2.2/data/plugins
    INFO[07-31|17:58:14] Path Provisioning                        logger=settings path=/Users/wanyang3/tmp/grafana-5.2.2/conf/provisioning
    INFO[07-31|17:58:14] App mode production                      logger=settings
    INFO[07-31|17:58:14] Initializing SqlStore                    logger=server
    INFO[07-31|17:58:14] Connecting to DB                         logger=sqlstore dbtype=sqlite3
    INFO[07-31|17:58:14] Starting DB migration                    logger=migrator
    INFO[07-31|17:58:14] Initializing SearchService               logger=server
    INFO[07-31|17:58:14] Initializing PluginManager               logger=server
    INFO[07-31|17:58:14] Starting plugin search                   logger=plugins
    INFO[07-31|17:58:14] Initializing InternalMetricsService      logger=server
    INFO[07-31|17:58:14] Initializing AlertingService             logger=server
    INFO[07-31|17:58:14] Initializing HTTPServer                  logger=server
    INFO[07-31|17:58:14] Initializing CleanUpService              logger=server
    INFO[07-31|17:58:14] Initializing NotificationService         logger=server
    INFO[07-31|17:58:14] Initializing ProvisioningService         logger=server
    INFO[07-31|17:58:14] Initializing RenderingService            logger=server
    INFO[07-31|17:58:14] Initializing TracingService              logger=server
    INFO[07-31|17:58:14] Initializing Stream Manager
    INFO[07-31|17:58:14] HTTP Server Listen                       logger=http.server address=0.0.0.0:3000 protocol=http subUrl= socket=
    

    启动完成后,本地浏览器访问 http://localhost:3000 即可打开 Grafana Web 页面,使用默认管理员账户: admin 密码:admin 登录并修改初始密码后,即可开始体验 Grafana,页面是不是很美观。

    img

    5.3、配置 DataSources

    接下来,我们需要添加一个 Prometheus 类型的数据源,将二者关联起来,点击 “Add data source” 按钮,填写 Prometheus 相关信息,如下:

    img

    Type 处选择 Prometheus,URL 处填写 Prometheus 连接地址,因为我们上边服务安装在本地,所以这里填写 http://localhost:9090,其他默认即可,填写完毕,点击下方 “Save & Test” 按钮,如果配置成功,会提示 Data source is working 消息。

    此时,已经添加好了 Prometheus 数据源,Grafana 默认给该类型监控提供了三个 Dashboard,我们可以直接在页面上点击 “Import” 即可,此时在 Dashboard 页面选择其中的一个类型,就可以看到监控数据图形啦!例如,这里我们选择 Prometheus 2.0 Stats 类型 Dashboard,一段时间后,看到的监控页面如下:

    img

    img

    6、仪表盘配置(MySQL monitoring using Prometheus)

    最后,我们需要在 Grafana 上添加 Mysql Prometheus 类型的 Dashboard,当然对应的 Dashboard Json 文件,可以直接拿现成的来用,别人写的比较全面,也比较好。这些 Dashboard Json 文件可以从 这里 找到,我们可以 Clone 到本地,导入到 Grafana 里面即可。

    $ cd ~/tmp/
    $ git clone https://github.com/percona/grafana-dashboards.git
    $ tree grafana-dashboards/dashboards/
    grafana-dashboards/dashboards/
    ├── Advanced_Data_Exploration.json
    ├── Amazon_RDS___Aurora_MySQL__Metrics.json
    ├── CPU_Utilization_Details_Cores.json
    ├── Cross_Server_Graphs.json
    ├── Disk_Performance.json
    ├── Disk_Space.json
    ├── Home_Dashboard.json
    ├── MongoDB_Cluster_Summary.json
    ├── MongoDB_InMemory.json
    ├── MongoDB_MMAPv1.json
    ├── MongoDB_Overview.json
    ├── MongoDB_ReplSet.json
    ├── MongoDB_RocksDB.json
    ├── MongoDB_WiredTiger.json
    ├── MySQL_Amazon_Aurora_Metrics.json
    ├── MySQL_Command_Handler_Counters_Compare.json
    ├── MySQL_InnoDB_Compression.json
    ├── MySQL_InnoDB_Metrics.json
    ├── MySQL_InnoDB_Metrics_Advanced.json
    ├── MySQL_MyISAM_Aria_Metrics.json
    ├── MySQL_MyRocks_Metrics.json
    ├── MySQL_Overview.json
    ├── MySQL_Performance_Schema.json
    ├── MySQL_Query_Response_Time.json
    ├── MySQL_Replication.json
    ├── MySQL_Table_Statistics.json
    ├── MySQL_TokuDB_Metrics.json
    ├── MySQL_User_Statistics.json
    ├── Network_Overview.json
    ├── Overview_NUMA_metrics.json
    ├── PXC_Galera_Cluster_Overview.json
    ├── PXC_Galera_Graphs.json
    ├── Prometheus.json
    ├── Prometheus_Exporter_Status.json
    ├── Prometheus_Exporters_Overview.json
    ├── ProxySQL_Overview.json
    ├── Summary_Dashboard.json
    ├── System_Overview.json
    ├── Trends_Dashboard.json
    ├── pmm-add-instance.json
    ├── pmm-qan.json
    ├── pmm-settings.json
    └── pmm-summary.json
    

    这里就是我们需要的 Dashboard Json 文件,包含 Mysql、MongoDB、System、pmm 等,可以直接拿来使用。点击 “Import” 进入页面后,点击 “Upload .json File” 按钮,选择本地 MySQL_Overview.json 文件,点击 “Import” 即可完成导入,

    img

    此时,我们就可以从页面上直观的看到 Prometheus 收集到的 Mysql 相关的各个指标监控的,例如:Mysql Connection、Mysql Questions、Mysql Thread Cache、Mysql Network Traffic 等等一系列指标。同时,还可以调整查询区间范围,编译每一个指标信息,查看每个指标信息详情,自定义拖拽排版面板等功能,功能很强大。

    img

    img

    还记得上边 Prometheus 配置文件中的 labels 么,我们配置为 mysql,在这里 Grafana 页面上边 Host 就自动关联了 mysql,如果没填 labels,这里会显示 localhost:9104,不是很直观的哈!

    参考资料

  • 相关阅读:
    软件包管理
    扩大swap分区
    逻辑卷加密(luks)
    创建一个独立的卷组
    dns视图搭建
    dns服务搭建
    [转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1)
    [转]ubuntu server上网配置
    [转]windows 软链接的建立及删除
    [转]Cygwin的包管理器:apt-cyg
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/13724529.html
Copyright © 2020-2023  润新知