如何使用 Helm 在 K8s 上集成 Prometheus 和 Grafana|Part 2

种地  金牌会员 | 2024-3-29 06:00:22 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 555|帖子 555|积分 1665

在 Part 1 中,我们一起了解了什么是 Prometheus 和 Grafana,以及使用这些工具的前提条件和优势。在本部分,将继续带您学习如何安装 Helm 以及如何使用 Prometheus Helm Charts。
 
开始使用 Helm 和 Helm Chart

ArtifactHub 为 Helm Chart 提供了公共和私有资源库。我们将使用这些 Helm Chart 来设置 Kubernetes 集群中的 pod 和服务。
 
Helm 社区为各种 Kubernetes 应用程序创建、管理和维护 Helm Chart,这些资源会时常更新。Helm Chart 可重复使用,安装简单。想要使用 Helm 在 Kubernetes 上集成 Prometheus 和 Grafana,让我们从安装 Helm 开始。
 
安装 Helm

安装 Helm 之前,必须使用以下命令启动 Minikube Kubernetes:
 
  1. minikube start --driver=docker
复制代码
 
下图显示 Minikube 正在运行。Kubectl 工具现已配置为使用 Minikube。
 

 
接下来,使用以下命令(根据你使用的操作系统使用对应的命令)安装 Helm:
 
在 Linux 上安装 Helm
 
  1. sudo apt-get install helm
复制代码
 
在 Windows 上安装 Helm
 
  1. choco install Kubernetes-helm
复制代码
 
在 macOS 系统上安装 Helm
 
  1. brew install helm
复制代码
 
提示:如果遇到问题,可以查看 Helm 官方文档指南 https://helm.sh/docs/intro/install/。
 
下图显示了 Helm 在 Windows 机器上的安装情况:
 

 
Helm 命令

要获取所有 Helm 命令,请运行此命令:
 
  1. helm
复制代码
 
命令输出结果:
 
  1. The Kubernetes package manager
  2. Common actions for Helm:
  3. - helm search:    search for charts
  4. - helm pull:      download a chart to your local directory to view
  5. - helm install:   upload the chart to Kubernetes
  6. - helm list:      list releases of charts
  7. Usage:
  8.   helm [command]
  9. Available Commands:
  10.   completion  generate autocompletion scripts for the specified shell
  11.   create      create a new chart with the given name
  12.   dependency  manage a chart's dependencies
  13.   env         helm client environment information
  14.   get         download extended information of a named release
  15.   help        Help about any command
  16.   history     fetch release history
  17.   install     install a chart
  18.   lint        examine a chart for possible issues
  19.   list        list releases
  20.   package     package a chart directory into a chart archive
  21.   plugin      install, list, or uninstall Helm plugins
  22.   pull        download a chart from a repository and (optionally) unpack it in local directory
  23.   push        push a chart to remote
  24.   registry    login to or logout from a registry
  25.   repo        add, list, remove, update, and index chart repositories
  26.   rollback    roll back a release to a previous revision
  27.   search      search for a keyword in charts
  28.   show        show information of a chart
  29.   status      display the status of the named release
  30.   template    locally render templates
  31.   test        run tests for a release
  32.   uninstall   uninstall a release
  33.   upgrade     upgrade a release
  34.   verify      verify that a chart at the given path has been signed and is valid
  35.   version     print the client version information
  36. Use "helm [command] --help" for more information about a command.
复制代码
 
最常用的 Helm 命令有
 

  • helm search:在 ArtifactHub 资源库中搜索 Helm Chart。
  • helm pull:从 ArtifactHub 资源库中提取并下载 Helm Chart。
  • helm install:上传 Helm Chart 并将其部署到 Kubernetes 集群。
  • helm list:列出 Kubernetes 集群中部署的所有 Helm Chart。
 
Prometheus Helm Charts

首先,我们将搜索 Prometheus Helm Charts。要搜索 Prometheus Helm,请运行以下命令:
 
  1. helm search hub prometheus
复制代码
 
该命令列出以下 Prometheus Helm Chart:
 
  1. URL                                                       CHART VERSION         APP VERSION                                           DESCRIPTION
  2. https://artifacthub.io/packages/helm/prometheus...        25.1.0                v2.47.0                                               Prometheus is a monitoring system and time seri...
  3. https://artifacthub.io/packages/helm/truecharts...        13.0.7                2.47.1                                                kube-prometheus-stack collects Kubernetes manif...
  4. https://artifacthub.io/packages/helm/saurabh6-p...        0.2.0                 1.1                                                   This is a Helm Chart for Prometheus Setup.
  5. https://artifacthub.io/packages/helm/prometheus...        13.0.0                2.22.1                                                Prometheus is a monitoring system and time seri...
  6. https://artifacthub.io/packages/helm/wenerme/pr...        25.1.0                v2.47.0                                               Prometheus is a monitoring system and time seri...
  7. https://artifacthub.io/packages/helm/wener/prom...        25.1.0                v2.47.0                                               Prometheus is a monitoring system and time seri...
  8. https://artifacthub.io/packages/helm/romanow-he...        1.3.5                 2.40.0                                                Prometheus collects and stores its metrics as t...
  9. https://artifacthub.io/packages/helm/mach1el-ch...        1.0.1                 v2.47.0                                               Prometheus Helm chart for Kubernetes
  10. https://artifacthub.io/packages/helm/cloudposse...        0.2.1                                                                       Prometheus instance created by the CoreOS Prome...
复制代码
 
您还可以前往 ArtifactHub 存储库(https://artifacthub.io/)并搜索官方 Prometheus Helm Chart,如下图所示:
 

 
列表中的第一个是官方的 Prometheus Helm Chart。要获取此 Helm Chart,请运行以下命令:
 
  1. helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  2. helm repo update
复制代码
 
输出结果:
 
  1. "prometheus-community" has been added to your repositories
  2. Hang tight while we grab the latest from your chart repositories...
  3. ...Successfully got an update from the "grafana" chart repository
  4. ...Successfully got an update from the "prometheus-community" chart repository
  5. ...Successfully got an update from the "bitnami" chart repository
  6. Update Complete. ⎈Happy Helming!⎈
复制代码
 
现在,我们已经下载好最新版本的 Prometheus 了。
 
在 K8s 集群上安装 Prometheus Helm Chart

要在 K8s 集群上安装 Prometheus Helm Chart,请运行以下helm install命令:
 
  1. helm install prometheus prometheus-community/prometheus
复制代码
 
输出结果:
 
  1. NAME: my-prometheus
  2. LAST DEPLOYED: Thu Oct 12 20:06:57 2023
  3. NAMESPACE: monitoring
  4. STATUS: deployed
  5. REVISION: 1
  6. TEST SUITE: None
  7. NOTES:
  8. The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster:
  9. my-prometheus-server.monitoring.svc.cluster.local
  10. Get the Prometheus server URL by running these commands in the same shell:
  11.   export POD_NAME=$(kubectl get pods --namespace monitoring -l "app.kubernetes.io/name=prometheus,app.kubernetes.io/instance=my-prometheus" -o jsonpath="{.items[0].metadata.name}")
  12.   kubectl --namespace monitoring port-forward $POD_NAME 9090
  13. The Prometheus alertmanager can be accessed via port 9093 on the following DNS name from within your cluster:
  14. my-prometheus-alertmanager.monitoring.svc.cluster.local
  15. Get the Alertmanager URL by running these commands in the same shell:
  16.   export POD_NAME=$(kubectl get pods --namespace monitoring -l "app.kubernetes.io/name=alertmanager,app.kubernetes.io/instance=my-prometheus" -o jsonpath="{.items[0].metadata.name}")
  17.   kubectl --namespace monitoring port-forward $POD_NAME 9093
  18. #################################################################################
  19. ######   WARNING: Pod Security Policy has been disabled by default since    #####
  20. ######            it deprecated after k8s 1.25+. use                        #####
  21. ######            (index .Values "prometheus-node-exporter" "rbac"          #####
  22. ###### .          "pspEnabled") with (index .Values                         #####
  23. ######            "prometheus-node-exporter" "rbac" "pspAnnotations")       #####
  24. ######            in case you still need it.                                #####
  25. #################################################################################
  26. The Prometheus PushGateway can be accessed via port 9091 on the following DNS name from within your cluster:
  27. my-prometheus-prometheus-pushgateway.monitoring.svc.cluster.local
  28. Get the PushGateway URL by running these commands in the same shell:
  29.   export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=prometheus-pushgateway,component=pushgateway" -o jsonpath="{.items[0].metadata.name}")
  30.   kubectl --namespace monitoring port-forward $POD_NAME 9091
  31. For more information on running Prometheus, visit:
  32. https://prometheus.io/
复制代码
 
现在我们已经在 Kubernetes 集群上安装了 Prometheus。我们可以通过80端口访问 Prometheus 服务器。下一步是查看部署的 Kubernetes 资源,也就是 Helm Chart 在 Kubernetes 集群中创建的 Pod 和服务。
 
要查看已部署的 Kubernetes 资源,请运行以下kubectl命令:
 
  1. kubectl get all
复制代码
 
输出结果:
 
  1. NAME                                                        READY   STATUS    RESTARTS   AGE
  2. pod/my-prometheus-prometheus-node-exporter-d5l8m            1/1     Running   0          27m
  3. pod/my-prometheus-prometheus-node-exporter-kfbxb            1/1     Running   0          27m
  4. pod/my-prometheus-prometheus-pushgateway-86d6f795cb-mlb7d   1/1     Running   0          27m
  5. pod/my-prometheus-kube-state-metrics-6c4b65c7b9-wbh9r       1/1     Running   0          27m
  6. pod/my-prometheus-prometheus-node-exporter-2z7rp            1/1     Running   0          27m
  7. pod/my-prometheus-alertmanager-0                            1/1     Running   0          27m
  8. pod/my-prometheus-server-795d8fc685-zcfct                   2/2     Running   0          27m
  9. NAME                                             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
  10. service/my-prometheus-alertmanager-headless      ClusterIP   None            <none>        9093/TCP   27m
  11. service/my-prometheus-server                     ClusterIP   10.43.186.10    <none>        80/TCP     27m
  12. service/my-prometheus-prometheus-pushgateway     ClusterIP   10.43.154.83    <none>        9091/TCP   27m
  13. service/my-prometheus-alertmanager               ClusterIP   10.43.217.41    <none>        9093/TCP   27m
  14. service/my-prometheus-kube-state-metrics         ClusterIP   10.43.168.65    <none>        8080/TCP   27m
  15. service/my-prometheus-prometheus-node-exporter   ClusterIP   10.43.195.241   <none>        9100/TCP   27m
  16. NAME                                                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
  17. daemonset.apps/my-prometheus-prometheus-node-exporter   3         3         3       3            3           kubernetes.io/os=linux   27m
  18. NAME                                                   READY   UP-TO-DATE   AVAILABLE   AGE
  19. deployment.apps/my-prometheus-prometheus-pushgateway   1/1     1            1           27m
  20. deployment.apps/my-prometheus-kube-state-metrics       1/1     1            1           27m
  21. deployment.apps/my-prometheus-server                   1/1     1            1           27m
  22. NAME                                                              DESIRED   CURRENT   READY   AGE
  23. replicaset.apps/my-prometheus-prometheus-pushgateway-86d6f795cb   1         1         1       27m
  24. replicaset.apps/my-prometheus-kube-state-metrics-6c4b65c7b9       1         1         1       27m
  25. replicaset.apps/my-prometheus-server-795d8fc685                   1         1         1       27m
  26. NAME                                          READY   AGE
  27. statefulset.apps/my-prometheus-alertmanager   1/1     27m
复制代码
 
安装 Helm Chart 会创建以下 Kubernetes 资源:
 

  • Pod:托管集群内已部署的 Prometheus Kubernetes 应用程序。
  • Replica Sets:Kubernetes 集群内同一应用程序实例的集合。它提高了应用程序的可靠性。
  • Deployments:这是创建应用程序 Pod 的蓝图。
  • Services:这是公开 Kubernetes 集群内运行的 Pod。我们用它来访问已部署的 Kubernetes 应用程序。
 
下一步是访问并启动 Prometheus Kubernetes 应用程序。这里将使用 Prometheus 的 Kubernetes 服务访问该应用程序。要获取 Prometheus 的所有 Kubernetes 服务,请运行以下命令:
 
  1. kubectl get service
复制代码
 
输出结果:
 
  1. NAME                                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
  2. my-prometheus-alertmanager-headless      ClusterIP   None            <none>        9093/TCP   28m
  3. my-prometheus-server                     ClusterIP   10.43.186.10    <none>        80/TCP     28m
  4. my-prometheus-prometheus-pushgateway     ClusterIP   10.43.154.83    <none>        9091/TCP   28m
  5. my-prometheus-alertmanager               ClusterIP   10.43.217.41    <none>        9093/TCP   28m
  6. my-prometheus-kube-state-metrics         ClusterIP   10.43.168.65    <none>        8080/TCP   28m
  7. my-prometheus-prometheus-node-exporter   ClusterIP   10.43.195.241   <none>        9100/TCP   28m
复制代码
 
输出结果列出了 Prometheus 的下列 K8s service:
 

  • prometheus-alertmanager
  • prometheus-alertmanager-headless
  • prometheus-kube-state-metrics
  • prometheus-prometheus-node-exporter
  • prometheus-prometheus-pushgateway
  • prometheus-server
 
我们将使用 prometheus-server Kubernetes service 来访问 Prometheus 应用程序。prometheus-server 是 ClusterIP 类型。您只能在 Kubernetes 集群内访问它。因此我们需要公开这个 Kubernetes service,以便在 Kubernetes 集群外部访问它。公开prometheus-server Kubernetes service 将生成一个 URL。我们可以在浏览器上加载 URL 并访问正在运行的应用程序。
 
公开 prometheus-server Kubernetes service

要公开prometheus-server Kubernetes service,请运行以下命令:
 
  1. kubectl expose service prometheus-server --type=NodePort --target-port=9090 --name=prometheus-server-ext
复制代码
 
该命令会将ClusterIP类型转换为NodePort类型。这样, prometheus-server就能在 Kubernetes 集群之外通过 9090 端口访问。
 
现在,我们公开了prometheus-server Kubernetes service。接下来使用以下命令访问 Prometheus 应用程序:
 
  1. minikube service prometheus-server-ext
复制代码
 
该命令生成以下 URL:
 

 
URL 可能需要稍等一会儿才可用。您需要在浏览器上进行多次重试,直到使用此 URL 访问 Prometheus Kubernetes 应用程序。您还需要保持终端打开并运行命令,以便继续访问服务。
 

 
到这里,我们已经成功使用 Helm 在 Kubernetes 上安装了 Prometheus。Prometheus 已经在集群内部运行,我们可以使用浏览器或 URL 来进行访问。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表