溫馨提示×

如何在Linux中監控GitLab的運行狀態

小樊
69
2025-03-04 13:19:06
欄目: 智能運維

在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的運行狀態,確保其穩定性和性能。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女