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:
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.
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.
Prometheus: A time-series database that collects and stores all the metric data.
Grafana: A platform that uses metrics from Prometheus to create visualizations.
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:
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:
Once Chocolatey is installed, use it to install Helm:
After the installation is complete, check the Helm version by running:
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:
After adding the repository, you can list all available charts:
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:
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:
This command does several things:
install
tells Helm we want to deploy a new releaseprometheus
is the name we're giving to this releaseprometheus-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:
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:
You should see pods for Prometheus, Grafana, Alertmanager, and various exporters. To access Prometheus, use port-forwarding:
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:
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:
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.