温馨提示×

如何在Linux中监控GitLab的运行状态

小樊
48
2025-03-04 13:19:06
栏目: 智能运维
Linux服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Linux中监控GitLab的运行状态可以通过多种方法实现,以下是一些常用的方法:

使用Prometheus和Grafana监控GitLab指标

  • 开启GitLab指标端: 编辑 /etc/kubernetes/addons/gitlab-values.yaml 文件,确保以下配置项已启用:

    gitlab:webservice:workhorse:metrics:enabled: true
    gitlab-runner:metrics:enabled: true
    global:monitoring:enabled: true
    

    然后使用Helm升级GitLab配置:

    helm -n gitlab upgrade gitlab -f /etc/kubernetes/addons/gitlab-values.yaml
    
  • Prometheus采集GitLab指标: 使用 kubectl 编辑Prometheus的配置文件,添加GitLab的监控目标:

    kubectl -n kube-system edit cm prometheus
    

    kubernetes_sd_configs 部分添加以下内容:

    - role: pod
      relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_gitlab_com_prometheus_scrape]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_pod_annotation_gitlab_com_prometheus_scheme]
          action: replace
          regex: (https?)
          target_label: __scheme__
        - source_labels: [__meta_kubernetes_pod_annotation_gitlab_com_prometheus_path]
          action: replace
          target_label: __metrics_path__
          regex: (.+)
        - source_labels: [__address__, __meta_kubernetes_pod_annotation_gitlab_com_prometheus_port]
          action: replace
          regex: ([^:]+)(?::\d+)?;(\d+)
          replacement: $1:$2
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - source_labels: [__meta_kubernetes_name]
          action: replace
    

使用自定义脚本监控服务器基本信息

  • 编写一个Shell脚本来获取服务器的CPU、内存、磁盘使用情况等基本信息,并定期输出监控信息。例如:
    #!/bin/bash
    hostname=$(hostname)
    ip_address=$(hostname -I | awk '{print $1}')
    os=$(lsb_release -ds)
    kernel=$(uname -r)
    uptime=$(uptime -p)
    
    while true; do
      cpu_model=$(cat /proc/cpuinfo | grep "model name" | head -n 1 | awk -F ': ''{print $2}')
      cpu_cores=$(cat /proc/cpuinfo | grep "model name" | wc -l)
      memory_total=$(free -h | awk 'NR==2{print $2}')
      memory_used=$(free -h | awk 'NR==2{print $3}')
      memory_free=$(free -h | awk 'NR==2{print $4}')
      memory_available=$(free -h | awk 'NR==2{print $7}')
      disk_total=$(df -h --output=size / | awk 'NR==2{print $1}')
      disk_used=$(df -h --output=used / | awk 'NR==2{print $1}')
      disk_free=$(df -h --output=avail / | awk 'NR==2{print $1}')
      cpu_usage=$(top -b -n 1 | grep "%Cpu(s):" | awk '{printf "%.2f%%", 100-$8}')
    
      clear
      echo "服务器信息:"
      echo "主机名:$hostname"
      echo "IP地址:$ip_address"
      echo "操作系统:$os"
      echo "内核版本:$kernel"
      echo "运行时间:$uptime"
      echo "--------------------------------------"
      echo "CPU信息:"
      echo "型号:$cpu_model"
      echo "核心数:$cpu_cores"
      echo "CPU使用率:$cpu_usage"
      echo "--------------------------------------"
      echo "内存信息:"
      echo "总量:$memory_total"
      echo "已使用:$memory_used"
      echo "可用:$memory_available"
      echo "--------------------------------------"
      echo "磁盘信息:"
      echo "总量:$disk_total"
      echo "已使用:$disk_used"
      echo "可用:$disk_free"
      sleep 3
    done
    

使用GitLab内置命令检查运行状态

  • 对于GitLab 3.1及更高版本,可以使用以下命令检查GitLab的运行状态:
    sudo -u gitlab -H bundle exec rake gitlab:app:status RAILS_ENV=production
    
    对于GitLab 4.0及更高版本,使用:
    sudo -u gitlab -H bundle exec rake gitlab:check RAILS_ENV=production
    

通过以上方法,您可以在Linux系统中有效地监控GitLab的运行状态,确保其稳定性和性能。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:如何在Linux上监控GitLab运行状态

0