Bring Your Own Prometheus

KubeSphere comes with several pre-installed customized monitoring components, including Prometheus Operator, Prometheus, Alertmanager, Grafana (Optional), various ServiceMonitors, node-exporter, and kube-state-metrics. These components might already exist before you install KubeSphere. It is possible to use your own Prometheus stack setup in KubeSphere v3.4.

Bring Your Own Prometheus

Step 1. Uninstall the customized Prometheus stack of KubeSphere

  1. Execute the following commands to uninstall the stack:

    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
    2. 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
    3. 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
    4. 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
    5. 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
    6. 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
    7. 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
    8. 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
    9. 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
    10. 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
    11. # Uncomment this line if you don't have Prometheus managed by Prometheus Operator in other namespaces.
    12. # 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. Delete the PVC that Prometheus uses.

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

Step 2. Install your own Prometheus stack

Note

KubeSphere 3.4 was certified to work well with the following Prometheus stack components:

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

Make sure your Prometheus stack components’ version meets these version requirements, especially node-exporter and kube-state-metrics.

Make sure you install node-exporter and kube-state-metrics if only Prometheus Operator and Prometheus are installed. node-exporter and kube-state-metrics are required for KubeSphere to work properly.

If you’ve already had the entire Prometheus stack up and running, you can skip this step.

The Prometheus stack can be installed in many ways. The following steps show how to install it into the namespace monitoring using Prometheus stack manifests in ks-installer (generated from a KubeSphere custom version of kube-prometheus).

  1. Obtain ks-installer that KubeSphere v3.4 uses.

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

    1. # create
    2. cat <<EOF > kustomization.yaml
    3. apiVersion: kustomize.config.k8s.io/v1beta1
    4. kind: Kustomization
    5. namespace: monitoring
    6. resources:
    7. EOF
    8. # append yaml paths
    9. find . -mindepth 2 -name "*.yaml" -type f -print | sed 's/^/- /' >> kustomization.yaml
  3. Remove unnecessary components. For example, if Grafana is not enabled in KubeSphere, you can run the following command to delete the Grafana section in kustomization.yaml.

    1. sed -i '/grafana\//d' kustomization.yaml
  4. Install the stack.

    1. kubectl apply -k .

Step 3. Install KubeSphere customized stuff to your Prometheus stack

Note

If your Prometheus stack is installed using Prometheus stack manifests in ks-installer, skip this step.

KubeSphere 3.4 uses Prometheus Operator to manage Prometheus/Alertmanager config and lifecycle, ServiceMonitor (to manage scrape config), and PrometheusRule (to manage Prometheus recording/alert rules).

If your Prometheus stack setup isn’t managed by Prometheus Operator, you can skip this step. But you have to make sure that:

  • You must copy the recording/alerting rules in PrometheusRule and PrometheusRule for etcd to your Prometheus config for KubeSphere v3.4 to work properly.

  • Configure your Prometheus to scrape metrics from the same targets as that in serviceMonitor of each component.

  1. Obtain ks-installer that KubeSphere v3.4 uses.

    1. cd ~ && git clone -b release-3.4 https://github.com/kubesphere/ks-installer.git && cd ks-installer/roles/ks-monitor/files/prometheus
  2. Create kustomization.yaml, fill the following content.

    1. apiVersion: kustomize.config.k8s.io/v1beta1
    2. kind: Kustomization
    3. namespace: <your own namespace>
    4. resources:
    5. - ./alertmanager/alertmanager-secret.yaml
    6. - ./etcd/prometheus-rulesEtcd.yaml
    7. - ./kube-state-metrics/kube-state-metrics-serviceMonitor.yaml
    8. - ./kubernetes/kubernetes-prometheusRule.yaml
    9. - ./kubernetes/kubernetes-serviceKubeControllerManager.yaml
    10. - ./kubernetes/kubernetes-serviceKubeScheduler.yaml
    11. - ./kubernetes/kubernetes-serviceMonitorApiserver.yaml
    12. - ./kubernetes/kubernetes-serviceMonitorCoreDNS.yaml
    13. - ./kubernetes/kubernetes-serviceMonitorKubeControllerManager.yaml
    14. - ./kubernetes/kubernetes-serviceMonitorKubeScheduler.yaml
    15. - ./kubernetes/kubernetes-serviceMonitorKubelet.yaml
    16. - ./node-exporter/node-exporter-serviceMonitor.yaml
    17. - ./prometheus/prometheus-clusterRole.yaml

    Note

    • Set the value of namespace to your own namespace in which the Prometheus stack is deployed. For example, it is monitoring if you install Prometheus in the monitoring namespace in Step 2.
    • If you have enabled the alerting component for KubeSphere, supplement yaml paths of thanos-ruler into kustomization.yaml.
  3. Install the required components of KubeSphere.

    1. kubectl apply -k .
  4. Find the Prometheus CR which is usually k8s in your own namespace.

    1. kubectl -n <your own namespace> get prometheus
  5. Set the Prometheus rule evaluation interval to 1m to be consistent with the KubeSphere v3.4 customized ServiceMonitor. The Rule evaluation interval should be greater than or equal to the scrape interval.

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

Step 4. Change KubeSphere’s monitoring endpoint

Now that your own Prometheus stack is up and running, you can change KubeSphere’s monitoring endpoint to use your own Prometheus.

  1. Run the following command to edit kubesphere-config.

    1. kubectl edit cm -n kubesphere-system kubesphere-config
  2. Navigate to the monitoring endpoint section, as shown in the following:

    1. monitoring:
    2. endpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
  3. Change monitoring endpoint to your own Prometheus:

    1. monitoring:
    2. endpoint: http://prometheus-operated.monitoring.svc:9090
  4. If you have enabled the alerting component of KubeSphere, navigate to prometheusEndpoint and thanosRulerEndpoint of alerting, and change the values according to the following sample. KubeSphere APIServer will restart automatically to make your configurations take effect.

    1. ...
    2. alerting:
    3. ...
    4. prometheusEndpoint: http://prometheus-operated.monitoring.svc:9090
    5. thanosRulerEndpoint: http://thanos-ruler-operated.monitoring.svc:10902
    6. ...
    7. ...

Warning

If you enable/disable KubeSphere pluggable components following this guide , the monitoring endpoint will be reset to the original value. In this case, you need to change it to the new one.

Feedback

Was this page Helpful?

Yes No