温馨提示×

GitLab如何监控CentOS

小樊
41
2026-08-18 17:32:52
栏目: 智能运维

在 GitLab 中“监控 CentOS”通常有两种常见理解,我分别说明,并给出可落地的做法


一、如果你指的是:用 GitLab CI/CD 监控 CentOS 服务器状态

这是最常见场景:通过 GitLab Runner 在 CentOS 上执行监控脚本,并展示结果。

1️⃣ 架构说明

CentOS 服务器
  └── 安装 GitLab Runner
        └── 定时执行 .gitlab-ci.yml
              └── 检查 CPU / 内存 / 磁盘 / 服务

2️⃣ 在 CentOS 上安装 GitLab Runner

# 添加官方仓库
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | bash

# 安装
yum install -y gitlab-runner

# 注册 Runner
gitlab-runner register

注册时:

  • GitLab URL:https://gitlab.com 或私有 GitLab 地址
  • Token:从 GitLab → Settings → CI/CD → Runners 获取
  • Executor:shell(最简单)

3️⃣ 编写监控脚本(CentOS)

例如:monitor.sh

#!/bin/bash

echo "===== CentOS 监控 ====="
echo "时间: $(date)"

echo "CPU 负载:"
uptime

echo "内存使用:"
free -h

echo "磁盘使用:"
df -h

echo "关键服务状态:"
systemctl status sshd | grep Active
chmod +x monitor.sh

4️⃣ .gitlab-ci.yml 定时监控

stages:
  - monitor

centos_monitor:
  stage: monitor
  only:
    - schedules
  script:
    - bash monitor.sh
  tags:
    - centos

在 GitLab 中:

  • CI/CD → Schedules
  • 设置 每天 / 每小时 执行一次

✅ 优点:

  • 无需额外监控系统
  • 结果直接出现在 GitLab Pipeline
  • 可结合失败报警

二、如果你指的是:GitLab 自身运行在 CentOS 上,如何监控 GitLab

这是 运维 GitLab 本身 的场景。

1️⃣ 系统级监控(CentOS)

top
htop
free -h
df -h

2️⃣ GitLab 自带监控

GitLab 提供 Prometheus + Grafana(默认启用)

查看:

gitlab-ctl status
gitlab-ctl tail

访问:

http://gitlab-ip/-/grafana

3️⃣ 常用 GitLab 监控命令

# 查看服务状态
gitlab-ctl status

# 查看资源使用
gitlab-ctl top

# 查看日志
gitlab-ctl tail nginx
gitlab-ctl tail postgresql

三、如果你指的是:用 GitLab + Prometheus 监控 CentOS

这是专业级做法

架构

CentOS
 └── node_exporter
       ↑
 Prometheus(GitLab 内置)
       ↓
 Grafana

步骤

  1. CentOS 安装 node_exporter
  2. GitLab Prometheus 拉取指标
  3. Grafana 展示 CPU / 内存 / 磁盘

✅ 适合:

  • 多台服务器
  • 长期监控
  • 可视化

四、推荐方案总结

需求 推荐方案
简单监控 GitLab CI + Runner
运维 GitLab GitLab 自带 Prometheus
企业级 GitLab + node_exporter + Grafana
报警 GitLab Pipeline Failed / Alertmanager

如果你愿意,可以告诉我:

  • ✅ GitLab 是 私有部署还是 gitlab.com
  • ✅ 想监控 GitLab 还是 CentOS
  • ✅ 是否需要 报警 / 可视化

我可以直接给你一套完整配置示例

0