CentOS上GitLab如何监控与日志
小樊
36
2025-12-21 10:40:48
CentOS上GitLab监控与日志实践
一 监控方案总览
- 系统级监控:使用 top/htop、vmstat、iostat、free、ss、dstat 等命令快速排查 CPU、内存、磁盘 I/O、网络 等瓶颈,适合临时定位与日常巡检。
- GitLab 自带监控:在管理员区域可查看实例指标与性能面板,必要时启用 Self Monitoring 项目做实例自监控。
- 外部时序监控:部署 Prometheus + Grafana,抓取 GitLab 与系统指标并做可视化与告警。
- 企业级监控:使用 Zabbix、Nagios 等做主机与服务可用性监控与通知。
二 启用与访问GitLab内置监控
- 启用监控组件:编辑 /etc/gitlab/gitlab.rb,设置
gitlab_rails[‘monitoring_enabled’] = true
执行:sudo gitlab-ctl reconfigure
- 访问监控页面:打开 http://your-gitlab-domain/monitoring 查看实例指标仪表盘。
- 性能剖析:在 Admin Area → Metrics and profiling 可启用 Performance Bar 等工具,定位请求耗时与瓶颈。
三 使用Prometheus与Grafana
- 部署与集成:安装 Prometheus 与 Grafana,在 Prometheus 配置中新增抓取任务,目标指向 GitLab 的 /metrics 端点(通常为 GitLab 主机:端口/metrics)。
- 可视化与告警:在 Grafana 中添加 Prometheus 数据源,导入 GitLab 相关面板或自建大盘,并配置阈值告警规则,实现指标可视化与异常通知。
四 日志查看与实时追踪
- Omnibus 日志目录:GitLab 组件日志位于 /var/log/gitlab/。常用文件:
- /var/log/gitlab/gitlab-rails/production.log(Rails 应用主日志)
- /var/log/gitlab/gitlab-rails/production_json.log(Rails JSON 异常)
- /var/log/gitlab/gitlab-shell/gitlab-shell.log(gitlab-shell)
- /var/log/gitlab/unicorn/unicorn_stdout.log(Unicorn 标准输出)
- 便捷查看命令:
- 实时查看全部组件日志:sudo gitlab-ctl tail
- 实时查看某组件日志:sudo gitlab-ctl tail nginx
- 查看 Rails 日志:tail -f /var/log/gitlab/gitlab-rails/production.log
- 检索错误关键字:grep “error” /var/log/gitlab/gitlab-rails/production.log
- 系统日志:使用 journalctl -u gitlab 查看与 GitLab 相关的 systemd 日志。
五 日志轮转与保留策略
- 使用内置 logrotate:Omnibus 包自带 logrotate,可在 /etc/gitlab/gitlab.rb 中调整策略,例如:
logging[‘logrotate_frequency’] = “daily”
logging[‘logrotate_rotate’] = 30
logging[‘logrotate_compress’] = “compress”
修改后执行:sudo gitlab-ctl reconfigure 使配置生效。
- 按大小切割与保留(Runit 服务):在 /etc/gitlab/gitlab.rb 中可设置
logging[‘svlogd_size’] = 200 * 1024 * 1024 # 单文件约 200MB
logging[‘svlogd_num’] = 30 # 保留 30 个文件
logging[‘svlogd_timeout’] = 24 * 60 * 60 # 24 小时轮转
logging[‘svlogd_filter’] = “gzip” # 压缩归档
修改后执行:sudo gitlab-ctl reconfigure。