top of page
Writer's pictureKateryna

Prometheus Monitoring: A Step-by-Step Guide with Code Examples


Prometheus is a powerful tool for monitoring your applications and infrastructure. Here's a breakdown of how to get started with Prometheus, covering different deployment options, code examples, configuring storage with Persistent Volume Claims (PVCs), and enabling remote write for Mimir:


Building the Prometheus Container (Customization):

This approach allows for customization of the Prometheus configuration. You'll need to build the Prometheus container image using the official Dockerfile or a custom configuration. Here's an example using the official Dockerfile:

Bash

# Clone the Prometheus repository
git clone https://github.com/prometheus/prometheus.git

# Navigate to the directory
cd prometheus

# Build the Prometheus container image (replace 'your-tag' with your desired tag)
docker build -t your-registry/prometheus:your-tag .

Deploying the Standalone Prometheus Container:

Once built, the Prometheus container can be deployed directly on a host machine using Docker or a container orchestration platform like Kubernetes. Remember to configure environment variables or mount configuration files for customization. Here's an example using Docker:

Bash

docker run -d \
  -p 9090:9090 \
  --name prometheus \
  -v prometheus.yml:/etc/prometheus/prometheus.yml \
  -v prometheus-data:/prometheus \
  your-registry/prometheus:your-tag

Deploying Prometheus in Kubernetes (k8s) with PVC:

For a more scalable and managed approach, consider deploying Prometheus within a Kubernetes cluster with a dedicated PVC for storage. Here's a basic deployment manifest example with PVC configuration:

YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prom/prometheus:latest
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: config-volume
          mountPath: /etc/prometheus
        - name: storage-volume
          mountPath: /prometheus
      volumes:
      - name: config-volume
        configMap:
          name: prometheus-config
      - name: storage-volume
        persistentVolumeClaim:
          claimName: prometheus-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Leveraging the Prometheus Operator:

Kubernetes offers the Prometheus Operator, which streamlines deployment and management. The operator automates tasks like service discovery, scaling, and configuration management for Prometheus. Refer to the official Prometheus Operator documentation for detailed instructions on installation and configuration: https://github.com/prometheus-operator/prometheus-operator


Enabling Remote Write for Mimir:

Prometheus can be configured to send scraped metrics to a remote storage solution like Mimir using the remote_write section in the Prometheus configuration file. Here's an example configuration snippet:

YAML

remote_write:
  # Configure Mimir connection details
  - url: http://<mimir-host>:80/api/v1/push
    # Optional: Set authentication credentials for Mimir
    # username: <username>
    # password: <password>

Exploring PromQL Queries:

Once your Prometheus instance is up and running, you can use PromQL, a powerful query language, to retrieve and analyze metrics data. Here are some basic PromQL examples:

  • up{job="node_exporter"} - Check if all node exporters are healthy.

  • irate(http_requests_total{status="200"}[5m]) - Calculate the rate of successful HTTP requests over the last 5 minutes.

  • count by (job) http_requests_total - Count the total HTTP requests grouped by job.

By following these steps and exploring PromQL queries, you can effectively monitor your applications and infrastructure with Prometheus. Remember, the chosen deployment method depends on your specific needs and infrastructure setup.

15 views0 comments

Recent Posts

See All

Comentários


bottom of page