跳到主要内容

GitLab 日常运维 SOP

本章提供可直接执行的日常巡检流程。建议 每日自动化 + 每周人工复核,重大变更窗口前额外执行完整清单。

1. 服务状态检查

每日第一件事:确认所有 Omnibus 服务处于 run 状态。

sudo gitlab-ctl status

期望输出中 run: 为正常,down: 需立即处理。重点关注:

服务职责
nginxWeb 入口
pumaRails 应用
sidekiq异步任务
postgresql主数据库
redis缓存/队列
gitalyGit 存储
gitlab-workhorse反向代理到 Rails/Gitaly

快速健康探测:

curl -sf http://127.0.0.1/-/health && echo "health OK"
curl -sf http://127.0.0.1/-/readiness && echo "readiness OK"
sudo gitlab-rake gitlab:check SANITIZE=true 2>&1 | tail -30

若有服务 down,先查日志再重启单个组件(避免整站 restart):

sudo gitlab-ctl tail sidekiq
sudo gitlab-ctl restart sidekiq

2. 日常巡检

2.1 每日巡检(自动化脚本)

#!/bin/bash
# /usr/local/bin/gitlab-daily-check.sh
set -euo pipefail
LOG="/var/log/gitlab-daily-check.log"
exec >> "$LOG" 2>&1
echo "=== $(date -Is) ==="

gitlab-ctl status | grep -E 'down:|fail' && echo "ALERT: service down" || echo "services OK"
curl -sf http://127.0.0.1/-/health || echo "ALERT: health failed"
df -h /var/opt/gitlab | awk 'NR==2 {print "disk:", $5}'
gitlab-rails runner "Sidekiq::Queue.all.each { |q| puts \"queue #{q.name}: #{q.size}\" if q.size > 500 }"
sudo chmod +x /usr/local/bin/gitlab-daily-check.sh
echo "0 8 * * * root /usr/local/bin/gitlab-daily-check.sh" | sudo tee /etc/cron.d/gitlab-daily

2.2 每周巡检

  • Admin → Dashboard → Overview:活跃用户数、项目数趋势
  • 检查 Pending jobs 是否有长期 pending 的 Pipeline
  • 审查 Monitoring → System information 中组件版本是否一致

3. 磁盘空间检查

GitLab 磁盘消耗主要来自:/var/opt/gitlab/git-data(仓库)、PostgreSQL、备份目录、Container Registry。

# 总体占用
df -h /var/opt/gitlab

# 各子目录 Top 10
sudo du -xh /var/opt/gitlab --max-depth=1 2>/dev/null | sort -hr | head -10

# 仓库体积 Top 10 项目(Rails runner)
sudo gitlab-rails runner "
Project.find_each do |p|
size = p.repository.size rescue 0
puts \"#{size}\t#{p.full_path}\" if size > 0
end
" | sort -rn | head -10

# Registry 占用
sudo du -sh /var/opt/gitlab/gitlab-rails/shared/registry

告警阈值:

路径警告严重
/var/opt/gitlab> 80%> 90%
备份目录备份失败或 > 7 天未清理磁盘满

清理策略:

# 清理旧备份(保留最近 7 天)
find /var/opt/gitlab/backups -name '*.tar' -mtime +7 -delete

# 清理 Docker Registry 未引用层(需启用 registry garbage collect)
sudo gitlab-ctl registry-garbage-collect

4. 用户权限检查

# 列出 Admin 用户
sudo gitlab-rails runner "User.admins.each { |u| puts \"#{u.username}\t#{u.email}\t2FA:#{u.two_factor_enabled?}\" }"

# 90 天未活跃用户
sudo gitlab-rails runner "
User.active.human.find_each do |u|
la = u.last_activity_on
puts \"#{u.username}\tlast:#{la}\" if la.nil? || la < 90.days.ago.to_date
end
"

# 外部用户(非 LDAP)数量
sudo gitlab-rails runner "puts User.human.where(provider: nil).count"

人工复核项:

  • 离职人员账号是否 Block 或删除
  • 共享账号(如 ci-bot)权限是否最小化
  • 组 Owner 是否与组织架构一致

5. 项目资源检查

Admin → Overview → Projects 或使用 API:

# 长期无活动的超大仓库
sudo gitlab-rails runner "
Project.find_each do |p|
next if p.last_activity_at && p.last_activity_at > 180.days.ago
size = p.repository.size rescue 0
puts \"#{p.full_path}\t#{size}\t#{p.last_activity_at}\" if size > 1_073_741_824
end
"

关注:

  • Fork 数量异常多的项目(可能 CI 滥用)
  • LFS 对象占用(Projects → Settings → Usage Quotas
  • 公开项目(visibility_level = 20)是否合规

6. Runner检查

# 列出所有 Runner 及最后联系时间
sudo gitlab-rails runner "
Ci::Runner.all.each do |r|
puts \"#{r.id}\t#{r.description}\t#{r.runner_type}\tcontacted:#{r.contacted_at}\tactive:#{r.active?}\"
end
"

# 离线超过 24 小时的 Runner
sudo gitlab-rails runner "
Ci::Runner.where('contacted_at < ?', 24.hours.ago).each { |r| puts r.description }
"

Runner 主机侧:

sudo gitlab-runner verify
sudo gitlab-runner list
docker ps -a --filter "label=com.gitlab.gitlab-runner" | wc -l # Docker executor 残留容器

7. Pipeline检查

# 最近 24h 失败率最高的项目
sudo gitlab-rails runner "
Project.find_each do |p|
failed = p.ci_pipelines.where('created_at > ?', 1.day.ago).where(status: 'failed').count
total = p.ci_pipelines.where('created_at > ?', 1.day.ago).count
puts \"#{p.full_path}\t#{failed}/#{total}\" if total > 0 && failed.to_f/total > 0.3
end
"

# 卡住超过 2 小时的 Pipeline
sudo gitlab-rails runner "
Ci::Pipeline.running.where('updated_at < ?', 2.hours.ago).limit(20).each { |pl| puts \"#{pl.project.full_path} ##{pl.id}\" }
"

Admin → CI/CD → Pipelines 查看全局 pending 任务。若 pending 持续增多,优先排查 Runner 容量与 Sidekiq。

8. 运维检查清单

复制以下清单到工单系统或 Confluence,每次巡检勾选。

每日

  • gitlab-ctl status 全部 run
  • /-/health/-/readiness 返回 200
  • /var/opt/gitlab 磁盘使用率 < 80%
  • Sidekiq 各队列无持续积压(> 500 告警)
  • 备份 cron 昨日执行成功(检查 backup 目录最新文件时间)
  • 无 P1 告警未确认

每周

  • Admin 用户列表与 2FA 状态复核
  • 离线 Runner 清理或恢复
  • 失败率 > 30% 的项目跟进
  • 公开项目与 License 合规检查
  • TLS 证书有效期 > 30 天(echo | openssl s_client -connect gitlab.example.com:443 2>/dev/null | openssl x509 -noout -dates
  • gitlab-rake gitlab:check SANITIZE=true 无新增 FAIL

每月

  • 备份恢复演练(在隔离环境验证 tar 可恢复)
  • 清理过期备份、Registry 垃圾回收
  • 审查 PAT / Deploy Token / CI Variables
  • 检查 GitLab 安全公告,评估升级窗口
  • 审查磁盘增长趋势,更新容量规划

变更窗口前

  • 全量备份 + 配置文件备份(/etc/gitlab/gitlab.rb
  • 确认回滚版本包可用
  • 通知用户维护窗口
  • 启用维护页:sudo gitlab-ctl deploy-page up