ubuntu gitlab监控如何搭建
Ubuntu系统下GitLab监控搭建指南(以Prometheus+Grafana为核心)
一、基础准备
确保Ubuntu服务器已安装GitLab(建议版本≥13.0),且系统已更新至最新状态。监控搭建需用到以下工具:
- Prometheus:开源时序数据库,负责采集和存储GitLab指标;
- Grafana:可视化工具,用于创建监控仪表盘;
- Node Exporter(可选):采集系统级指标(CPU、内存、磁盘等),补充GitLab自身指标。
二、安装与配置Prometheus
-
安装Prometheus
通过APT仓库安装(推荐):wget https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus_2.48.1_amd64.deb sudo dpkg -i prometheus_2.48.1_amd64.deb或使用Docker快速部署(需提前安装Docker):
docker run -d --name=prometheus -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus -
配置Prometheus抓取GitLab指标
编辑/etc/prometheus/prometheus.yml(本地安装)或容器内的配置文件,添加GitLab监控任务:scrape_configs: # 监控GitLab自身指标(需GitLab启用metrics端口) - job_name: 'gitlab' static_configs: - targets: ['localhost:9090'] # GitLab metrics默认端口为9090,若修改需同步调整 metrics_path: '/metrics'保存后重启Prometheus:
sudo systemctl restart prometheus # 本地安装 docker restart prometheus # Docker部署 -
验证Prometheus连接
访问http://< Ubuntu-IP> :9090/targets,确认gitlab任务状态为UP(表示成功连接到GitLab)。
三、安装与配置Grafana
-
安装Grafana
通过APT仓库安装:sudo apt-get update sudo apt-get install -y grafana sudo systemctl enable --now grafana-server访问
http://< Ubuntu-IP> :3000(默认账号admin,密码admin),完成初始登录。 -
配置Prometheus数据源
进入Grafana控制台→Configuration→Data Sources→Add data source:- 选择Prometheus;
- 在URL栏输入
http://localhost:9090(若Prometheus用Docker部署,需改为容器IP或宿主机IP); - 点击Save & test,确认连接成功。
-
导入GitLab监控仪表盘
在Grafana控制台→Create→Dashboard→Import,输入以下仪表盘ID(官方或社区提供):- GitLab Overview(ID: 11867):展示GitLab实例整体状态(CPU、内存、作业运行数等);
- GitLab Runner(ID: 11868):监控Runner的运行状态、任务成功率;
- GitLab Database(ID: 11869):监控数据库性能(查询延迟、连接数)。
导入后,可根据需求调整面板布局和指标阈值。
四、启用GitLab自身指标
GitLab需开启内置指标功能,才能被Prometheus采集:
- 编辑GitLab配置文件
打开/etc/gitlab/gitlab.rb,添加以下配置:gitlab_rails['gitlab_metrics_enabled'] = true gitlab_rails['gitlab_metrics_port'] = 9090 # 指标端口(需与Prometheus配置一致) gitlab_rails['gitlab_metrics_token'] = 'your_secure_token' # 可选:用于API认证 gitlab_runner['metrics_enabled'] = true # 启用Runner指标 - 应用配置
运行以下命令使配置生效:sudo gitlab-ctl reconfigure sudo gitlab-ctl restart
五、设置告警规则
-
Prometheus告警规则
在/etc/prometheus/目录下创建alerts.yml文件,定义告警条件(示例):groups: - name: gitlab_alerts rules: # CPU使用率超过80%持续1分钟触发告警 - alert: GitLabHighCPU expr: process_cpu_seconds_total{ job="gitlab"} / system_cpu_cores > 0.8 for: 1m labels: severity: warning annotations: summary: "GitLab CPU usage is high (instance { { $labels.instance } } )" description: "GitLab CPU usage is above 80% for more than 1 minute." # 内存使用率超过85%持续5分钟触发告警 - alert: GitLabHighMemory expr: process_resident_memory_bytes{ job="gitlab"} / process_virtual_memory_bytes{ job="gitlab"} > 0.85 for: 5m labels: severity: critical annotations: summary: "GitLab memory usage is critical (instance { { $labels.instance } } )" description: "GitLab memory usage is above 85% for more than 5 minutes."在
prometheus.yml中加载告警规则:rule_files: - "/etc/prometheus/alerts.yml"重启Prometheus使规则生效:
sudo systemctl restart prometheus -
配置Alertmanager通知
安装Alertmanager(若未安装):sudo apt-get install -y alertmanager sudo systemctl enable --now alertmanager编辑
/etc/alertmanager/alertmanager.yml,配置通知渠道(以邮件为例):route: receiver: 'email-notifications' receivers: - name: 'email-notifications' email_configs: - to: 'admin@example.com' from: 'alertmanager@example.com' smarthost: 'smtp.example.com:587' auth_username: 'your_email@example.com' auth_password: 'your_email_password'在Prometheus中配置Alertmanager地址(
prometheus.yml):alerting: alertmanagers: - static_configs: - targets: ['localhost:9093'] # Alertmanager默认端口
六、验证监控与告警
- 查看监控数据
登录Grafana控制台,进入已导入的仪表盘,确认CPU、内存、作业运行数等指标正常显示。 - 触发告警测试
手动占用GitLab服务器资源(如运行stress-ng --cpu 4 --timeout 300),观察Prometheus是否触发GitLabHighCPU告警,并通过Alertmanager发送通知到指定邮箱。
七、可选扩展
- GitLab自监控项目:在GitLab中创建“Monitoring”项目,启用自监控功能(
Settings → Metrics and profiling → Self monitoring),将项目指标集成到Prometheus中,实现更细粒度的监控。 - 第三方工具集成:除Prometheus外,可集成Zabbix、Datadog等工具,满足企业级监控需求;或使用Grafana的
loki组件收集GitLab日志,实现日志与指标的关联分析。
通过以上步骤,即可在Ubuntu系统上搭建完整的GitLab监控体系,实现从指标采集、可视化到告警的全流程管理,确保GitLab实例的稳定运行。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: ubuntu gitlab监控如何搭建
本文地址: https://pptw.com/jishu/742906.html
