解决在集群环境中部署 Prometheus 添加标志位参数后无法正常访问的问题。
集群化
配置修改
首先修改监控节点 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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
测试
修改完成后重启服务,然后访问 https://git.example.com/-/prom/metrics
和监控节点IP http://10.199.11.192: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
重新生成配置并重启服务即可。
附录
参考链接
本文由 柒 创作,采用 知识共享署名4.0
国际许可协议进行许可。
转载本站文章前请注明出处,文章作者保留所有权限。
最后编辑时间: 2025-10-10 20:50 PM