Writing Data from Prometheus to Thanos

  ·   4 min read

In the world of cloud-native applications, monitoring and observability are crucial for maintaining the health and performance of your systems. Prometheus has become a go-to solution for monitoring due to its powerful querying capabilities and ease of use. However, as organizations scale, they often encounter challenges with Prometheus’s storage limitations. This is where Thanos comes into play, extending Prometheus’s capabilities by providing long-term storage, high availability, and global querying across multiple Prometheus instances.

In this article, we’ll explore how to write data from Prometheus to Thanos, enabling you to leverage Thanos’s powerful features to enhance your monitoring stack.

Understanding Thanos Architecture

Before diving into the integration, it’s essential to understand the key components of Thanos:

  1. Sidecar: This component runs alongside each Prometheus instance, exposing its data to Thanos. It uploads Prometheus’s data to an object storage bucket and serves as a proxy for querying.

  2. Store Gateway: This component reads data from the object storage and serves it to the Querier.

  3. Compactor: It compacts and down-samples data in the object storage, optimizing it for long-term storage.

  4. Querier: This component aggregates data from multiple sources, including Sidecars and Store Gateways, enabling global querying.

  5. Ruler: Similar to Prometheus’s alerting rules, the Ruler evaluates rules against data in object storage.

Prerequisites

  • A running Prometheus instance.
  • An object storage solution (e.g., AWS S3, Google Cloud Storage, or MinIO).
  • A Kubernetes cluster or a server environment to deploy Thanos components.

Step-by-Step Integration

Step 1: Deploy Thanos Sidecar

The Thanos Sidecar is the bridge between Prometheus and Thanos. It uploads Prometheus’s data to the object storage and allows Thanos to query it.

  1. Modify Prometheus Configuration: Ensure that Prometheus is configured to use a persistent volume for its data directory. This is crucial for the Sidecar to access the data.

  2. Deploy Sidecar: Run the Thanos Sidecar alongside your Prometheus instance. Below is a sample Kubernetes deployment configuration:

    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
            args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus"
            volumeMounts:
            - name: prometheus-storage
              mountPath: /prometheus
          - name: thanos-sidecar
            image: thanosio/thanos:latest
            args:
            - "sidecar"
            - "--tsdb.path=/prometheus"
            - "--objstore.config-file=/etc/thanos/objstore.yml"
            volumeMounts:
            - name: prometheus-storage
              mountPath: /prometheus
            - name: thanos-config
              mountPath: /etc/thanos
          volumes:
          - name: prometheus-storage
            persistentVolumeClaim:
              claimName: prometheus-pvc
          - name: thanos-config
            configMap:
              name: thanos-config
    
  3. Configure Object Storage: Create a ConfigMap for the object storage configuration (objstore.yml). Here is an example for AWS S3:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: thanos-config
    data:
      objstore.yml: |
        type: S3
        config:
          bucket: "your-bucket-name"
          endpoint: "s3.amazonaws.com"
          access_key: "your-access-key"
          secret_key: "your-secret-key"
          insecure: false    
    

Step 2: Deploy Thanos Store Gateway

The Store Gateway reads data from the object storage and serves it to the Querier.

  1. Deploy Store Gateway: Use the following configuration to deploy the Store Gateway:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: thanos-store
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: thanos-store
      template:
        metadata:
          labels:
            app: thanos-store
        spec:
          containers:
          - name: thanos-store
            image: thanosio/thanos:latest
            args:
            - "store"
            - "--objstore.config-file=/etc/thanos/objstore.yml"
            volumeMounts:
            - name: thanos-config
              mountPath: /etc/thanos
          volumes:
          - name: thanos-config
            configMap:
              name: thanos-config
    

Step 3: Deploy Thanos Querier

The Querier aggregates data from multiple sources, enabling global querying.

  1. Deploy Querier: Use the following configuration to deploy the Querier:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: thanos-querier
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: thanos-querier
      template:
        metadata:
          labels:
            app: thanos-querier
        spec:
          containers:
          - name: thanos-querier
            image: thanosio/thanos:latest
            args:
            - "query"
            - "--store=thanos-store:10901"
            - "--store=prometheus:10901"
    

Step 4: Access Thanos Querier

Once deployed, you can access the Thanos Querier using a Kubernetes service or an ingress. This allows you to perform global queries across all your Prometheus instances and historical data stored in the object storage.

Conclusion

By integrating Prometheus with Thanos, you can overcome the limitations of Prometheus’s local storage, enabling long-term storage, high availability, and global querying capabilities. This setup is particularly beneficial for organizations with large-scale, distributed systems that require robust monitoring solutions.

Thanos, being an open-source project, provides a cost-effective way to enhance your observability stack without vendor lock-in. As you implement this setup, ensure that your object storage is configured correctly and that you monitor the performance of your Thanos components to maintain a reliable monitoring infrastructure.

References