prometheus 安装与展示

普罗米修斯监控

监控就是实时的帮助我们来监控或者探测我们部署的服务是否正常运行。
一、常用的监控
- zabbix
  - 组件比较全,缺点:性能不高
  - 当zabbix监控大型集群的时候,怎么优化
  - 当数据库中一个表中数据超过2000w的时候,数据库的性能急剧下降
  
- 阿里云云监控
- 腾讯云蓝鲸监控
- 普罗米修斯
  - 性能比较高,底层使用(时序数据库)
  - 原生支持监控容器
二、Promethus监控系统介绍
能够安装prometheus服务器
能够通过安装node_exporter监控远程linux
能够通过安装mysqld_exporter监控远程mysql数据库
能够安装grafana
能够在grafana添加prometheus数据源
能够在grafana添加监控cpu负载的图形
能够在grafana图形显示mysql监控数据

# 官网: https://prometheus.io/
下载链接: https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
普罗米修斯监控流程

普罗米修斯监控分为两种情况:

1、携带metrics接口的服务(kubernetes、ETCD、Docker)

2、不携带metrics接口的服务(Nginx、mysql、Linux主机),针对不携带metrics接口的服务,我们需要安装一个export插件

1.概叙
Prometheus(由go语言(golang)开发)是一套开源的监控&报警&时间序列数据库的组合。适合监控docker容器。因为kubernetes(俗称k8s)的流行带动了prometheus的发展。

在这里插入图片描述

时间序列数据
时间序列数据(TimeSeries Data) : 按照时间顺序记录系统、设备状态变化的数据被称为时序数据。
1.特点
# 性能好

关系型数据库对于大规模数据的处理性能糟糕。NOSQL可以比较好的处理大规模数据,让依然比不上时间序列数据库。

# 成本低

# 高效的压缩算法,节省存储空间,有效降低IO

Prometheus有着非常高效的时间序列数据存储方法,每个采样数据仅仅占用3.5byte左右空间,上百万条时间序列,30秒间隔,保留60天,大概花了200多G(来自官方数据)
2.特征
多维度数据模型
灵活的查询语言(PromQL)
不依赖分布式存储,单个服务器节点是自治的
以HTTP方式,通过pull模型拉去时间序列数据
也可以通过中间网关支持push模型
通过服务发现或者静态配置,来发现目标服务对象
支持多种多样的图表和界面展示
3.环境设置
主机名称ip服务
Prometheus172.16.1.125Grafana、prometheus
web01172.16.1.7node_exporter
web02172.16.1.7node_exporter
web03172.16.1.7node_exporter
db01172.16.1.51mysqld_exporter
4.prometheus服务端部署
- 从 https://prometheus.io/download/ 下载相应版本,安装到服务器上官网提供的是二进制版,解压就能用,不需要编译。
# 方式一:prometheus手动安装启动
# 1、下载
[root@prometheus opt]# wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz

# 2、解压
[root@prometheus opt]# tar -xf prometheus-2.25.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# cd /usr/local/

# 3、建立超链接
[root@prometheus local]# ln -s /usr/local/prometheus-2.25.0.linux-amd64 /usr/local/prometheus
[root@prometheus local]# ll
lrwxrwxrwx  1 root root  30 Jun  2 09:49 prometheus -> prometheus-2.25.0.linux-amd64/

# 4、创建环境变量
[root@prometheus local]# cat >> /etc/profile.d/prometheus.sh <<EOF
export PROMETHEUS_HOME=/usr/local/prometheus
PATH=$PATH:$PROMETHEUS_HOME
export PATH
EOF
[root@prometheus ~]# source /etc/profile

# 5、测试安装成功
[root@prometheus ~]# prometheus --version
prometheus, version 2.25.0 (branch: HEAD, revision: a6be548dbc17780d562a39c0e4bd0bd4c00ad6e2)
  build user:       root@615f028225c9
  build date:       20210217-14:17:24
  go version:       go1.15.8
  platform:         linux/amd64
  
# 6、查看配置文件(prometheus.yml)
[root@prometheus ~]# cd /usr/local/prometheus
[root@prometheus prometheus]# ll
total 167984
drwxr-xr-x 2 3434 3434       38 Feb 18 00:11 console_libraries
drwxr-xr-x 2 3434 3434      173 Feb 18 00:11 consoles
-rw-r--r-- 1 3434 3434    11357 Feb 18 00:11 LICENSE
-rw-r--r-- 1 3434 3434     3420 Feb 18 00:11 NOTICE
-rwxr-xr-x 1 3434 3434 91044140 Feb 17 22:19 prometheus
-rw-r--r-- 1 3434 3434     1170 Jun  2 18:16 prometheus.yml
-rwxr-xr-x 1 3434 3434 80948693 Feb 17 22:21 promtool

# 7、启动prometheus
[root@prometheus ~]# prometheus --config.file=/usr/local/prometheus/prometheus.yml


# 方式二:prometheus的systemd脚本执行安装启动
[root@openvpn files]# cat prometheus.sh
echo "1.下载"
cd /opt/ &&\

wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz &&\

echo "2.解压"
tar -xf /opt/prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local/ &&\

echo "3. 建立超链接"
ln -s /usr/local/prometheus-2.27.1.linux-amd64 /usr/local/prometheus &&\

echo "4.创建环境变量"
echo "export PATH=$PATH:/usr/local/prometheus/" >> /etc/profile.d/prometheus.sh &&\

echo "5.加载环境变量"
source /etc/profile &&\

echo "6.创建promethets的systemd启动文件"
cat >>/usr/lib/systemd/system/prometheus.service <<EOF
[Unit]
Description=https://prometheus.io

[Service]    
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml

[Install]
WantedBy=multi-user.target  
EOF
echo "7.启动promethets"
systemctl daemon-reload &&\
systemctl enable --now prometheus.service

# 访问
http://192.168.16.125:9090/
通过`http://服务器IP:9090/metrics`可以查看到监控的数据,在web主界面可以通过关键字查询监控项 
5、使用Garafana展示数据

Garafana是业内做数据展示挺好的一款产品

下载网站:https://grafana.com/get/?plcmt=top-nav&cta=downloads

[root@prometheus opt]# rz -E
[root@prometheus opt]# ll
-rw-r--r-- 1 root root 53727312 Jun  2 11:21 grafana-7.3.6-1.x86_64.rpm

[root@prometheus opt]# yum install -y grafana-7.3.6-1.x86_64.rpm

[root@prometheus opt]# systemctl start grafana-server.service

[root@prometheus opt]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name                       
tcp6       0      0 :::3000                 :::*                    LISTEN      10137/grafana-serve 

#访问
HTTPS://172.16.1.81:3000

在这里插入图片描述

6.测试连接

在浏览器上输入:http://ip:9090,出现如下页面及表示启动成功。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OqVJWV9i-1622889846353)(C:\Users\17155\Desktop\下载图片\1622598243380.png)]

7.web客户端部署
1、安装node_exporter组件
# 在被web集群上安装node_exporte脚本执行安装组件

# 在web集群三台机器分别执行该脚本
[root@web01files]# cat node.sh  
echo "1.下载node包"
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz

echo "2.解压"
tar -xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
echo "3. 建立超链接"
ln -s /usr/local/node_exporter-1.1.2.linux-amd64/ /usr/local/node_exporter &&\

echo "4.创建systemd服务"
cat > /etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=This is prometheus node exporter
After=node_exporter.service

[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
ExecReload=/bin/kill -HUP
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
echo "5.启动node_exporter服务"
systemctl daemon-reload &&\
systemctl enable --now node_exporter.service

参数:
	--web.listen-address=":9100" 	#修改默认端口,防止冲突
	--web.telemetry-path="/metrics" #获取metric信息的url,默认是/metrics,若需要修改则通过此参数
    --log.level="info" 			    #设置日志级别
    --log.format="logger:stderr"     #设置打印日志的格式,若有自动化日志提取工具可以使用这个参数规范日志打印的格式
2、回到prometheus服务器的配置文件里添加被监控机器的配置段
在主配置文件最后加上下面三行
  - job_name: 'agent01'
    static_configs:
      - targets: ['172.16.1.7:9100']

改完配置文件后,重启服务
[root@k8s-master-01 opt]# /usr/local/prometheus-2.25.0/prometheus --config.file="/usr/local/prometheus-2.25.0/prometheus.yml" &

回到web管理界面 --> 点Status --> 点Targets --> 可以看到多了两台监控目标
8.监控MySQL
1、安装mysqld_exporter组件
# 在db01机器上安装mysqld_exporter组件
# 方式一: mysqld_exporter手动安装组件
# 1、上传解压包
[root@db01 opt]# rz -E
-rw-r--r-- 1 root root 7121565 Apr 22 21:33 mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@db01 opt]# tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
[root@db01 opt]# cd /usr/local/
[root@db01 local]# ll
drwxr-xr-x  2 3434 3434 58 Jul 29  2019 mysqld_exporter-0.12.1.linux-amd64
# 2、软连接
[root@db01 local]# ln -s /usr/local/mysqld_exporter-0.12.1.linux-amd64/ /usr/local/mysqld_exporter
lrwxrwxrwx  1 root root 46 Jun  2 17:47 mysqld_exporter -> /usr/local/mysqld_exporter-0.12.1.linux-amd64/

# 3、创建用户并授权以及查看用户权限
[root@db01 ~]# mysql -uroot -p123

MariaDB [(none)]> create user 'root'@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> grant process, replication client, select on *.* to 'root'@'%';
Query OK, 0 rows affected (0.01 sec)

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]> select host,user from user;
+------------+----------+
| host       | user     |
+------------+----------+
| %          | root |
| 127.0.0.1  | root     |
| 172.16.1.% | www      |
| ::1        | root     |
| db01       |          |
| db01       | root     |
| localhost  |          |
| localhost  | root     |
+------------+----------+
8 rows in set (0.00 sec)
# 4、添加文件
# 创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名 和密码要对应)
[root@db01 mysqld_exporter]# vim .my.cnf
[client]
host=172.16.1.51
user=root
password=123
# 5、启动并查看监控是否成功(9104端口)
[root@db01 mysqld_exporter]# ./mysqld_exporter  --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf"


# 方式二: mysqld_exporter脚本安装组件
[root@openvpn files]# cat mysqld_exporter.sh 
echo "1.上传解压包 ”
[root@db01 opt]# rz -E
-rw-r--r-- 1 root root 7121565 Apr 22 21:33 mysqld_exporter-0.12.1.linux-amd64.tar.gz
echo "2.解压"
[root@db01 opt]# tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
[root@db01 opt]# cd /usr/local/
[root@db01 local]# ll
drwxr-xr-x  2 3434 3434 58 Jul 29  2019 mysqld_exporter-0.12.1.linux-amd64
echo "3. 建立超链接"
ln -s /usr/local/mysqld_exporter-0.12.1.linux-amd64/ /usr/local/mysqld_exporter &&\
echo "4.编辑my.cnf"

cat >> /usr/local/mysqld_exporter/.my.cnf <<EOF
[client]
host=172.16.1.51
user=root
password=123
EOF

echo "5.创建systemdqldmysqld_exporter.service务"
cat >> /usr/lib/systemd/system/mysqld_exporter.service <<EOF
[Unit]
Description=Prometheus

[Service]
Environment=DATA_SOURCE_NAME=root:123@(172.16.1.51:3306)/
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf --web.listen-address=:9104
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

echo "6.启动node_exporter服务"
systemctl daemon-reload &&\
systemctl enable --now mysqld_exporter.service

# 普罗米修斯是通过mysql_exporter来找mariadb进行获取数据的,所以要给它授权(用户名可自定义,与.my.cnf对应)
# (注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb。所以这个localhost是指的mysql_exporter的IP)
2、回到prometheus服务器的配置文件里添加被监控机器的配置段
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml 
  - job_name: 'Mysql server'
    static_configs:
      - targets:
        - "172.16.1.51:9104"
#重载prometheus
[root@prometheus ~]# prometheus --config.file=/usr/local/prometheus/prometheus.yml

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

9、补充

Error updating options: e.replace is not a function

# 1、方式一
https://zhuanlan.zhihu.com/p/598390674
# 2、方式二:
https://blog.csdn.net/weixin_43317099/article/details/104150098
1、grafana dashboard

https://grafana.com/grafana/dashboards/

2、grafna 展示模块 (8919)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FikL919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值