目前无论是传统软件还是云部署都需要有一套完善的监控体系去监控系统的状态。目前比较常见的监控系列就是Prometheus+grafana+alertmanger,当然,如果监控指标量级比较大,可以参考使用Thanos等方案存储,也可以借鉴官网的联邦机制,本文就不多赘述了,后面有机会再介绍下。
本文主要是以安装为重要内容。
首先现在常用的安装方式,一种是原生安装,即直接安装在虚拟机或者物理机上;另一种是容器化安装,目前容器化安装有2种,一种是基于k8s安装,一种是单纯基于docker安装在虚拟机。
先讲原生安装:
- prometheus安装,首先下载prometheus二进制文件,prometheus.io/download/ 可以下载对应的文件,本文以linux系统举例:
wget https://github.com/prometheus/prometheus/releases/download/v2.39.1/prometheus-2.39.1.linux-amd64.tar.gz
tar -zxvf prometheus-2.39.1.linux-amd64.tar.gz
cd prometheus-2.39.1.linux-amd64
nohup ./prometheus --config.file=prometheus.yml & ##后台运行
生产推荐使用systemctl启动,可以配置开机自启动和异常拉起以及资源限制,简单配置如下:
vi /etc/systemd/system/prometheus.service
.....
[Unit]
Description=prometheus
[Service]
User=root
#启动路径,配置真实的路径
ExecStart=/root/prometheus-2.39.1.linux-amd64/prometheus --config.file=/root/prometheus-2.39.1.linux-amd64/prometheus.yml
#关闭prometheus
ExecStop=/usr/bin/killall prometheus
MemoryHigh=300M
#限制内存使用最多400M
MemoryMax=400M
#限制CPU使用最多一个核
CPUQuota=100%
#非正常退出重启
Restart=on-failure
#配置重新启动服务之前的睡眠时间,采用以秒为单位
RestartSec=1
[Install]
WantedBy=multi-user.target
.....
#配置加载
systemctl daemon-reload
#开机自启动
systemctl enable prometheus.service
#服务启动
systemctl start prometheus.service
#查看服务状态
systemctl status prometheus.service
2.grafana安装,可以看grafana.com/grafana/dow… 选择合适的安装包,本文就以二进制安装包举例:
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.2.0.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-9.2.0.linux-amd64.tar.gz
cd grafana-9.2.0
nohup ./bin/grafana-server web & ##后台运行
生产推荐使用systemctl启动,参考上面的prometheus配置即可。 3.alertmanager安装,在prometheus.io/download/ 下载对应版本的alertmanager的二进制包
wget https://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanager-0.24.0.linux-amd64.tar.gz
tar -zxvf alertmanager-0.24.0.linux-amd64.tar.gz
cd alertmanager-0.24.0.linux-amd64
nohup ./alertmanager --config.file=alertmanager.yml &
生产推荐使用systemctl启动,参考上面的prometheus配置即可。
容器安装:
-
基于docker安装:
本文介绍使用docker-compose安装。
version: '3'
services:
prometheus:
image: prom/prometheus:v2.39.1
container_name: prometheus
restart: always
network_mode: bridge
ports:
- "9090:9090"
volumes:
- /data/monitor/prometheus/conf/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- /data/monitor/prometheus/data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.enable-lifecycle'
grafana:
image: grafana/grafana:9.1.8
container_name: grafana
restart: always
network_mode: bridge
ports:
- "3000:3000"
volumes:
- /data/monitor/grafana/data:/var/lib/grafana
- /data/monitor/grafana/conf/grafana.ini:/etc/grafana/grafana.ini:ro
- /data/monitor/grafana/plugin:/var/lib/grafana/plugins
alertmanager:
image: prom/alertmanager:v0.24.0
container_name: alertmanager
restart: always
network_mode: bridge
ports:
- "9093:9093"
volumes:
- /data/monitor/alertmanager/conf/config.yml:/etc/alertmanager/config.yml:ro
- /data/monitor/alertmanager/data:/alertmanager/data
command:
- '--config.file=/etc/alertmanager/config.yml'
执行
docker-compose up -d
- 基于k8s安装: 可以通过helm安装,本文是通过安装github.com/prometheus-… 实现。 下载文档到机器上。
cd kube-prometheus
kubectl apply --server-side -f manifests/setup
kubectl wait \
--for condition=Established \
--all CustomResourceDefinition \
--namespace=monitoring
kubectl apply -f manifests/
本文先简单介绍下如何安装Prometheus+grafana+alertmanger,后面文章介绍如何使用和配置对