二進位制部署 Prometheus+Alertmanager+Grafana

一毛丶丶發表於2024-03-14

從官網手動安裝

  • Prometheus 採集、儲存資料
  • Grafana 用於圖表展示
  • alertmanager 用於接收 Prometheus 傳送的告警資訊
  • node-exporter 用於收集作業系統和硬體資訊的metrics

二進位制部署

#切換到root使用者
sudo -i
#建立一個專門的 prometheus 使用者:
useradd -M -s /usr/sbin/nologin prometheus
#更改 prometheus 使用者的資料夾許可權:
chown prometheus:prometheus -R /opt/prometheus
#下載prometheus二進位制壓縮包
wget https://github.com/prometheus/prometheus/releases/download/v2.37.6/prometheus-
2.37.6.linux-amd64.tar.gz
#解壓
tar xf prometheus-2.37.6.linux-amd64.tar.gz
#檢視解壓後的檔名
ls -l
mkdir /opt/prometheus -p
#移動解壓後的檔名到/opt/,並改名prometheus
mv prometheus-2.37.6.linux-amd64/ /opt/prometheus/prometheus
#建立 systemd 服務
cat > /etc/systemd/system/prometheus.service << "EOF"
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/prometheus/prometheus \
--config.file=/opt/prometheus/prometheus/prometheus.yml \
--storage.tsdb.path=/opt/prometheus/prometheus/data \
--storage.tsdb.retention.time=60d \
--web.enable-lifecycle
[Install]
WantedBy=multi-user.target
EOF

配置引數解釋:
透過 /opt/prometheus/prometheus -h 檢視幫助詳情

--config.file=/opt/prometheus/prometheus/prometheus.yml #主配置檔案
--storage.tsdb.path=/opt/prometheus/prometheus/data #資料庫儲存目
錄
--web.console.libraries=/opt/prometheus/prometheus/console_libraries #指定控制檯庫
目錄路徑
--web.console.templates=/opt/prometheus/prometheus/consoles #指定控制檯模
版目錄路徑
--storage.tsdb.retention=60d #指明資料保留
天數,預設15天
--web.enable-lifecycle #熱載入

啟動 Prometheus

systemctl daemon-reload
systemctl start prometheus.service
systemctl enable prometheus.service
systemctl status prometheus.service

檢視 Prometheus 的日誌以進行故障排除:

journalctl -u prometheus.service -f
應用 訪問地址 備註
prometheus http://192.168.61.30:9090 無使用者和密

安裝alertmanager

下載alertmanager二進位制壓縮包
wget
https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-
0.25.0.linux-amd64.tar.gz
#解壓
tar xf alertmanager-0.25.0.linux-amd64.tar.gz
#檢視解壓後的檔名
ls -l
#移動解壓後的檔名到/opt/,並改名為alertmanager
mv alertmanager-0.25.0.linux-amd64 /opt/prometheus/alertmanager

更改 alertmanager 資料夾許可權:

chown prometheus:prometheus -R /opt/prometheus/alertmanager

建立 systemd 服務

cat >/etc/systemd/system/alertmanager.service << "EOF"
[Unit]
Description=Alert Manager
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/alertmanager/alertmanager \
--config.file=/opt/prometheus/alertmanager/alertmanager.yml \
--storage.path=/opt/prometheus/alertmanager/data
Restart=always
[Install]
WantedBy=multi-user.target
EOF

啟動 alertmanager

systemctl daemon-reload
systemctl start alertmanager.service
systemctl enable alertmanager.service
systemctl status alertmanager.service

修改prometheus配置
加入alertmanager

vim /opt/prometheus/prometheus/prometheus.yml
alerting:
alertmanagers:
- static_configs:
- targets:
#根據實際填寫alertmanager的地址
- localhost:9093
應用 訪問地址 備註
alertmanager http://192.168.11.61:9093 無使用者和密碼
增加觸發器配置檔案
檢查配置
重啟prometheus或重新載入配置檔案(二選一)
訪問地址
檢查
rule_files:
#根據實際名修改檔名
- "alert.yml"
# 搜刮配置
scrape_configs:
- job_name: 'alertmanager'
scrape_interval: 15s
static_configs:
- targets: ['alertmanager:9093']

增加觸發器配置檔案

cat > /opt/prometheus/prometheus/alert.yml <<"EOF"
groups:
- name: Prometheus alert
rules:
# 對任何例項超過30秒無法聯絡的情況發出警報
- alert: 服務告警
expr: up == 0
for: 30s
labels:
severity: critical
annotations:
summary: "服務異常,例項:{{ $labels.instance }}"
description: "{{ $labels.job }} 服務已關閉"
EOF

檢查配置
cd /opt/prometheus/prometheus/
./promtool check config prometheus.yml
重啟prometheus或重新載入配置檔案(二選一)

#重啟
systemctl restart prometheus
或:
#過載,需要--web.enable-lifecycle配置
curl -X POST http://localhost:9090/-/reload
應用 訪問地址 備註
alertmanager http://192.168.61.30:9093 無使用者和密

二進位制安裝grafana

cd ~
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.4.3.linuxamd64.tar.gz
tar -zxvf grafana-enterprise-9.4.3.linux-amd64.tar.gz
ls -l
mv grafana-9.4.3/ /opt/prometheus/grafana

更改 grafana 資料夾許可權:

chown prometheus:prometheus -R /opt/prometheus

建立 systemd 服務

cat >/etc/systemd/system/grafana-server.service<<"EOF"
[Unit]
Description=Grafana server
Documentation=http://docs.grafana.org
[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/grafana/bin/grafana-server \
--config=/opt/prometheus/grafana/conf/defaults.ini \
--homepath=/opt/prometheus/grafana
[Install]
WantedBy=multi-user.target
EOF

啟動服務

systemctl daemon-reload
systemctl start grafana-server.service
systemctl enable grafana-server.service
systemctl status grafana-server.service

檢查日誌

journalctl -u grafana-server.service -f

web訪問地址

應用 訪問地址 備註
grafana 192.168.61.30:3000 admin/admin

安裝node_exporter

wget
https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-
1.5.0.linux-amd64.tar.gz
tar xvf node_exporter-1.5.0.linux-amd64.tar.gz
ls -l
mv node_exporter-1.5.0.linux-amd64 /opt/prometheus/node_exporter

更改 node_exporter 資料夾許可權:

chown prometheus:prometheus -R /opt/prometheus/node_exporter

建立 systemd 服務

cat > /etc/systemd/system/node_exporter.service <<"EOF"
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

啟動 node_exporter

systemctl daemon-reload
systemctl start node_exporter.service
systemctl enable node_exporter.service
systemctl status node_exporter.service

檢查日誌

journalctl -u node_exporter.service -f

web訪問地址

應用 訪問地址 備註
node-exporter http://192.168.61.30:9100/metrics 無使用者和密碼

修改prometheus配置
prometheus伺服器操作

cat >> /opt/prometheus/prometheus/prometheus.yml <<"EOF"
# 再scrape_configs這行下面新增如下配置:
#node-exporter配置
- job_name: 'node-exporter'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9100']
labels:
instance: Prometheus伺服器
EOF

過載prometheus

curl -X POST http://localhost:9090/-/reload

Prometheus web上檢查
http://192.168.61.30:9090/

https://grafana.com/grafana/dashboards/

快速部署

cd /opt
git clone https://gitee.com/linge365/prometheus.git
cd prometheus
#移動systemd 服務到/etc/systemd/system/目錄下
mv *.service /etc/systemd/system/
#檢查
ls -l /etc/systemd/system/
useradd -M -s /usr/sbin/nologin prometheus
chown prometheus:prometheus -R /opt/prometheus
systemctl daemon-reload
#啟動服務
systemctl start prometheus.service
systemctl start grafana-server.service
systemctl start node_exporter.service
systemctl start alertmanager.service
#開機啟動
systemctl enable prometheus.service
systemctl enable grafana-server.service
systemctl enable node_exporter.service
systemctl enable alertmanager.service
#檢查
systemctl status node_exporter.service
systemctl status prometheus.service
systemctl status grafana-server.service
systemctl status alertmanager.service

相關文章