GitLab内置了监控组件,可通过内置仪表盘快速查看实例运行状态:
/etc/gitlab/gitlab.rb,找到gitlab_rails['monitoring_enabled']行,取消注释并设置为true;保存后运行sudo gitlab-ctl reconfigure应用更改。http://your-gitlab-domain/monitoring访问内置监控页面,查看CPU、内存、磁盘、请求延迟等核心指标。gitlab-ctl status:查看所有GitLab服务(如nginx、postgresql、unicorn)的运行状态;gitlab-ctl tail:实时查看GitLab服务的合并日志(包括rails、unicorn等);gitlab-ctl restart:重启GitLab服务(或指定服务,如gitlab-ctl restart nginx)。通过Linux系统工具可快速检查GitLab的资源使用情况及进程状态:
top命令实时显示系统进程的资源占用(按CPU排序可快速定位高负载进程);htop是top的增强版,支持颜色显示和鼠标操作。vmstat 1每秒刷新一次系统虚拟内存、CPU、磁盘I/O等统计信息(重点关注free内存、si/so交换分区使用量);free -h以易读格式显示内存使用情况。netstat -tuln查看当前活动的网络连接(监听端口);ss -tuln是netstat的替代工具,性能更优。dstat -cdngy实时显示CPU、磁盘、网络、内存等综合指标(需安装:sudo yum install dstat)。对于需要长期存储、可视化及报警的场景,推荐使用**Prometheus(指标收集)+ Grafana(可视化)**组合:
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz),编辑prometheus.yml配置文件,添加GitLab抓取任务:scrape_configs:
- job_name: 'gitlab'
static_configs:
- targets: ['gitlab.example.com:9090'] # 替换为GitLab实例地址
启动Prometheus:./prometheus --config.file=prometheus.yml。/etc/gitlab/gitlab.rb,添加以下配置以开启Prometheus指标导出:gitlab_rails['prometheus_export_address'] = 'localhost'
gitlab_rails['prometheus_export_port'] = '9090'
运行sudo gitlab-ctl reconfigure使配置生效。wget https://dl.grafana.com/oss/release/grafana-8.2.0.linux-amd64.tar.gz),访问http://localhost:3000(默认账号admin/admin),添加Prometheus数据源(地址填写http://localhost:9090)。gitlab_rails_database_queries_seconds_max表示数据库查询最大耗时、gitlab_workhorse_http_requests_total表示HTTP请求数),设置可视化图表(如折线图、仪表盘)。GitLab的日志文件记录了详细的运行信息,通过分析日志可快速定位问题:
/var/log/gitlab/gitlab-rails/production.log(主要业务日志);/var/log/gitlab/unicorn/unicorn_stdout.log(应用服务器日志);/var/log/gitlab/gitlab-shell/gitlab-shell.log(SSH操作日志)。tail -f /var/log/gitlab/gitlab-rails/production.log实时跟踪Rails日志,快速定位错误(如500错误、请求超时)。通过报警机制可及时通知管理员处理异常:
/etc/gitlab/gitlab.rb中配置报警规则(如gitlab_rails['monitoring_alerts_email_enabled'] = true开启邮件报警),设置报警阈值(如CPU使用率超过80%触发报警)。- alert: HighCPUUsage,条件为process_cpu_seconds_total > 80),设置报警方式(邮件、Slack、PagerDuty等)。