核心安装命令

# 1. 添加 Prometheus Community 的 Helm 仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

# 2. 更新本地仓库缓存,以获取最新的图表信息
helm repo update

# 3. 在 Kubernetes 集群中安装 Prometheus Stack
helm install prometheus prometheus-community/kube-prometheus-stack

其中第一步可能报错无法找到库,可以考虑代理
参考 https://blog.csdn.net/qq_40738764/article/details/134263735

重要补充和最佳实践

默认安装可能不符合所有需求,以下是一些关键的补充信息:

a) 指定命名空间(Namespace)

监控组件通常希望被安装在一个独立的命名空间中,例如 monitoring。

# 首先创建命名空间
kubectl create namespace monitoring

# 安装时指定命名空间
helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring

强烈推荐使用此方式,这样可以将监控资源与业务应用隔离。

b) 获取访问信息

安装完成后,组件会以 Service 的形式暴露。你可以查看安装的组件:

# 如果使用了命名空间,请加上 -n monitoring
kubectl get svc

你会看到类似 prometheus-prometheus-operated和 prometheus-grafana的服务。默认情况下,它们可能是 ClusterIP类型,只能在集群内部访问。

​临时访问方式(用于测试):​​

使用 kubectl port-forward将服务端口映射到本地。

  • 访问 Prometheus UI:​kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090:9090 -n monitoring然后在浏览器中访问 http://localhost:9090。
  • 访问 Grafana UI:​kubectl port-forward svc/prometheus-grafana 3000:80 -n monitoring然后在浏览器中访问 http://localhost:3000。
    默认用户是 admin。
    密码可以通过以下命令获取(假设 Release 名字是 prometheus):
    kubectl get secret prometheus-grafana -n monitoring -o jsonpath=“{.data.admin-password}” | base64 --decode ; echo

自定义配置

(使用 Values 文件)
直接使用 helm install会采用 Chart 的默认配置。要自定义(例如设置持久化存储、配置抓取规则、修改 Grafana 密码等),你需要一个自定义的 values.yaml文件。

获取默认的 values 文件作为参考:

helm show values prometheus-community/kube-prometheus-stack > custom-values.yaml

编辑 custom-values.yaml文件,根据你的需求修改配置。

使用你的自定义文件进行安装:

helm install prometheus prometheus-community/kube-prometheus-stack -f custom-values.yaml --namespace monitoring
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐