Linux云计算架构-搭建prometheus+grafana云平台监控系统

Linux云计算架构-搭建prometheus+grafana云平台监控系统

1. prometheus介绍

prometheus(普罗米修斯),是一套结合了监控、报警、时间序列数据库为一体的开源监控系统。常用于doker、mesos、kubernetes容器管理系统中。

监控原理:通过http协议周期性的获取被监控组件的状态。被监控组件只需提供相应的http接口即可,不需要任何的SDK或者其他的集成过程。

被监控组件提供状态信息的http接口称之为exporter
目前互联网公司大部分的组件都有exporter可以直接使用。

相对于nagios和zabbix的监控界面,prometheus的监控界面更为美观。

2. prometheus架构原理

promethues架构是promethues及其一些生态系统组件的整体架构。

2.1 prometheus架构组成

在这里插入图片描述

prometheus server:是prometheus组件的核心部分,负责获取监控数据,并对其存储和查询。可以通过静态配置管理监控目标,也可以配置service discovery(服务发现)的方式动态管理监控目标,并从这些目标中获取数据。prometheus server是一个实时数据库,将采集的监控数据以时间序列的方式存储到本地磁盘中。对外提供自定义的promQL,实现对数据的查询和分析。prometheus server还可以从其他的prometheus server实例中获取数据。

exporter:被监控组件提供监控数据的接口,使得监控数据通过http服务的形式展示给prometheus server。而prometheus server通过访问exporter提供的endpoint端口,就可以获取到监控数据。
exporter一般分为2类:
①直接采集:支持prometheus,直接采集被监控组件的信息。
②间接采集:不直接支持prometheus,通过prometheus提供的针对该监控组件的监控采集程序来采集被监控组件的数据。

alertmanager:在prometheus server中支持基于promQL创建告警规则,如果满足promQL定义的规则,就会产生告警。常见接受方式为:邮件,webhook。【类似zabbix中的触发器】

扫描二维码关注公众号,回复: 11710764 查看本文章

pushgateway:prometheus数据采集基于prometheus server从exporter中pull(拉)数据,这必须保持prometheus和exporter之间的通信。若不允许两者之间的通信,可以使用pushgateway来进行中转,即被监控组件主动推数据到pushgateway上,然后pushgateway再将数据发送到promethues server。

2.2 prometheus架构工作流

  1. promethues server获取metrics(指标)
    ①从配置好的jobsexporter中拉取metrics
    ②接受pushgateway发送过来的metrics
    ③从其他promethues server中拉取metrics

  2. promethues server处理metrics
    ①本地存储metrics
    ②运行定义好的alerts.rules
    ③满足规则,向alertmanager推送警报;不满足规则,仅记录新的时间序列。

  3. alertmanager处理警报

    ①根据配置文件,对接受的警报进行处理,发出告警。

  4. 图形界面对数据进行可视化。

3. 部署promethues云平台监控系统

prometheus云平台监控系统拓扑图:
在这里插入图片描述

3.1 下载安装包

# 下载以下6个tar包,国内的网不一定下的到。
# 下载地址:https://prometheus.io/download/
# premetheus server和alertmanager下载
https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz

https://github.com/prometheus/alertmanager/releases/download/v0.21.0/alertmanager-0.21.0.linux-amd64.tar.gz

# mysqld_exporter、node_exporter、pushgateway下载
https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz

https://github.com/prometheus/pushgateway/releases/download/v1.2.0/pushgateway-1.2.0.linux-amd64.tar.gz

# grafana下载
https://dl.grafana.com/oss/release/grafana-7.1.3-1.x86_64.rpm

# 这个地址比较快
https://mirrors.huaweicloud.com/grafana/7.1.3/grafana-7.1.3.linux-amd64.tar.gz

[root@master prometheus]# pwd
/root/prometheus
[root@master prometheus]# ls
alertmanager-0.21.0.linux-amd64.tar.gz     node_exporter-1.0.1.linux-amd64.tar.gz
grafana-7.1.3.linux-amd64.tar.gz           prometheus-2.20.1.linux-amd64.tar.gz
mysqld_exporter-0.12.1.linux-amd64.tar.gz  pushgateway-1.2.0.linux-amd64.tar.gz

3.2 部署prometheus server

# 安装epel扩展源
[root@master ~]# yum install epel-release -y

# 安装go语言环境
# go(golang)是goolge开发的一种静态强类型、编译型、并发型、具有垃圾回收功能的编程语言。
[root@master ~]# yum install go -y
[root@master ~]# go version
go version go1.13.14 linux/amd64

# 直接解压到/usr/local/目录下即可使用
[root@master ~]# tar xzf /root/prometheus/prometheus-2.20.1.linux-amd64.tar.gz -C /usr/local/
[root@master ~]# ll -d /usr/local/prometheus-2.20.1.linux-amd64/
drwxr-xr-x. 4 3434 3434 144 8月   6 03:42 /usr/local/prometheus-2.20.1.linux-amd64/

# 修改prometheus的配置文件,最后加即可。
# yml语法注重缩进
vim prometheus.yml
  - job_name: system-status              # 监控项,监控系统状态
    static_configs:          
      - targets: ['192.168.8.178:9100']  # 监控主机IP和端口号
        labels:
          instance: server-status
  - job_name: mysql-status               # 监控mysql状态
    static_configs:
      - targets: ['192.168.8.178:9104']  #监控主机IP和端口号
        labels:
          instance: server-mysql         # 实例名,用于grafana

# 启动prometheus,并放到后台运行
[root@master ~]# cd /usr/local/prometheus-2.20.1.linux-amd64/
[root@master prometheus-2.20.1.linux-amd64]# ./prometheus --config.file=prometheus.yml &

===========================================
&              前台进程放到后台运行
jobs           查看当前的后台进程
fg 后台序号     后台进程放到前台运行
ctrl+z         前台进程放到后台运行,并停止
bg             后台进程继续运行
ctrl+c         退出前台进程
===========================================

# 如果不用&,表示前台运行。
# 看到下面这句就说明启动成功了
level=info ts=2020-08-17T14:17:25.259Z caller=main.go:652 msg="Server is ready to receive web requests."
# 按了ctrl+c退出,即关闭了。
level=info ts=2020-08-17T14:21:12.348Z caller=main.go:767 msg="See you next time!"

# 由于prometheus服务器占用9090号端口,故防火墙要开放9090号端口
[root@master ~]# firewall-cmd --permanent --zone=public --add-port=9090/tcp
success
[root@master ~]# firewall-cmd --reload 
success

# 查看prometheus的web界面
http://192.168.8.177:9090/

在这里插入图片描述
由于还未配置node和mysql的exporter,故获取不到数据。
在这里插入图片描述

[root@master prometheus-2.20.1.linux-amd64]# 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']
    
  - job_name: system-status              # 监控项,监控系统状态
    static_configs:          
      - targets: ['192.168.8.178:9100']  # 监控主机IP和端口号
        labels:
          instance: server-status
  - job_name: mysql-status               # 监控mysql状态
    static_configs:
      - targets: ['192.168.8.178:9104']  #监控主机IP和端口号
        labels:
          instance: server-mysql         # 实例名,用于grafana

# 监控多个IP地址
['192.168.8.100:9100','192.168.8.101:9100']

3.3 部署exporter(出口节点)

常用exporter:
node_exporter监控系统性能和运行状态
mysqld_exporter监控mysql数据库服务
snmp_exporter监控网络设备

# 在被监控主机上配置以下2个exporter出口节点,即接口。用于采集被监控主机上对应的状态。
# 传输tar包
[root@master prometheus]# scp /root/prometheus/node_exporter-1.0.1.linux-amd64.tar.gz 192.168.8.178:/opt/
[root@master prometheus]# scp /root/prometheus/mysqld_exporter-0.12.1.linux-amd64.tar.gz 192.168.8.178:/opt/

1. 配置node_exporter
[root@client ~]# tar xzf /opt/node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local/
[root@client ~]# /usr/local/node_exporter-1.0.1.linux-amd64/node_exporter &
[root@client ~]# netstat -antup | grep 9100
tcp6       0      0 :::9100                 :::*                    LISTEN      10098/node_exporter 
tcp6       0      0 192.168.8.178:9100      192.168.8.177:52894     ESTABLISHED 10098/node_exporter 

2. 配置mysqld_exporter
# 如果有类似报错:Access denied for user 'zabbix'@'localhost' (using password: YES)
# 估计要重置对应用户密码。
[root@client ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> grant replication client,process on *.* to 'mysql_monitor'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.05 sec)

MariaDB [mysql]> grant select on *.* to 'mysql_monitor'@'localhost';
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye

# 解压mysql_exportor
[root@client ~]# tar xzf /opt/mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
# 使用指定数据库用户进行收集数据
[root@client ~]# vim /usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.cnf
[root@client ~]# cat /usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.cnf 
[client]
user=mysql_monitor
password=123456
# 启动mysql_exportor,看到以下内容说明启动成功。
[root@client ~]# /usr/local/mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.cnf" &
[1] 20812
[root@client ~]# INFO[0000] Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96)  source="mysqld_exporter.go:257"
INFO[0000] Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58)  source="mysqld_exporter.go:258"
INFO[0000] Enabled scrapers:                             source="mysqld_exporter.go:269"
INFO[0000]  --collect.global_status                      source="mysqld_exporter.go:273"
INFO[0000]  --collect.global_variables                   source="mysqld_exporter.go:273"
INFO[0000]  --collect.slave_status                       source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmp             source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmpmem          source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.query_response_time    source="mysqld_exporter.go:273"
INFO[0000] Listening on :9104                            source="mysqld_exporter.go:283

可以看到,prometheus server已经可以从远程主机上获取到系统状态信息和mysql数据库信息了。都为up了。
在这里插入图片描述

# 查看下端口,监听正常
[root@client ~]# netstat -antup | grep 9100
tcp6       0      0 :::9100                 :::*                    LISTEN      10098/node_exporter 
tcp6       0      0 192.168.8.178:9100      192.168.8.177:50488     ESTABLISHED 10098/node_exporter 
[root@client ~]# netstat -antup | grep 9104
tcp6       0      0 :::9104                 :::*                    LISTEN      20812/mysqld_export 
tcp6       0      0 192.168.8.178:9104      192.168.8.177:39890     ESTABLISHED 20812/mysqld_export 

至此为止,prometheus云平台监控系统已经部署完毕了。

4. 使用grafana美化prometheus

如果监控看的是以上界面,那怎么可以说prometheus的界面比zabbix好看呢?这时需要使用grafana美化工具进行美化下。

4.1 安装grafana

[root@master ~]# ll /root/prometheus/grafana-7.1.3.linux-amd64.tar.gz 
-rw-r--r--. 1 root root 52611015 8月   6 12:55 /root/prometheus/grafana-7.1.3.linux-amd64.tar.gz
[root@master ~]# tar xzf /root/prometheus/grafana-7.1.3.linux-amd64.tar.gz -C /usr/local/
[root@master ~]# cd /usr/local/grafana-7.1.3/
# 后台运行grafana
[root@master grafana-7.1.3]# ./bin/grafana-server &
# 可以看到后台运行的已经有两个程序了。
[root@master grafana-7.1.3]# jobs
[1]-  运行中               ./prometheus --config.file=prometheus.yml &(工作目录:/usr/local/prometheus-2.20.1.linux-amd64)
[2]+  运行中               ./bin/grafana-server &

# 防火墙开放3000端口号
[root@master grafana-7.1.3]# firewall-cmd --permanent --zone=public --add-port=3000/tcp
success
[root@master grafana-7.1.3]# firewall-cmd --reload 
success

# 还是不懂的,可以参考以下网址:
https://grafana.com/docs/grafana/latest/installation/rpm/

访问:http://192.168.8.177:3000/login,可以看到以下内容。
初始账号密码:admin/admin
在这里插入图片描述
修改初始密码:
在这里插入图片描述
登录进来的首页如下:
在这里插入图片描述

4.2 美化prometheus

用ie打开这个grafana界面兼容性有问题。我这里用谷歌展示。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
添加系统状态信息和mysql数据库信息的仪表盘:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
通过以上查找,可以看到两个仪表盘的ID分别是:8919 7362
导入系统状态的仪表盘:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
导入mysql数据库信息的仪表盘:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以上就是系统状态和mysql数据库状态信息的监控面板了。

5. 效果图

5.1 系统状态

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.2 mysql数据库状态

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6. 调整仪表盘监控面板

导入仪表盘模板后,最好不要对其删除。先了解仪表盘内各监控内容,然后选择性的删除。因为想要添加是要写查询的,不好写。
在这里插入图片描述
以上就是prometheus监控系统的内容了,对于一些其他的监控实战,可以关注博主,后续会有。

猜你喜欢

转载自blog.csdn.net/weixin_36522099/article/details/108066934