
Prometheus K8s
Here’s a tutorial on how to set up Prometheus auto-discovery for pods with the label app=beta:
-
Deploy Prometheus: To get started, you’ll need to deploy Prometheus in your Kubernetes cluster. There are several ways to do this, but one common approach is to use a Prometheus Operator, which is a tool that automates the deployment and management of Prometheus instances. If you don’t have the Prometheus Operator installed, you can find installation instructions in the official documentation.
-
Define a ServiceMonitor: To enable Prometheus to automatically discover pods with the label app=beta, you’ll need to define a ServiceMonitor resource in your cluster. A ServiceMonitor is a custom resource definition (CRD) that tells Prometheus what pods to monitor.
Here’s an example ServiceMonitor that discovers pods with the label app=beta:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: beta-app-servicemonitor
labels:
app: beta
spec:
selector:
matchLabels:
app: beta
endpoints:
- port: http
interval: 30s
path: /metrics
Save this YAML file as beta-app-servicemonitor.yaml and run the following command to create the ServiceMonitor:
$ kubectl apply -f beta-app-servicemonitor.yaml
- Label the pods: Next, you need to label the pods that you want to monitor with the app=beta label. For example, if you already have pods created with the following YAML file:
apiVersion: v1
kind: Pod
metadata:
name: beta-pod-1
labels:
app: beta
spec:
containers:
- name: beta-container-1
image: nginx
---
apiVersion: v1
kind: Pod
metadata:
name: beta-pod-2
labels:
app: beta
spec:
containers:
- name: beta-container-2
image: nginx
You can label the pods using the following command:
$ kubectl label pods beta-pod-1 beta-pod-2 app=beta
- Verify auto-discovery:
Once you’ve created the ServiceMonitor and labeled the pods, Prometheus should automatically discover the pods and start scraping their metrics. You can verify this by checking the Prometheus targets page, which should show the newly discovered pods. This is a simple example of how to set up Prometheus auto-discovery for pods with a specific label. You can modify this example to suit your specific requirements, such as monitoring different labels, namespaces, or endpoints.
Example
To add Prometheus metrics to a Golang web service, you will need to use a Prometheus client library for Golang. Here’s a simple example that demonstrates how to expose a metric for the number of requests handled by a web service:
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
var (
requestsCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "requests_total",
Help: "The total number of HTTP requests.",
},
)
)
func init() {
prometheus.MustRegister(requestsCounter)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestsCounter.Inc()
w.Write([]byte("Hello, World!"))
})
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
In this example, the package github.com/prometheus/client_golang/prometheus is imported to use the Prometheus client library. A counter metric requestsCounter is defined using the prometheus.NewCounter function. This metric tracks the total number of HTTP requests. The metric must be registered with Prometheus using prometheus.MustRegister.
In the main function, an HTTP handler is created using the http.HandleFunc method. This handler increments the requestsCounter metric and returns a “Hello, World!” response. Another handler is created to expose the Prometheus metrics using promhttp.Handler. The web service listens on port 8080 and serves both the response to client requests and the Prometheus metrics.
You can access the Prometheus metrics at the /metrics endpoint on the web service. These metrics can then be scraped by a Prometheus server to be stored and analyzed.