Configure Prometheus long-term storage with Thanos for unlimited data retention

Advanced 45 min Apr 07, 2026 15 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Deploy Thanos components with Prometheus to achieve unlimited data retention using object storage. This advanced setup enables querying years of historical metrics while maintaining high availability and reducing local storage costs.

Prerequisites

  • Root or sudo access
  • Minimum 4GB RAM
  • Object storage backend (S3, MinIO, or GCS)
  • Basic Prometheus knowledge

What this solves

Prometheus has a built-in 15-day data retention limit that creates gaps in long-term monitoring and capacity planning. Thanos solves this by shipping metric data to object storage (S3, MinIO, GCS) while maintaining the same PromQL query interface. This setup enables unlimited historical data retention, horizontal scaling across multiple Prometheus instances, and significant cost reduction compared to local disk storage.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest versions and security patches.

sudo apt update && sudo apt upgrade -y
sudo apt install -y wget curl tar systemd
sudo dnf update -y
sudo dnf install -y wget curl tar systemd

Create dedicated users

Create separate system users for Prometheus and Thanos components to follow security best practices.

sudo useradd --no-create-home --shell /bin/false prometheus
sudo useradd --no-create-home --shell /bin/false thanos
sudo mkdir -p /etc/prometheus /var/lib/prometheus /etc/thanos /var/lib/thanos
sudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown thanos:thanos /etc/thanos /var/lib/thanos

Download and install Prometheus

Download the latest Prometheus binary and install it with proper permissions.

cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.49.1/prometheus-2.49.1.linux-amd64.tar.gz
tar xzf prometheus-2.49.1.linux-amd64.tar.gz
sudo cp prometheus-2.49.1.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.49.1.linux-amd64/promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
sudo chmod 755 /usr/local/bin/prometheus /usr/local/bin/promtool

Download and install Thanos

Download Thanos binary which includes all components (sidecar, query, store, compactor).

cd /tmp
wget https://github.com/thanos-io/thanos/releases/download/v0.33.0/thanos-0.33.0.linux-amd64.tar.gz
tar xzf thanos-0.33.0.linux-amd64.tar.gz
sudo cp thanos-0.33.0.linux-amd64/thanos /usr/local/bin/
sudo chown thanos:thanos /usr/local/bin/thanos
sudo chmod 755 /usr/local/bin/thanos

Configure object storage

Create the object storage configuration file for Thanos. This example uses MinIO, but you can adapt it for S3 or GCS.

type: s3
config:
  bucket: "thanos-storage"
  endpoint: "203.0.113.10:9000"
  access_key: "thanos-access-key"
  secret_key: "thanos-secret-key-change-this"
  insecure: false
  signature_version2: false
  encrypt_sse: false
  put_user_metadata: {}
  http_config:
    idle_timeout: 90s
    response_header_timeout: 2m
    insecure_skip_verify: false
  trace:
    enable: false
  part_size: 67108864

For AWS S3, use this configuration instead:

type: s3
config:
  bucket: "your-thanos-bucket"
  region: "us-east-1"
  access_key: "AKIAIOSFODNN7EXAMPLE"
  secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
  encrypt_sse: true
sudo chown thanos:thanos /etc/thanos/bucket.yml
sudo chmod 640 /etc/thanos/bucket.yml
Security Notice: Store object storage credentials securely. Consider using IAM roles, environment variables, or secret management systems instead of plain text files in production.

Configure Prometheus for Thanos integration

Configure Prometheus to enable external labels and remote read capabilities for Thanos sidecar.

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    cluster: 'production'
    replica: 'prometheus-01'
    region: 'us-east-1'

rule_files:
  - "/etc/prometheus/rules/*.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'thanos-sidecar'
    static_configs:
      - targets: ['localhost:10902']

  - job_name: 'thanos-query'
    static_configs:
      - targets: ['localhost:10904']

  - job_name: 'thanos-store'
    static_configs:
      - targets: ['localhost:10905']

  - job_name: 'node-exporter'
    static_configs:
      - targets: ['localhost:9100']
sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml
sudo chmod 644 /etc/prometheus/prometheus.yml

Create Prometheus systemd service

Create a systemd service file for Prometheus with Thanos integration enabled.

[Unit]
Description=Prometheus Time Series Database
After=network.target

[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus/ \
  --storage.tsdb.retention.time=2h \
  --storage.tsdb.min-block-duration=2h \
  --storage.tsdb.max-block-duration=2h \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries \
  --web.listen-address=0.0.0.0:9090 \
  --web.enable-lifecycle \
  --web.enable-admin-api
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target
Note: The retention time is set to 2 hours because Thanos sidecar will handle long-term storage. The min/max block duration ensures compatibility with Thanos.

Create Thanos sidecar service

The sidecar runs alongside Prometheus and uploads data blocks to object storage.

[Unit]
Description=Thanos Sidecar
After=network.target prometheus.service
Requires=prometheus.service

[Service]
User=thanos
Group=thanos
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/thanos sidecar \
  --tsdb.path=/var/lib/prometheus \
  --prometheus.url=http://localhost:9090 \
  --grpc-address=0.0.0.0:10901 \
  --http-address=0.0.0.0:10902 \
  --objstore.config-file=/etc/thanos/bucket.yml \
  --log.level=info
KillMode=mixed
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target

Create Thanos query service

Query component provides the unified query interface across all Prometheus instances and object storage.

[Unit]
Description=Thanos Query
After=network.target

[Service]
User=thanos
Group=thanos
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/thanos query \
  --grpc-address=0.0.0.0:10903 \
  --http-address=0.0.0.0:10904 \
  --store=localhost:10901 \
  --store=localhost:10905 \
  --query.replica-label=replica \
  --query.auto-downsampling \
  --log.level=info
KillMode=mixed
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target

Create Thanos store gateway service

Store gateway serves historical data directly from object storage for queries beyond Prometheus retention.

[Unit]
Description=Thanos Store Gateway
After=network.target

[Service]
User=thanos
Group=thanos
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/thanos store \
  --grpc-address=0.0.0.0:10905 \
  --http-address=0.0.0.0:10906 \
  --data-dir=/var/lib/thanos/store \
  --objstore.config-file=/etc/thanos/bucket.yml \
  --log.level=info
KillMode=mixed
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target

Create Thanos compactor service

Compactor downsamples and compacts data in object storage to optimize query performance and reduce storage costs.

[Unit]
Description=Thanos Compactor
After=network.target

[Service]
User=thanos
Group=thanos
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/thanos compact \
  --data-dir=/var/lib/thanos/compactor \
  --objstore.config-file=/etc/thanos/bucket.yml \
  --retention.resolution-raw=30d \
  --retention.resolution-5m=90d \
  --retention.resolution-1h=365d \
  --wait \
  --log.level=info
KillMode=mixed
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target
sudo mkdir -p /var/lib/thanos/store /var/lib/thanos/compactor
sudo chown -R thanos:thanos /var/lib/thanos

Configure firewall rules

Open the necessary ports for Thanos components communication.

sudo ufw allow 9090/tcp comment 'Prometheus'
sudo ufw allow 10901/tcp comment 'Thanos Sidecar gRPC'
sudo ufw allow 10902/tcp comment 'Thanos Sidecar HTTP'
sudo ufw allow 10903/tcp comment 'Thanos Query gRPC'
sudo ufw allow 10904/tcp comment 'Thanos Query HTTP'
sudo ufw allow 10905/tcp comment 'Thanos Store gRPC'
sudo ufw allow 10906/tcp comment 'Thanos Store HTTP'
sudo firewall-cmd --permanent --add-port=9090/tcp
sudo firewall-cmd --permanent --add-port=10901/tcp
sudo firewall-cmd --permanent --add-port=10902/tcp
sudo firewall-cmd --permanent --add-port=10903/tcp
sudo firewall-cmd --permanent --add-port=10904/tcp
sudo firewall-cmd --permanent --add-port=10905/tcp
sudo firewall-cmd --permanent --add-port=10906/tcp
sudo firewall-cmd --reload

Enable and start all services

Start all services in the correct order and enable them for automatic startup.

sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
sudo systemctl enable --now thanos-sidecar
sudo systemctl enable --now thanos-store
sudo systemctl enable --now thanos-query
sudo systemctl enable --now thanos-compactor

Configure Grafana data source

Configure Grafana to use Thanos Query as the data source instead of direct Prometheus connection.

Name: Thanos
Type: Prometheus
URL: http://localhost:10904
Access: Server (default)
Scrape interval: 15s
Query timeout: 60s
HTTP Method: GET

Verify your setup

Check that all services are running correctly and can communicate with each other.

sudo systemctl status prometheus thanos-sidecar thanos-query thanos-store thanos-compactor
curl http://localhost:9090/-/healthy
curl http://localhost:10902/-/healthy
curl http://localhost:10904/-/healthy
curl http://localhost:10905/-/healthy

Test Thanos Query interface and verify it can access both live and historical data:

curl "http://localhost:10904/api/v1/stores"
curl "http://localhost:10904/api/v1/query?query=up"
thanos tools bucket ls --objstore.config-file=/etc/thanos/bucket.yml

Access the web interfaces to confirm everything is working:

  • Prometheus: http://your-server:9090
  • Thanos Query: http://your-server:10904
  • Thanos Sidecar: http://your-server:10902
  • Thanos Store: http://your-server:10906

Common issues

SymptomCauseFix
Sidecar can't upload blocksObject storage credentials or connectivityVerify bucket.yml config and network access: thanos tools bucket ls --objstore.config-file=/etc/thanos/bucket.yml
Query shows no historical dataStore gateway not connected or no uploaded blocksCheck store gateway logs: sudo journalctl -u thanos-store -f
Prometheus blocks not uploadingBlock duration mismatch or insufficient retentionEnsure min/max block duration is 2h in prometheus.service
Permission denied errorsIncorrect file ownershipFix ownership: sudo chown -R thanos:thanos /var/lib/thanos /etc/thanos
gRPC communication failuresFirewall blocking or wrong addressesVerify ports are open and services bind to correct interfaces
Compactor not processing dataInsufficient retention settingsWait 2+ hours for first blocks or check compactor logs

Next steps

Automated install script

Run this to automate the entire setup

#thanos #prometheus #long-term-storage #object-storage #monitoring

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer