0. 介绍
用tar包的方式安装 Prometheus 和 Grafana
- Prometheus:开源的监控方案
- Grafana:将Prometheus的数据可视化平台
1. Prometheus
1. 下载 与 解压
- 官网下载: https://prometheus.io/download/#prometheus
- 上传至机器
- 解压命令:tar -xzf prometheus-*.tar.gz
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 文件
[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
- Type=simple:该服务是个简单基本的服务,一旦启动命令被执行,systemd 将认为服务已经启动完成,不会监视服务进程的运行状态或退出。对于启动后会一直运行的服务(如守护进程)非常适用,因为它们会在后台运行而不会立即退出。
- Restart:指定服务在失败或退出后是否自动重启.no/always/on-failure(非零退出代码(失败)退出时)/on-abnormal(服务以异常退出时(如由信号终止))
3. web查看
浏览器 打开 Prometheus所在机器ip:9090
(默认端口9090)