In this tutorial, we’ll discuss how to set up Prometheus on Kubernetes. We’ll assume that you’ve already installed Helm and K3s. If you don’t have K3s and Helm installed you can follow our guide. Prometheus has established itself as a top open-source option for monitoring and alerting systems. It is renowned for its multi-dimensional data model with time series data identified by metric name and key/value pairs, flexible query language, and integration with many well-liked graphing and dashboarding solutions.
Introduction
Due to its powerful features and usability, Prometheus has established itself as a go-to solution for many organizations. You can build a strong platform for monitoring your applications and systems by combining it with K3s, a Kubernetes distribution that is incredibly effective and streamlined.
Getting Your Environment Ready
It’s important to confirm that your environment is properly configured before we start the installation process of Prometheus on Kubernetes. We won’t do K3s and Helm installation because we’ll assume that they are already installed.
You must ensure that Helm is installed and updated to the most recent version and that the K3s cluster is operational. Run kubectl get nodes to verify the K3s cluster, and helm version to verify Helm.
Deploy Prometheus on Kubernetes
The Prometheus Operator is an open-source project that enables quick monitoring definitions for Kubernetes services as well as the deployment and administration of Prometheus instances.
First, add the Prometheus community Helm charts by executing the following command:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
Next, update the Helm repository:
helm repo update
Now, create a namespace for Prometheus:
kubectl create namespace prometheus
Finally, install the Prometheus Operator:
helm install prometheus prometheus-community/kube-prometheus-stack --namespace prometheus
This command will install the Prometheus Operator in the ‘prometheus’ namespace.
Create a Prometheus Configuration File
To specify your Prometheus on Kubernetes instance, Service, and ServiceMonitor, you must create a configuration file (prometheus.yaml). Here is an easy illustration:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
namespace: prometheus
spec:
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
team: frontend
resources:
requests:
memory: 400Mi
enableAdminAPI: false
---
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: prometheus
spec:
selector:
prometheus: prometheus
type: NodePort
ports:
- name: web
port: 9090
targetPort: web
nodePort: 30900
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: k3s
labels:
team: frontend
spec:
selector:
matchLabels:
app: k3s
endpoints:
- port: web
interval: 15s
Use kubectl
to apply the configuration file and deploy Prometheus on Kubernetes:
kubectl apply -f prometheus.yaml -n prometheus
This command will deploy Prometheus in the ‘prometheus’ namespace using the configuration specified in the prometheus.yaml
file.
Once Prometheus is deployed, you can access it by using the kubectl port-forward
command:
kubectl port-forward svc/prometheus 9090:9090 -n prometheus
Then, open your web browser and navigate to http://localhost:9090
.
Configuring Prometheus on Kubernetes
You must set up the Prometheus Operator to monitor your K3s cluster after installation. In order to monitor Kubernetes services, the Prometheus Operator provides a custom resource called a ServiceMonitor, which you will create to accomplish this.
Here is a sample ServiceMonitor configuration:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: k3s
labels:
release: prometheus
spec:
selector:
matchLabels:
k8s-app: kube-state-metrics
namespaceSelector:
matchNames:
- kube-system
endpoints:
- port: http-metrics
interval: 30s
Save this configuration to a file named ‘servicemonitor.yaml’, then apply it with kubectl apply -f servicemonitor.yaml -n prometheus.
Verifying the Deployment
Once Prometheus has been installed, you should make sure it is functioning properly. To begin with, make sure the Prometheus pods are operational:
kubectl get pods -n prometheus
You should see the Prometheus pods listed. Next, check that the Prometheus service is running:
kubectl get service -n prometheus
Monitoring Your K3s Cluster
You can use Prometheus to monitor your K3s cluster now that it has been installed and is operational. Grafana, which is part of the kube-prometheus-stack Helm chart, allows you to create alerting rules, query your time-series data, and visualize your data.
Final Thoughts
In this guide, we walked you through the process of deploying Prometheus on Kubernetes cluster using Helm. Although the process may seem complicated at first, it becomes straightforward once you understand the steps involved. By deploying Prometheus on K3s, you can leverage a powerful and flexible monitoring solution that can help you keep a close eye on your applications and systems.
1 Comment
How To Install And Configure Docker Swarm On Ubuntu 22.04 - Virtono Community · September 27, 2023 at 11:49 AM
[…] Docker Swarm on Ubuntu cluster to guarantee the functionality and performance of your applications. Prometheus and Grafana are two applications you can use to collect and display metrics. In order to handle […]