Linux tar包安装 Prometheus 和 Grafana(知识点:systemd Unit/重定向)

0. 介绍

用tar包的方式安装 Prometheus 和 Grafana

  • Prometheus:开源的监控方案
  • Grafana:将Prometheus的数据可视化平台

Prometheus已经有了查询功能为什么还需要grafana呢?Prometheus基于promQL这一SQL方言,有一定门槛!Grafana基于浏览器的操作与可视化图表大大降低了理解难度


1. Prometheus

1. 下载 与 解压
2. 启动与暂停
  • 进入解压后的文件夹:cd prometheus-*
    ll命令可以发现可执行文件 prometheus 和 prometheus.yml ,分别是启动文件和配置文件

启动prometheus我们可以编写systemd unit 服务,也可以直接nohup &直接挂起

2.1 挂起后台启动:

nohup ./prometheus --config.file=prometheus.yml --web.enable-admin-api --web.enable-lifecycle > nohup.out 2>&1 &

  • --web.enable-admin-api: 开启API服务,为下个参数动态加载配置打基础
  • --web.enable-lifecycle : 这个配置后,可以动态加载配置文件而无需重启prometheus,具体命令是 curl -X POST Prometheus所在机器ip:Prometheus监控的端口/-/reload
  • 2>&1 :标准错误输出重定向标准输出, &>filename 可以实现也是一样的效果.2>&1 是旧shell写法兼容性更高点
  • nohup ...&:
    • & 只是将命令置于后台,但是命令仍与终端窗口关联.导致默认情况下,命令的标准输出和标准错误输出仍然连接到终端;
    • nohup 将命令放入后台运行,并且它会将命令的标准输出和标准错误输出重定向到一个名为 nohup.out 的文件中,这样即使你关闭终端,命令也会继续运行,并且输出会写入到 nohup.out 文件中。
    • 看起来nohup拥有了 &的效果,为什么还用&?一方面是 &比nohup更兼容 另一方面是 单独nohup后,你需要手动 ctrl+z 将命令挂起, 配合 &可以马上放入后台运行~

ps -ef|grep prometheus 命令可以查看prometheus进程信息

2.2 systemd service 启动

创建prometheus.service 文件,不熟悉systemd定时器可以去看看阮老大文章Systemd 定时器教程

,这里我的prometheus 在/opt下,各位注意换成自己的路径

bash 复制代码
[Unit]
Description=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus.yml --web.enable-admin-api --web.enable-lifecycle
Restart=on-failure 
[Install]
WantedBy=multi-user.target

将上述文件保存到 /etc/systemd/system 目录后,输入下列命令

#1.加载系统服务

sudo systemctl daemon-reload

#2.启动服务

sudo systemctl start prometheus.service

#3.设置为系统自启动

sudo systemctl enable prometheus.service

#4 .查看状态

sudo systemctl status prometheus.service

下面是systemd其他常用命令

停止服务

sudo systemctl stop prometheus.service

关闭自启动

sudo systemctl disable prometheus.service

3. web查看

浏览器 打开 Prometheus所在机器ip:9090 (默认端口9090)

4. 修改配置文件yml

先说两个辅助命令:

  1. Prometheus 根目录下自带了一个 检查配置文件是否正确的小工具 promtool,
    使用语法: ./promtool check config prometheus.yml
  2. 之前启动命令配置了 热启动(enable-lifestyle),所以我们可以通过命令热更新配置文件无需重启整个Prometheus curl -X POST ip:port/-/reload

2. Grafana

Grafana 和 Prometheus安装步骤类似,官网文档 https://grafana.com/docs/grafana/latest

2.1 下载 和安装
2.2 启动和停止服务

grafana的启动脚本在 根目录下的bin文件夹,叫 grafana-server

启动,这里只写了nohup命令,systemd 的server文件参考上面的Prometheus的

nohup ./bin/grafana-server 2>&1 &

停止服务,nohup就kill 掉,systemd 就 systemctl stop xx.service

3. web查看

浏览器 打开 grafana安装机器ip:3000 (默认端口3000),第一次登录用户/密码均是 admin,之后按提示更改密码即可

4. grafana配置文件修改

待补充

5. grafana模板

grafana 模板可以在 https://grafana.com/grafana/dashboards/ 寻找,

之后在grafana左侧sidebar的dashboards->import 面板导入使用


3.现在的不足

我们现在有了Prometheus 和Grafana~

但是Prometheus去哪里抓取数据呢?(Prometheus是pull模型)因此有了各种各样的export用于对主机进行数据刮削,需要在被监控的主机中按需按照export

现在的链路是 export(刮削数据)->Prometheus主动抓取export(export注册发现?配置在Prometheus的配置文件中)->grafana(导入Prometheus数据源后即可展示)

现在还缺什么?告警组件,监控平台除开数据的展示外,另一个重要的功能就是当某些数据达到阈值后进行主动告警!

Prometheus生态下的告警组件是 alertmanager,但是不包含Prometheus中,需要你额外安装配置,prometheus server获取监控指标,基于这些指标定义规则(rules),若这些指标满足告警规则便将信息推送到Alertmanager

alert manager的编写阈值规则稍微有些复杂,但是有 https://samber.github.io/awesome-prometheus-alerts/rules.html 这样的前人分析了规则供我们借鉴的网站,所以还好

grafana web界面也可以配置alert,但是没研究过

上述介绍就放到后续文章中吧!

相关推荐
不做无法实现的梦~22 分钟前
Linux 上使用 CLion 开发嵌入式,并用 Codex CLI
linux·运维·服务器
张32340 分钟前
Ansible拆分大型Playbook
linux·ansible
苦逼大学生被编程薄纱2 小时前
Ext 文件系统基础:Linux 存储基石入门(下)
linux·运维·服务器
Lumos_7772 小时前
Linux -- 进程
linux·运维·服务器
南境十里·墨染春水2 小时前
linux学习进展 进程间通讯——共享内存
linux·数据库·学习
小此方3 小时前
Re:Linux系统篇(五)指令篇 ·四:shell外壳程序及其工作原理
linux·运维·服务器
其实防守也摸鱼3 小时前
sqlmap下载和安装保姆级教程(附安装包)
linux·运维·服务器·测试工具·渗透测试·攻防·护网行动
jingyu飞鸟3 小时前
Linux系统发送邮件,解决信誉等级低问题 docker compose修改启动一键使用
linux·运维·docker
Lumos_7773 小时前
Linux -- exec 进程替换
linux·运维·chrome
ElfBoard5 小时前
飞凌精灵(ElfBoard)技术贴|如何在RK3506开发板上实现UART功能复用
大数据·linux·人工智能·驱动开发·单片机·嵌入式硬件·物联网