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,但是没研究过

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

相关推荐
迷路爸爸1802 分钟前
源码编译安装最新 tmux 教程(含 Debian/Ubuntu/CentOS/Arch/macOS 等系统)
linux·ubuntu·macos·centos·debian·tmux·archlinux
励志不掉头发的内向程序员14 分钟前
【Linux系列】掌控 Linux 的脉搏:深入理解进程控制
linux·运维·服务器·开发语言·学习
東雪蓮☆18 分钟前
K8s Ingress 详解与部署实战
linux·运维·kubernetes
望获linux27 分钟前
【实时Linux实战系列】实时 Linux 在边缘计算网关中的应用
java·linux·服务器·前端·数据库·操作系统
聆风吟º1 小时前
无需 VNC / 公网 IP!用 Docker-Webtop+cpolar,在手机浏览器远程操控 Linux
linux·运维·docker
deng-c-f1 小时前
Linux C/C++ 学习日记(22):Reactor模式(二):实现简易的webserver(响应http请求)
linux·c语言·网络编程·reactor·http_server
BTU_YC1 小时前
CentOS 7 虚拟IP配置指南:使用传统network-scripts实现高可用
linux·tcp/ip·centos
陌路201 小时前
LINUX14 进程间的通信 - 管道
linux·网络
大聪明-PLUS2 小时前
从头开始为 ARM 创建 Ubuntu 映像
linux·嵌入式·arm·smarc
chenzhou__2 小时前
MYSQL学习笔记(个人)(第十五天)
linux·数据库·笔记·学习·mysql