环境准备
#要在 Vim 中默认启用 set paste 和 set number,
vim ~/.vimrc
#在 .vimrc 文件中添加以下内容:
set paste
set number
安装 Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/2.31.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
目录
/usr/prometheus-grafana
配置 Prometheus
创建 Prometheus 配置文件
vim prometheus.yml
global:
scrape_interval: 15s # 默认抓取间隔
scrape_configs:
# 监控 Prometheus 自身
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# 监控 Node Exporter
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9910']
配置 Docker Compose
vim docker-compose.yml
version: '3.8'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- /usr/prometheus-grafana/data:/prometheus
ports:
- "9090:9090"
restart: always
grafana:
image: grafana/grafana:latest
container_name: grafana
volumes:
- grafana-data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
ports:
- "3000:3000"
restart: always
node-exporter:
image: prom/node-exporter:latest
container_name: node-exporter
ports:
- "9910:9910"
restart: always
volumes:
grafana-data:
说明:
-
Prometheus 运行在端口
9090
。 -
Grafana 运行在端口
3000
,默认用户名和密码为admin
。 -
node-exporter
用于监控主机系统的硬件和操作系统指标。
启动服务
#目录
/usr/prometheus-grafana
#权限
sudo chmod -R 777 /usr/prometheus-grafana
#启动
docker-compose up -d
docker ps
#停止所有服务
docker-compose down
验证服务
#prometheus url
http://127.0.0.1:9090
点击 Status -> Targets,查看 node-exporter 是否处于 UP 状态
#Grafana
http://127.0.0.1:3000,默认用户名和密码为 admin/admin
账户
admin
密码
n0R35C42os1dlSkJ
配置添加代码端口4000
vim prometheus.yml
global:
scrape_interval: 15s # 默认抓取间隔
scrape_configs:
# 监控 Prometheus 自身
- job_name: 'prometheus'
static_configs:
- targets:
- 'localhost:9090' # Prometheus 自身的地址,通常是 localhost:9090
# 监控 Node Exporter
- job_name: 'node-exporter'
static_configs:
- targets:
- 'node-exporter:9100' # Node Exporter 地址,默认端口是 9100
# 监控 go-zero 应用 - API
- job_name: 'go-zero-api'
static_configs:
- targets:
- '127.0.0.1:4000' # go-zero API 服务地址
# 监控 go-zero 应用 - RPC
- job_name: 'go-zero-rpc'
static_configs:
- targets:
- '127.0.0.1:4001' # go-zero RPC 服务地址
# 监控 etcd 服务
- job_name: 'etcd'
metrics_path: '/metrics' # etcd 默认的 metrics 路径
static_configs:
- targets:
- '127.0.0.1:2379' # etcd 服务的地址
# 监控 Apollo 配置中心
- job_name: 'apollo'
metrics_path: '/prometheus' # Apollo 暴露的 metrics 路径
static_configs:
- targets:
- '127.0.0.1:8088' # Apollo 的地址和端口
重启
docker-compose restart prometheus
查看go-zero端口
#端口查看
sudo lsof -i :4000
#查看进程
ps -p 1343489 -f
验证配置是否生效:
-
健康检查 : 访问
http://localhost:9090/-/healthy
查看 Prometheus 是否正常。 -
查看抓取目标 : 访问
http://localhost:9090/targets
查看抓取目标的状态。