下面介绍几种 Prometheus Server 的安装方式,如 RPM 安装、Docker 安装、二进制安装、kubernetes 安装等。
Prometheus Server 安装部署
Prometheus 介绍:
- 默认监听端口:
9090
- 访问:
http://<ip>:9090/
/graph
/alerts
/config
/targets
/tsdb-status
apt 部署
sudo apt install prometheus prometheus-node-exporter prometheus-pushgateway prometheus-alertmanager
sudo systemctl enable prometheus
sudo systemctl start prometheus
二进制安装
cd /usr/local/
tar -zxvf prometheus-2.19.2.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/prometheus-2.19.2.linux-amd64 prometheus
# tsdb 目录
mkdir /var/lib/prometheus
# 创建用户
useradd -r -s /sbin/nologin prometheus
chown -R prometheus.prometheus /usr/local/prometheus/
chown -R prometheus.prometheus /var/lib/prometheus
$ cat /etc/profile.d/prometheus.sh
export PROMETHEUS_HOME=/usr/local/prometheus/
export PATH=${PROMETHEUS_HOME}:$PATH
$ cat /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
After=network.target
[Service]
User=prometheus
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.external-url=http://100.80.0.128:9090
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
$ grep -Ev "^ *#|^$" /usr/local/prometheus/prometheus.yml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
# - "rules/*.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'prometheus'
# basic_auth 认证
basic_auth:
username: ""
password: ""
static_configs:
- targets: ['localhost:9090']
cd /usr/local/prometheus/
promtool check config prometheus.yml
# 检查规则
promtool check rules rules/target.yml
systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus
RPM 包安装
rpm 源参考:https://github.com/lest/prometheus-rpm
ubuntu 安装
apt install prometheus
docker 安装
docker run -d --name prometheus --restart always -p 9090:9090 prom/prometheus
docker-compose
version: '3'
services:
prometheus:
image: prom/prometheus
ports:
- 9090:9090/tcp
user: root
volumes:
- ${PWD}/prometheus/data:/prometheus
- ${PWD}/prometheus/conf/prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin123
image: grafana/grafana
volumes:
- ${PWD}/grafana/data:/var/lib/grafana
ports:
- 3000:3000/tcp
user: grafana
- 获取 prometheus.yml 配置文件脚本并配置采集 prometheus 数据
( docker run --rm -ti --entrypoint '' prom/prometheus cat/etc/prometheus/prometheus.yml ) > ./prometheus/conf/prometheus.yml
# 新增配置
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- 启动
- prometheus: http://127.0.0.1:9090
- grafana: http://127.0.0.1:3000
docker-compose up -d
kubernetes 安装
配置
添加静态配置主机
- 修改
/usr/local/prometheus/prometheus.yml
,新增一个 job_name: 'monitor-node'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'monitor-node'
static_configs:
- targets: ['<host1>:9090', '<host2>:9090', '<host3>:9090']
check config /usr/local/prometheus/prometheus.yml
Prometheus exporter 安装
数据展示
Grafana