最新更新:

    集成您自己的 Prometheus

    Kube AI Hub 自带一些预装的自定义监控组件,包括 Prometheus Operator、Prometheus、Alertmanager、Grafana(可选)、各种 ServiceMonitor、node-exporter 和 kube-state-metrics。在您安装 Kube AI Hub 之前,这些组件可能已经存在。在 Kube AI Hub 3.4 中,您可以使用自己的 Prometheus 堆栈设置。

    集成您自己的 Prometheus

    要使用您自己的 Prometheus 堆栈设置,请执行以下步骤:

    步骤 1:卸载 Kube AI Hub 的自定义 Prometheus 堆栈

    1. 执行以下命令,卸载堆栈:

      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/alertmanager/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/devops/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/etcd/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/grafana/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/kube-state-metrics/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/node-exporter/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/upgrade/ 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/prometheus-rules-v1.16\+.yaml 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/prometheus-rules.yaml 2>/dev/null
      kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/prometheus 2>/dev/null
      # Uncomment this line if you don't have Prometheus managed by Prometheus Operator in other namespaces.
      # kubectl -n kubesphere-system exec $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -- kubectl delete -f /kubesphere/kubesphere/prometheus/init/ 2>/dev/null
      
    2. 删除 Prometheus 使用的 PVC。

      kubectl -n kubesphere-monitoring-system delete pvc `kubectl -n kubesphere-monitoring-system get pvc | grep -v VOLUME | awk '{print $1}' |  tr '\n' ' '`
      

    步骤 2:安装您自己的 Prometheus 堆栈

    备注

    Kube AI Hub 3.4 已经过认证,可以与以下 Prometheus 堆栈组件搭配使用:

    • Prometheus Operator v0.55.1+
    • Prometheus v2.34.0+
    • Alertmanager v0.23.0+
    • kube-state-metrics v2.5.0
    • node-exporter v1.3.1

    请确保您的 Prometheus 堆栈组件版本符合上述版本要求,尤其是 node-exporterkube-state-metrics

    如果只安装了 Prometheus OperatorPrometheus,请您务必安装 node-exporterkube-state-metricsnode-exporterkube-state-metrics 是 Kube AI Hub 正常运行的必要条件。

    如果整个 Prometheus 堆栈已经启动并运行,您可以跳过此步骤。

    Prometheus 堆栈可以通过多种方式进行安装。下面的步骤演示如何使用 ks-installer 中的 Prometheus stack manifests (其由 Kube AI Hub 基于 kube-prometheus 的定制版本生成) 将 Prometheus 堆栈安装至命名空间 monitoring 中。

    1. 获取 Kube AI Hub 3.4 所使用的 ks-installer

      cd ~ && git clone -b release-3.4 https://github.com/kubesphere/ks-installer.git && cd ks-installer/roles/ks-monitor/files/prometheus
      
    2. 创建 kustomization.yaml:

      # create 
      cat <<EOF > kustomization.yaml
      apiVersion: kustomize.config.k8s.io/v1beta1
      kind: Kustomization
      namespace: monitoring
      resources:
      EOF
      # append yaml paths
      find . -mindepth 2 -name "*.yaml" -type f -print | sed 's/^/- /' >> kustomization.yaml
      
    3. (可选)移除不必要的组件。例如,Kube AI Hub 未启用 Grafana 时,可以删除 kustomization.yaml 中的 grafana 部分:

      sed -i '/grafana\//d' kustomization.yaml
      
    4. 安装堆栈。

      kubectl apply -k .
      

    步骤 3:将 Kube AI Hub 自定义组件安装至您的 Prometheus 堆栈

    备注

    如果您的 Prometheus 堆栈是通过 ks-installer 中的 Prometheus stack manifests 进行安装,您可以跳过此步骤。

    Kube AI Hub 3.4 使用 Prometheus Operator 来管理 Prometheus/Alertmanager 配置和生命周期、ServiceMonitor(用于管理抓取配置)和 PrometheusRule(用于管理 Prometheus 记录/告警规则)。

    如果您的 Prometheus 堆栈不是由 Prometheus Operator 进行管理,您可以跳过此步骤。但请务必确保:

    • 您必须将 PrometheusRulePrometheusRule for etcd 中的记录/告警规则复制至您的 Prometheus 配置中,以便 Kube AI Hub 3.4 能够正常运行。

    • 配置您的 Prometheus,使其抓取指标的目标 (Target) 与 各组件的 serviceMonitor 文件中列出的目标相同。

    1. 获取 Kube AI Hub 3.4 所使用的 ks-installer

      cd ~ && git clone -b release-3.4 https://github.com/kubesphere/ks-installer.git && cd ks-installer/roles/ks-monitor/files/prometheus
      
    2. 创建 kustomization.yaml,填充如下内容。

      apiVersion: kustomize.config.k8s.io/v1beta1
      kind: Kustomization
      namespace: <your own namespace>
      resources:
      - ./alertmanager/alertmanager-secret.yaml
      - ./etcd/prometheus-rulesEtcd.yaml
      - ./kube-state-metrics/kube-state-metrics-serviceMonitor.yaml
      - ./kubernetes/kubernetes-prometheusRule.yaml
      - ./kubernetes/kubernetes-serviceKubeControllerManager.yaml
      - ./kubernetes/kubernetes-serviceKubeScheduler.yaml
      - ./kubernetes/kubernetes-serviceMonitorApiserver.yaml
      - ./kubernetes/kubernetes-serviceMonitorCoreDNS.yaml
      - ./kubernetes/kubernetes-serviceMonitorKubeControllerManager.yaml
      - ./kubernetes/kubernetes-serviceMonitorKubeScheduler.yaml
      - ./kubernetes/kubernetes-serviceMonitorKubelet.yaml
      - ./node-exporter/node-exporter-serviceMonitor.yaml
      - ./prometheus/prometheus-clusterRole.yaml
      

      备注

      • 将此处 namespace 的值设置为您自己的命名空间。例如,如果您在步骤 2 将 Prometheus 安装在命名空间 monitoring 中,这里即为 monitoring
      • 如果您启用了 Kube AI Hub 的告警,还需要将 thanos-ruler 的 yaml 文件路径补充到 kustomization.yaml 中。
    3. 安装以上 Kube AI Hub 必要组件。

      kubectl apply -k .
      
    4. 在您自己的命名空间中查找 Prometheus CR,通常为 k8s。

      kubectl -n <your own namespace> get prometheus
      
    5. 将 Prometheus 规则评估间隔设置为 1m,与 Kube AI Hub 3.4 的自定义 ServiceMonitor 保持一致。规则评估间隔应大于或等于抓取间隔。

      kubectl -n <your own namespace> patch prometheus k8s --patch '{
        "spec": {
          "evaluationInterval": "1m"
        }
      }' --type=merge
      

    步骤 4:更改 Kube AI Hub 的 monitoring endpoint

    您自己的 Prometheus 堆栈现在已启动并运行,您可以更改 Kube AI Hub 的监控 Endpoint 来使用您自己的 Prometheus。

    1. 运行以下命令,编辑 kubesphere-config

      kubectl edit cm -n kubesphere-system kubesphere-config
      
    2. 搜索 monitoring endpoint 部分,如下所示。

          monitoring:
            endpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
      
    3. endpoint 的值更改为您自己的 Prometheus。

          monitoring:
            endpoint: http://prometheus-operated.monitoring.svc:9090
      
    4. 如果您启用了 Kube AI Hub 的告警组件,请搜索 alertingprometheusEndpointthanosRulerEndpoint,并参照如下示例修改。Kube AI Hub Apiserver 将自动重启使设置生效。

      ...
         alerting:
           ...
           prometheusEndpoint: http://prometheus-operated.monitoring.svc:9090
           thanosRulerEndpoint: http://thanos-ruler-operated.monitoring.svc:10902
           ...
      ...
      

    警告

    如果您按照此指南启用/禁用 Kube AI Hub 可插拔组件,monitoring endpoint 会重置为初始值。此时,您需要再次将其更改为您自己的 Prometheus。