開始使用 Helm 和 Helm Chart
ArtifactHub 為 Helm Chart 提供了公共和私有資源庫。我們將使用這些 Helm Chart 來設置 Kubernetes 集群中的 pod 和服務。
Helm 社區為各種 Kubernetes 應用程序創建、管理和維護 Helm Chart,這些資源會時常更新。Helm Chart 可重復使用,安裝簡單。想要使用 Helm 在 Kubernetes 上集成 Prometheus ,讓我們從安裝 Helm 開始。
安裝 Helm
安裝 Helm 之前,必須使用以下命令啟動 Minikube Kubernetes:
minikube start --driver=docker
下圖顯示 Minikube 正在運行。Kubectl 工具現已配置為使用 Minikube。
接下來,使用以下命令(根據你使用的操作系統使用對應的命令)安裝 Helm:
在 Linux 上安裝 Helm
sudo apt-get install helm
在 Windows 上安裝 Helm
choco install Kubernetes-helm
在 macOS 系統上安裝 Helm
brew install helm
提示:如果遇到問題,可以查看 Helm 官方文檔指南 https://helm.sh/docs/intro/install/。
下圖顯示了 Helm 在 Windows 機器上的安裝情況:
Helm 命令
要獲取所有 Helm 命令,請運行此命令:
helm
命令輸出結果:
The Kubernetes package manager Common actions for Helm: - helm search: search for charts - helm pull: download a chart to your local directory to view - helm install: upload the chart to Kubernetes - helm list: list releases of charts Usage: helm [command] Available Commands: completion generate autocompletion scripts for the specified shell create create a new chart with the given name dependency manage a chart's dependencies env helm client environment information get download extended information of a named release help Help about any command history fetch release history install install a chart lint examine a chart for possible issues list list releases package package a chart directory into a chart archive plugin install, list, or uninstall Helm plugins pull download a chart from a repository and (optionally) unpack it in local directory push push a chart to remote registry login to or logout from a registry repo add, list, remove, update, and index chart repositories rollback roll back a release to a previous revision search search for a keyword in charts show show information of a chart status display the status of the named release template locally render templates test run tests for a release uninstall uninstall a release upgrade upgrade a release verify verify that a chart at the given path has been signed and is valid version print the client version information 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,請運行以下命令:
helm search hub prometheus
該命令列出以下 Prometheus Helm Chart:
URL CHART VERSION APP VERSION DESCRIPTION https://artifacthub.io/packages/helm/prometheus... 25.1.0 v2.47.0 Prometheus is a monitoring system and time seri... https://artifacthub.io/packages/helm/truecharts... 13.0.7 2.47.1 kube-prometheus-stack collects Kubernetes manif... https://artifacthub.io/packages/helm/saurabh6-p... 0.2.0 1.1 This is a Helm Chart for Prometheus Setup. https://artifacthub.io/packages/helm/prometheus... 13.0.0 2.22.1 Prometheus is a monitoring system and time seri... https://artifacthub.io/packages/helm/wenerme/pr... 25.1.0 v2.47.0 Prometheus is a monitoring system and time seri... https://artifacthub.io/packages/helm/wener/prom... 25.1.0 v2.47.0 Prometheus is a monitoring system and time seri... https://artifacthub.io/packages/helm/romanow-he... 1.3.5 2.40.0 Prometheus collects and stores its metrics as t... https://artifacthub.io/packages/helm/mach1el-ch... 1.0.1 v2.47.0 Prometheus Helm chart for Kubernetes 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,請運行以下命令:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update
輸出結果:
"prometheus-community" has been added to your repositories Hang tight while we grab the latest from your chart repositories... ...Successfully got an update from the "grafana" chart repository ...Successfully got an update from the "prometheus-community" chart repository ...Successfully got an update from the "bitnami" chart repository Update Complete. ?Happy Helming!?
現在,我們已經下載好最新版本的 Prometheus 了。
在 K8s 集群上安裝 Prometheus Helm Chart
要在 K8s 集群上安裝 Prometheus Helm Chart,請運行以下helm install命令:
helm install prometheus prometheus-community/prometheus
輸出結果:
NAME: my-prometheus LAST DEPLOYED: Thu Oct 12 20:06:57 2023 NAMESPACE: monitoring STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster: my-prometheus-server.monitoring.svc.cluster.local Get the Prometheus server URL by running these commands in the same shell: 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}") kubectl --namespace monitoring port-forward $POD_NAME 9090 The Prometheus alertmanager can be accessed via port 9093 on the following DNS name from within your cluster: my-prometheus-alertmanager.monitoring.svc.cluster.local Get the Alertmanager URL by running these commands in the same shell: 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}") kubectl --namespace monitoring port-forward $POD_NAME 9093 ################################################################################# ###### WARNING: Pod Security Policy has been disabled by default since ##### ###### it deprecated after k8s 1.25+. use ##### ###### (index .Values "prometheus-node-exporter" "rbac" ##### ###### . "pspEnabled") with (index .Values ##### ###### "prometheus-node-exporter" "rbac" "pspAnnotations") ##### ###### in case you still need it. ##### ################################################################################# The Prometheus PushGateway can be accessed via port 9091 on the following DNS name from within your cluster: my-prometheus-prometheus-pushgateway.monitoring.svc.cluster.local Get the PushGateway URL by running these commands in the same shell: export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=prometheus-pushgateway,component=pushgateway" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace monitoring port-forward $POD_NAME 9091 For more information on running Prometheus, visit: https://prometheus.io/
現在我們已經在 Kubernetes 集群上安裝了 Prometheus。我們可以通過80端口訪問 Prometheus 服務器。下一步是查看部署的 Kubernetes 資源,也就是 Helm Chart 在 Kubernetes 集群中創建的 Pod 和服務。
要查看已部署的 Kubernetes 資源,請運行以下kubectl命令:
kubectl get all
輸出結果:
NAME READY STATUS RESTARTS AGE pod/my-prometheus-prometheus-node-exporter-d5l8m 1/1 Running 0 27m pod/my-prometheus-prometheus-node-exporter-kfbxb 1/1 Running 0 27m pod/my-prometheus-prometheus-pushgateway-86d6f795cb-mlb7d 1/1 Running 0 27m pod/my-prometheus-kube-state-metrics-6c4b65c7b9-wbh9r 1/1 Running 0 27m pod/my-prometheus-prometheus-node-exporter-2z7rp 1/1 Running 0 27m pod/my-prometheus-alertmanager-0 1/1 Running 0 27m pod/my-prometheus-server-795d8fc685-zcfct 2/2 Running 0 27m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/my-prometheus-alertmanager-headless ClusterIP None9093/TCP 27m service/my-prometheus-server ClusterIP 10.43.186.10 80/TCP 27m service/my-prometheus-prometheus-pushgateway ClusterIP 10.43.154.83 9091/TCP 27m service/my-prometheus-alertmanager ClusterIP 10.43.217.41 9093/TCP 27m service/my-prometheus-kube-state-metrics ClusterIP 10.43.168.65 8080/TCP 27m service/my-prometheus-prometheus-node-exporter ClusterIP 10.43.195.241 9100/TCP 27m NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/my-prometheus-prometheus-node-exporter 3 3 3 3 3 kubernetes.io/os=linux 27m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/my-prometheus-prometheus-pushgateway 1/1 1 1 27m deployment.apps/my-prometheus-kube-state-metrics 1/1 1 1 27m deployment.apps/my-prometheus-server 1/1 1 1 27m NAME DESIRED CURRENT READY AGE replicaset.apps/my-prometheus-prometheus-pushgateway-86d6f795cb 1 1 1 27m replicaset.apps/my-prometheus-kube-state-metrics-6c4b65c7b9 1 1 1 27m replicaset.apps/my-prometheus-server-795d8fc685 1 1 1 27m NAME READY AGE 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 服務,請運行以下命令:
kubectl get service
輸出結果:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-prometheus-alertmanager-headless ClusterIP None9093/TCP 28m my-prometheus-server ClusterIP 10.43.186.10 80/TCP 28m my-prometheus-prometheus-pushgateway ClusterIP 10.43.154.83 9091/TCP 28m my-prometheus-alertmanager ClusterIP 10.43.217.41 9093/TCP 28m my-prometheus-kube-state-metrics ClusterIP 10.43.168.65 8080/TCP 28m my-prometheus-prometheus-node-exporter ClusterIP 10.43.195.241 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-serverKubernetes service 來訪問 Prometheus 應用程序。prometheus-server是 ClusterIP 類型。您只能在 Kubernetes 集群內訪問它。因此我們需要公開這個 Kubernetes service,以便在 Kubernetes 集群外部訪問它。公開prometheus-serverKubernetes service 將生成一個 URL。我們可以在瀏覽器上加載 URL 并訪問正在運行的應用程序。
公開 prometheus-server Kubernetes service
要公開prometheus-serverKubernetes service,請運行以下命令:
kubectl expose service prometheus-server --type=NodePort --target-port=9090 --name=prometheus-server-ext
該命令會將ClusterIP類型轉換為NodePort類型。這樣,prometheus-server就能在 Kubernetes 集群之外通過 9090 端口訪問。
現在,我們公開了prometheus-serverKubernetes service。接下來使用以下命令訪問 Prometheus 應用程序:
minikube service prometheus-server-ext
該命令生成以下 URL:
URL 可能需要稍等一會兒才可用。您需要在瀏覽器上進行多次重試,直到使用此 URL 訪問 Prometheus Kubernetes 應用程序。您還需要保持終端打開并運行命令,以便繼續訪問服務。
到這里,我們已經成功使用 Helm 在 Kubernetes 上安裝了 Prometheus。Prometheus 已經在集群內部運行,我們可以使用瀏覽器或 URL 來進行訪問。
鏈接:https://blog.51cto.com/u_15682575/9174831
審核編輯:劉清
-
Linux系統
+關注
關注
4文章
591瀏覽量
27354 -
URL
+關注
關注
0文章
139瀏覽量
15312
原文標題:如何使用 Helm 在 K8s 上集成 Prometheus
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論