目录
安装Prometheus Server
创建用户
useradd -m -s /bin/false prometheus
id prometheus
创建配置目录
存储Prometheus配置文件和数据
mkdir /etc/prometheus
mkdir /var/lib/prometheus
# 在/var/lib/prometheus上设置所有权
chown prometheus:prometheus /var/lib/prometheus/
下载
# 地址https://prometheus.io/download/,请选取合适的版本
dnf install wget -y
wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.linux-amd64.tar.gz -P /tmp
解压
tar -zxpvf prometheus-2.19.2.linux-amd64.tar.gz
# 复制prometheus和promtool到/usr/local/bin路径
cd /tmp/prometheus-2.19.2.linux-amd64
cp prometheus /usr/local/bin
cp promtool /usr/local/bin
# 将prometheus.yml复制到/etc/prometheus/路径
cp prometheus.yml /etc/prometheus/
创建配置文件
vi /etc/prometheus/prometheus.yml
# Global config
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.
scrape_timeout: 15s # scrape_timeout is set to the global default (10s).
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
防火墙开放9090
firewall-cmd --add-port=9090/tcp --permanent
firewall-cmd --reload
创建systemd服务文件
为了使用systemd将Prometheus作为服务进行管理
vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
启动
# 重新加载
systemctl daemon-reload
# 启动并使Prometheus在启动时运行
systemctl start prometheus
systemctl enable prometheus
# 验证Prometheus正在运行
systemctl status prometheus
# 查看端口9090
netstat -tunlp |grep 9090
# 浏览器打开http://server-ip:9090
安装node_exporter
node_exporter可收集和运送大量Linux系统指标,例如CPU,内存使用率,文件系统和网络统计信息。
创建用户
useradd -m -s /bin/false node_exporter
id node_exporter
下载解压
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
tar -zxpvf node_exporter-1.0.1.linux-amd64.tar.gz
# 复制node_exporter到/usr/local/bin
cp node_exporter-1.0.1.linux-amd64/node_exporter /usr/local/bin
chown node_exporter:node_exporter /usr/local/bin/node_exporter
# 配置node_exporter以作为服务运行
vi /etc/systemd/system/node_exporter.service
node_exporter.service内容
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target`
# 重新加载
systemctl daemon-reload
# 启动并启用node_exporter服务
systemctl start node_exporter
systemctl enable node_exporter
# 查看状态
systemctl status node_exporter
# 查看端口
netstat -pnltu | grep 9100
# 防火墙中打开端口9100
firewall-cmd --add-port=9100/tcp --permanent
irewall-cmd --reload
将node_exporter目标添加到prometheus.yml文件
vi /etc/prometheus/prometheus.yml
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
# 重新启动Prometheus服务
systemctl restart prometheus
# 进入浏览器查看指标
# http://192.168.174.195:9100/metrics
安装Grafana
下载地址
https://grafana.com/grafana/download
安装
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.3.6-1.x86_64.rpm
sudo yum install grafana-enterprise-8.3.6-1.x86_64.rpm
检查是否安装成功
/usr/sbin/grafana-server -v
启动
systemctl start grafana-server.service
检查是否启动成功
ss -lntp | grep 3000
对接prometheus
登录
http://121.122.123.47:3000/login
# admin/admin
# 修改密码 admin/111111
配置数据源
选择prometheus
添加dashboard
import现成的dashboard
https://grafana.com/grafana/dashboards?orderBy=name&direction=asc