Grafana 入門篇1.2 整合Prometheus監控Proxmox ve

Proxmox ve 是一套很好用的管理系統

Ruei-Chi Huang
Kirin Blog | Kirin の IT Geek
12 min readNov 28, 2022

--

上一篇介紹到Grafana的安裝,這邊繼續介紹怎麼整合Prometheus監控Proxmox ve (以下簡稱PVE)。

先來個Lab 架構圖。

這是這系列介紹的Lab環境架構圖,內容混合了Proxmox ve VM & Container,Container部分使用K8s,K8s除了地端(local)之外也架了三大公有雲,會規畫這樣稍微複雜的系統,因為這本篇的分享,除了把monitor PVE介紹完之外,之後還想寫一些

  • K8s 12.5 安裝 cri-dockerd 整合 docker
  • K8s 地跨多雲整合(local & AWS & Azure & GCP-GKE)
  • 如何使用Grafana monitor K8s & Container status
  • 如何monitor local & cloud network status & traffic
  • metrics、alert、grafana oncall規劃
  • HorizontalPodAutoscaler monitor

的分享~

Step1. 佈署Prometheus of Container

K8s是很強大的微服務(microservices)管理系統,本篇對k8s的安裝就不做說明,免得文太長,日後會再寫一篇介紹,本篇先聚焦在k8s上透過docker、cri-docker、portainer佈署Prometheus。

1.1、先在Docker 上安裝 portainer

到Docker hub 上,用 portainer 當關鍵字搜索,找到 “portainer/portainer-ee” (Business)這個容器影像檔。

Docker Hub : https://hub.docker.com/

點入後複製右邊的Docker Pull Command,到Docker host 上執行。

這邊建議Docker host 可以用VM 建立,好處是可以Snapshot ,方便安裝系統發生問題時隨時可以rollback

docker pull portainer/portainer-ee
docker run -d -p 8000:8000 -p 9443:9443 --name=portainer --restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ee:latest

8000 是 http port / 9443 是 https port,可以自行調整

container run成功之後,打開瀏覽器

網址 : https://<<ur ip>>:<<ur https port>>

第一次進入 Portainer 需要設定密碼,完成後就可以進入主畫面了。

安裝完成之後,註冊一個portainer 帳號,可以免費取得free 5 node 的 license

https://install.portainer.io/

1.2 、 在portainer上使用github 安裝 Prometheus

到Prometheus官網查docker 的namespace

Prometheus官網 https://prometheus.io/docs/prometheus/latest/installation/

Prometheus docker namespace : prom/prometheus

在portainer → home → local → Containers → Add container

namespace 可以自訂,這邊我用prometheus

image 用 prom/prometheus

Port 用 9090:9090

設定完成後按 Deploy the container,完成後可以在 Container List 看到 running 就表示成功了。

彙整一下到目前為止,整個架構的IP / Port Number 資訊

Proxmox ve : https://192.168.50.60:8006

Grafana : http://192.168.50.61:3000/login

Prometheus : http://192.168.50.62:9090

Docker Host : 192.168.50.62

Portainer : https://192.168.50.62:9443

K8s Master : 192.168.50.61

K8s Node1 : 192.168.50.63

1.3 將Prometheus整合入Grafana

打開Grafana → Configuration → data sources → add data sources

選 Prometheus

data type 選 Prometheus
add data source 設定

新增一個Dashboard 測試prometheus data get 是否正常

Dashboard → New Dashboard → Add a new panel

新增Dashboard之後,Title 改為 Prometheus,並確認左下角的Data source 為剛剛1.3 第一步驟新增的 Source data “Prometheus”

選好Metric 之後,點 Run queries 再點 Apply 儲存 Dashboard
完成後的Dashboard

1.4、透過Prometheus 監控 Proxmox ve

在1.3 章節打通 Grafana 與 Prometheus 之後,接下讓 Prometheus可以get PVE的資料,然後Grafana透過Prometheus把PVE資訊顯示在Dashborad上,要達到這個目標有三件事要做

  • 在PVE上安裝 prometheus-pve-exporter
  • 在Prometheus上設定PVE
  • Grafana 上的Dashboard 要新增PVE data

先進入到PVE 的Command mode

新增一個bash files

nano /tmp/pve-exporter.sh
/*一鍵安裝prometheus-pve-exporter*/
#!/bin/sh
# -*- coding: utf-8 -*-
# Author: CIASM
# Date: 2022/07/11

groupadd --system prometheus
useradd -s /sbin/nologin --system -g prometheus prometheus
mkdir /etc/prometheus/

apt install python2 pip -y
pip install prometheus-pve-exporter

cat << EOF > /etc/prometheus/pve.yml
default:
user: root@pam
password: your_password_here
verify_ssl: false
EOF

chown -R prometheus:prometheus /etc/prometheus/
chmod -R 775 /etc/prometheus/

cat << EOF > /etc/systemd/system/prometheus-pve-exporter.service
[Unit]
Description=Prometheus exporter for Proxmox VE
Documentation=https://github.com/znerol/prometheus-pve-exporter
[Service]
Restart=always
User=prometheus
ExecStart=/usr/local/bin/pve_exporter /etc/prometheus/pve.yml
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable --now prometheus-pve-exporter
sh pve-exporter.sh
/* 執行*/
http://<<ur ip>>:9221/pve
/*驗證*/

完成prometheus-pve-exporter安裝之後,再設定Prometheus

ssh 到 docker host

ssh 192.168.50.62 22

先進到tmp 然後新增一份 prometheus.yml

cd /tmp
nano prometheus.yml

加入以下內容後儲存

global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
alerting:
alertmanagers:
- follow_redirects: true
enable_http2: true
scheme: http
timeout: 10s
api_version: v2
static_configs:
- targets: []
scrape_configs:
- job_name: prometheus
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- localhost:9090

- job_name: 'proxmox'
metrics_path: /pve
static_configs:
- targets: ['192.168.50.60:9221']
/* targets 是prometheus-pve-exporter api url */

到portainer 內查 prometheus 的 container id

把新增的prometheus.yml 寫入 container內

docker cp /tmp/prometheus.yml 154f28014e9d391063ac0321aae82e7e0c6e321b21781e810bc5a6dd429f8582:/etc/prometheus/

在portainer內 restart container

Web 開啟 prometheus 的Service Discovery就可以看到一個 proxmox

1.5、Grafana 加入 PVE Dashboard

一樣1.3的流程,新增一個Dashboard 這時候在 Metric 就可以看到很多 pve 類的Metric

這邊建議import 一個不錯的 PVE Dashboard template

Dashboard → import → id : 10347

import 後就大功告成

後記 : 這本文從規劃到完工加上整套lab系統建置,花了兩天半的功夫,這篇架構比較大一些,主要是我看網路上很多文都是用local建置,比較分散也缺少整合的思考架構,所以我思考後希望技術分享文的內容,能夠跟實際工作環境類似,這樣比較能幫助到讀者,所以其實還蠻硬的,但寫完之後有種充實感 ^_^

Previous Post — Grafana 入門篇1.1 — 在ubuntu 22.04 上安裝Grafana Enterprise

Next Post — 如何透過Grafana 監控K8s (K8s 安裝篇)

--

--

Ruei-Chi Huang
Kirin Blog | Kirin の IT Geek

I am Kirin., I was ShellFans AI Technology’s CTO. Our team specializes in IoT, Blockchain, Cloud and Ai. | https://www.shell.fans