How to use Helm to integrate Prometheus and Grafana Part 2 on K8s

Mondo Technology Updated on 2024-02-01

In Part 1, we looked at what Prometheus and Grafana are, as well as the prerequisites and benefits of using these tools. In this section, you'll continue to learn how to install Helm and how to use Prometheus Helm charts.

Artifacthub provides public and private repositories for Helm Chart. We'll use these helm charts to set up pods and services in a kubernetes cluster.

The Helm community creates, manages, and maintains Helm charts for a variety of Kubernetes applications, and these resources are updated frequently. The Helm Chart is reusable and easy to install. To integrate Prometheus and Grafana on Kubernetes using Helm, let's start by installing Helm.

Before installing Helm, you must start minikube kubernetes with the following command:

minikube start --driver=docker
The following diagram shows minikube in operation. The kubectl tool is now configured to use minikube.

Next, install helm:: using the following command (depending on the operating system you're using).

Install helm on linux

sudo apt-get install helm
Install Helm on Windows

choco install kubernetes-helm
Install Helm on your macOS system

brew install helm
Tip: If you're having trouble, you can check out the official Helm documentation guide

The following diagram shows the installation of Helm on a Windows machine

To get all the helm commands, run this command:

helm
Command output:

the kubernetes package managercommon 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 chartsusage: helm [command]**ailable 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 informationuse "helm [command] -help" for more information about a command.
The most commonly used helm commands are.

Helm Search: Search for Helm charts in the ArtifactHub repository.

helm pull: Extract and helm chart from the ArtifactHub repository.

Helm install: Upload the Helm chart and deploy it to the Kubernetes cluster.

Helm List: Lists all Helm charts deployed in a Kubernetes cluster.

First, we'll search for Prometheus Helm charts. To search for Prometheus Helm, run the following command:

helm search hub prometheus
The command lists the following Prometheus Helm charts:

url chart version app version description v2.47.0 prometheus is a monitoring system and time seri...2.47.1 kube-prometheus-stack collects kubernetes manif...1.1 this is a helm chart for prometheus setup. 2.22.1 prometheus is a monitoring system and time seri...v2.47.0 prometheus is a monitoring system and time seri...v2.47.0 prometheus is a monitoring system and time seri...2.40.0 prometheus collects and stores its metrics as t...v2.47.0 prometheus helm chart for kubernetes prometheus instance created by the coreos prome...
You can also head over to the ArtifactHub repository (and search for the official Prometheus Helm chart, as shown in the image below:

The first on the list is the official Prometheus Helm chart. To get this helm chart, run the following command:

helm repo add prometheus-community repo update
Output:

"prometheus-community" has been added to your repositorieshang 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 repositoryupdate complete. ⎈happy helming!⎈
Now, we've got the latest version of Prometheus ready.

To install Prometheus Helm Chart on a K8S cluster, run the followinghelm installCommand:

helm install prometheus prometheus-community/prometheus
Output:

name: my-prometheuslast deployed: thu oct 12 20:06:57 2023namespace: monitoringstatus: deployedrevision: 1test suite: nonenotes:the prometheus server can be accessed via port 80 on the following dns name from within your cluster:my-prometheus-server.monitoring.svc.cluster.localget 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="") kubectl --namespace monitoring port-forward $pod_name 9090the prometheus alertmanager can be accessed via port 9093 on the following dns name from within your cluster:my-prometheus-alertmanager.monitoring.svc.cluster.localget 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="") 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.localget 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="") kubectl --namespace monitoring port-forward $pod_name 9091for more information on running prometheus, visit:
Now we have Prometheus installed on the Kubernetes cluster. We can access the Prometheus server through port 80. The next step is to look at the deployed Kubernetes resources, which are the Pods and services that the Helm Chart has created in the Kubernetes cluster.

To view the deployed Kubernetes resources, run the followingkubectlCommand:

kubectl get all
Output:

name ready status restarts agepod/my-prometheus-prometheus-node-exporter-d5l8m 1/1 running 0 27mpod/my-prometheus-prometheus-node-exporter-kfbxb 1/1 running 0 27mpod/my-prometheus-prometheus-pushgateway-86d6f795cb-mlb7d 1/1 running 0 27mpod/my-prometheus-kube-state-metrics-6c4b65c7b9-wbh9r 1/1 running 0 27mpod/my-prometheus-prometheus-node-exporter-2z7rp 1/1 running 0 27mpod/my-prometheus-alertmanager-0 1/1 running 0 27mpod/my-prometheus-server-795d8fc685-zcfct 2/2 running 0 27mname type cluster-ip external-ip port(s) ageservice/my-prometheus-alertmanager-headless clusterip none 9093/tcp 27mservice/my-prometheus-server clusterip 10.43.186.10 80/tcp 27mservice/my-prometheus-prometheus-pushgateway clusterip 10.43.154.83 9091/tcp 27mservice/my-prometheus-alertmanager clusterip 10.43.217.41 9093/tcp 27mservice/my-prometheus-kube-state-metrics clusterip 10.43.168.65 8080/tcp 27mservice/my-prometheus-prometheus-node-exporter clusterip 10.43.195.241 9100/tcp 27mname desired current ready up-to-date **ailable node selector agedaemonset.apps/my-prometheus-prometheus-node-exporter 3 3 3 3 3 kubernetes.io/os=linux 27mname ready up-to-date **ailable agedeployment.apps/my-prometheus-prometheus-pushgateway 1/1 1 1 27mdeployment.apps/my-prometheus-kube-state-metrics 1/1 1 1 27mdeployment.apps/my-prometheus-server 1/1 1 1 27mname desired current ready agereplicaset.apps/my-prometheus-prometheus-pushgateway-86d6f795cb 1 1 1 27mreplicaset.apps/my-prometheus-kube-state-metrics-6c4b65c7b9 1 1 1 27mreplicaset.apps/my-prometheus-server-795d8fc685 1 1 1 27mname ready agestatefulset.apps/my-prometheus-alertmanager 1/1 27m
Installing the helm chart creates the following Kubernetes resources:

pod: Hosts deployed Prometheus Kubernetes applications within a cluster.

replica sets: A collection of instances of the same application within a Kubernetes cluster. It improves the reliability of the application.

deployments: This is the blueprint for creating the application pod.

services: This is a pod running inside a public kubernetes cluster. We use it to access deployed Kubernetes applications.

The next step is to access and launch the Prometheus Kubernetes application. Here, the application will be accessed using Prometheus' Kubernetes service. To get all the Kubernetes services for Prometheus, run the following command:

kubectl get service
Output:

name type cluster-ip external-ip port(s) agemy-prometheus-alertmanager-headless clusterip none 9093/tcp 28mmy-prometheus-server clusterip 10.43.186.10 80/tcp 28mmy-prometheus-prometheus-pushgateway clusterip 10.43.154.83 9091/tcp 28mmy-prometheus-alertmanager clusterip 10.43.217.41 9093/tcp 28mmy-prometheus-kube-state-metrics clusterip 10.43.168.65 8080/tcp 28mmy-prometheus-prometheus-node-exporter clusterip 10.43.195.241 9100/tcp 28m
The output lists the following K8S services for Prometheus

prometheus-alertmanager

prometheus-alertmanager-headless

prometheus-kube-state-metrics

prometheus-prometheus-node-exporter

prometheus-prometheus-pushgateway

prometheus-server

We will useprometheus-serverKubernetes service to access Prometheus applications. prometheus-serveris of type clusterip. You can only access it within a Kubernetes cluster. So we need to expose this kubernetes service so that it can be accessed outside of the kubernetes cluster. Publicprometheus-serverThe Kubernetes service will generate a URL. We can load the URL on the browser and access the running application.

Be publicprometheus-serverkubernetes service, run the following command:

kubectl expose service prometheus-server --type=nodeport --target-port=9090 --name=prometheus-server-ext
The command willclusteriptype tonodeportType. In this way,prometheus-serverIt can be accessed outside of the Kubernetes cluster via port 9090.

Now, we're out thereprometheus-serverkubernetes service。Next, use the following command to access the Prometheus application:

minikube service prometheus-server-ext
The command generates the following URL:

The URL may need to wait a while for it to become available. You'll need to do multiple retries on your browser until you use this URL to access the Prometheus Kubernetes application. You'll also need to keep your terminal open and running commands in order to continue accessing the service.

At this point, we have successfully installed Prometheus on Kubernetes using Helm. Prometheus is already running inside the cluster and we can access it using a browser or URL.

Related Pages