解决在集群环境中部署 Prometheus 添加标志位参数后无法正常访问的问题。
集群化
根本原因在于 GitLab 并没有读取标志位参数来对监控项的路径进行一致化修改,因此会出现部分节点的监控项请求失败。
配置修改
首先修改监控节点 GitLab 配置:
prometheus['flags'] = {
'web.external-url' => "https://git.example.com/-/prom",
'web.route-prefix' => "/", # 这里建议使用双引号
## 单引号在部分旧版本中会提示语法错误
}
如果外部负载均衡使用的 HAProxy,需要修改配置文件:
frontend http-in
...
acl prometheus path_beg -i /-/prom
use_backend prometheus if prometheus
backend prometheus
## 健康检查改为从根域请求
option httpchk GET /-/healthy
server monitor-1 10.199.11.167:9090 check inter 3s fall 1
## 增加重写参数并添加对应的头,上面的 route-prefix 参数会将内部接口挂载至根域;
## 因此所有的请求路径都需要基于根域进行重写;
http-request replace-path ^/-/prom(/.*)$ \1
http-request set-header X-Forwarded-Proto https
http-request set-header X-Forwarded-Prefix /-/prom
如果使用的 NGiNX,参考下面的配置文件,原理同上:
location ^~ /-/prom/ {
rewrite ^/-/prom(/.*)$ $1 break;
proxy_pass http://10.199.11.167:9090;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Prefix /-/prom;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
测试
修改完成后重启服务,然后访问 https://git.example.com/-/prom/metrics
和监控节点IP http://10.199.11.167:9090/metrics
确认这两个域名都能正常访问即修改完成;
Omnibus
在 AIO 实例中安装 Grafana 后对接 GitLab 内置 Prometheus。
修改配置文件,添加:
nginx['custom_gitlab_server_config'] = <<-'NGINX'
location /-/grafana/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
location = /-/grafana {
return 301 /-/grafana/;
}
NGINX
重新生成配置并重启服务即可。
在 Grafana 中,需要添加数据源,然后创建配置模板目录:
apiVersion: 1
providers:
- name: GitLab Omnibus
orgId: 1
folder: GitLab Omnibus
type: file
disableDeletion: false
allowUiUpdates: true
updateIntervalSeconds: 600
options:
path: "/etc/grafana/provisioning/dashboards"
下载监控模板,解压到目标文件夹
wget https://gitlab.com/gitlab-org/grafana-dashboards/-/archive/master/grafana-dashboards-master.tar.gz
tar xf grafana-dashboards-master.tar.gz
sudo cp grafana-dashboards-master/omnibus/*.json /etc/grafana/provisioning/dashboards/
修改 Grafana 面板配置中自定义访问地址 /etc/grafana/grafana.ini
;root_url = %(protocol)s://%(domain)s:%(http_port)s/
;serve_from_sub_path = true
[server]
root_url = 'https://git-intranet.example.com/-/grafana'
serve_from_sub_path = true
重启服务并检查状态
sudo systemctl restart grafana-server.service
最后将其他的多余监控面板删除重启服务即可。
附录
参考链接
本文由 柒 创作,采用 知识共享署名4.0
国际许可协议进行许可。
转载本站文章前请注明出处,文章作者保留所有权限。
最后编辑时间: 2025-10-22 10:09 AM
Currently it seems like Movable Type iѕ the bеѕt blogging platform
ɑvailable right now. (from what Ӏ've reаd) Is that
what you aгe using on yоur blog?