Sep 28, 2024

Deploying the Kube-Prometheus-Stack (Kubernetes Monitoring)

The kube-prometheus-stack is a powerful combination of software that works together to collect metrics, visualize them, and create alerts. This Kubernetes monitoring solution comprises five crucial components:

  1. Node-Exporter: Collects system-level metrics from all nodes in the Kubernetes cluster, such as CPU usage, memory consumption, disk I/O, and network statistics.

  2. Kube-state-metrics: Collects metrics about the state of Kubernetes objects, such as the number of replicas for deployments, the status of pods, and resource allocation for nodes.

  3. Prometheus: A time-series database that collects and stores all the metric data.

  4. Grafana: A platform that uses metrics from Prometheus to create visualizations.

  5. Alertmanager: A tool that uses metrics from Prometheus to create and manage alerts.


In this article, you will use a Prometheus Helm chart to deploy the kube-prometheus-stack, a comprehensive Kubernetes monitoring solution.

Installing Helm

Mac

If you don't have Homebrew installed, install it first by running:

/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"

Once Homebrew is installed, use it to install Helm:

brew install helm

After the installation is complete, check the Helm version by running:

helm version

Windows

If you don't have Chocolatey installed, install it first. Open PowerShell as Administrator and run:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('<https://community.chocolatey.org/install.ps1>'))

Once Chocolatey is installed, use it to install Helm:

choco install kubernetes-helm

After the installation is complete, check the Helm version by running:

helm version

Prometheus Helm Chart

As seen in the Kubernetes Bootcamp, Helm charts greatly simplify the deployment of sophisticated software. These charts are stored inside repositories.

The Prometheus community repository provides a large collection of Helm charts, most notably the kube-prometheus-stack chart. This chart encapsulates all the necessary components that make up the kube-prometheus-stack, their configurations, and dependencies into a single, easily deployable package.

First, add this repository to your local Helm installation:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

After adding the repository, you can list all available charts:

helm search repo prometheus-community

This command will show you all charts in the prometheus-community repository. Look for the kube-prometheus-stack in the output.

To see the available versions of the kube-prometheus-stack chart, use:

helm search repo prometheus-community/kube-prometheus-stack --versions

Now that we have the repository information, let's install the kube-prometheus-stack version 45.7.1. In Helm terminology, installing a chart is called "releasing" it. Let's release the kube-prometheus-stack into our cluster:

helm install prometheus prometheus-community/kube-prometheus-stack --version 45.7.1 --namespace monitoring --create-namespace

This command does several things:

  • install tells Helm we want to deploy a new release

  • prometheus is the name we're giving to this release

  • prometheus-community/kube-prometheus-stack specifies which chart to install

  • -version 45.7.1 specifies the chart version

  • -namespace monitoring --create-namespace creates and uses a dedicated namespace for our monitoring stack

Helm will now go through the process of creating all necessary Kubernetes resources defined in the chart. This includes deployments, services, config maps, and more - all preconfigured to work together.

After the installation completes, you should see output indicating that the release was successful. However, it may take a few minutes for all pods to start and become ready.

To check the status of your release, you can use:

helm list -n monitoring

Exploring the Deployment

Now that we've deployed the kube-prometheus-stack, let's explore what we've set up. First, check the pods in the monitoring namespace:

kubectl get pods -n monitoring

You should see pods for Prometheus, Grafana, Alertmanager, and various exporters. To access Prometheus, use port-forwarding:

kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090

Now, you can open a web browser and go to http://localhost:9090. Here, you can explore the metrics being collected. Try querying for node_cpu_seconds_total to see CPU metrics collected by Node Exporter, or kube_pod_container_status_running to see metrics about running pods from kube-state-metrics.

Next, let's look at Grafana. Port-forward the Grafana service:

kubectl port-forward -n monitoring svc/prometheus-grafana 3000:80

Visit http://localhost:3000 in your browser. The default credentials are:

  • Username: admin

  • Password: prom-operator

If these credentials don't work, you can retrieve the admin password by running:

kubectl get secret -n monitoring prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

This command decodes the base64-encoded password stored in the Kubernetes secret for Grafana.

Once logged in, you'll see that Grafana has been pre-configured with dashboards that query Prometheus. Navigate to Dashboards -> Browse and look for dashboards like "Node Exporter / Nodes" or "Kubernetes / Compute Resources / Cluster".

These dashboards demonstrate how Grafana is already set up to visualize the metrics collected by Prometheus from both system-level sources (via Node Exporter) and Kubernetes-specific metrics (via kube-state-metrics).

Conclusion

With these steps, you've successfully deployed the kube-prometheus-stack using a Helm chart and verified its functionality. This stack now provides a fully-functional monitoring solution for your Kubernetes cluster, complete with Prometheus for metrics collection, Grafana for visualization, and Alertmanager for handling alerts. The kube-prometheus-stack Helm chart simplifies the deployment process, making it easier to set up comprehensive monitoring for your Kubernetes environment.

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates