You run a small fleet of Prometheus servers — one per environment, one per cluster, one in front of the edge — and the retention is fifteen days because that’s the longest the local disk will tolerate. Every quarter someone asks for a year-over-year graph. Every quarter you tell them the metrics aren’t there. Then a customer-facing incident asks the question “how long has this latency p99 been creeping up” and the honest answer is “we don’t know, the data fell off the end three weeks ago.” Long-term metric storage isn’t a nice-to-have once the business starts asking historical questions — it’s the difference between metrics-as-monitoring and metrics-as-evidence.
What’s happening
A single Prometheus server is designed for two weeks of high-resolution data on a local TSDB. Beyond that the disk fills, query performance degrades, and a node failure means you’ve lost everything. Thanos is the standard answer: a sidecar runs next to each Prometheus and ships completed two-hour TSDB blocks to object storage; a store gateway serves queries against those blocks from S3; a querier sits in front, federates across all sidecars and stores, and presents a single PromQL endpoint; a compactor runs in the background, deduplicates overlapping blocks, applies retention, and produces 5-minute and 1-hour downsampled views so queries over a year don’t have to scan raw samples.
The thing that breaks people is forgetting that the sidecar is not an alternative path for queries — it’s the upload path. Queries flow through the querier, which fans out to every sidecar (for fresh data not yet uploaded) and every store gateway (for historical blocks). Skip the store gateway and your year-over-year query returns nothing. Skip the compactor and your S3 bill compounds; raw two-hour blocks for two years across thirty Prometheus servers is millions of files.
How to verify
Sidecars reachable from the querier:
curl -fs http://thanos-querier:10902/api/v1/stores | jq '.data[] | {name, lastCheck, lastError}'
Every entry should have lastError: null. A connection refused here means a sidecar is down or the gRPC port (10901) is firewalled.
Object storage actually receiving blocks:
thanos tools bucket inspect --objstore.config-file=/etc/thanos/objstore.yml \
--sort-by=FROM | tail -20
You want the FROM column to be within the last 2-3 hours for active Prometheus servers — otherwise the sidecar isn’t shipping.
Compactor caught up:
curl -fs http://thanos-compactor:10902/metrics | grep thanos_compact_iterations_total
curl -fs http://thanos-compactor:10902/metrics | grep thanos_compact_halted
thanos_compact_halted == 1 is a stop-the-world signal — usually an overlap from two sidecars sharing the same external_labels. Fix the labels before doing anything else.
The fix
Save the script as /root/setup-thanos.sh and run it on a dedicated Ubuntu 22.04 host (the “Thanos box”). It installs the binaries, writes systemd units for querier + store + compactor + ruler, plus a sidecar unit you copy to each Prometheus host. S3 credentials go through environment variables so the same config file works in every environment.
#!/usr/bin/env bash
set -euo pipefail
### Thanos 0.36 installer on Ubuntu 22.04
### Run on the central "Thanos box" — querier + store + compactor + ruler
### Sidecar systemd unit is generated at the end; copy it to each Prometheus host
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT ===
THANOS_VERSION="0.36.1"
PROM_HOSTS=("prom01:10901" "prom02:10901" "prom03:10901")
S3_BUCKET="acme-thanos-metrics"
S3_ENDPOINT="s3.us-east-1.amazonaws.com"
S3_REGION="us-east-1"
S3_ACCESS_KEY="CHANGE_ME"
S3_SECRET_KEY="CHANGE_ME"
RAW_RETENTION="30d"
DOWNSAMPLE_5M_RETENTION="180d"
DOWNSAMPLE_1H_RETENTION="2y"
# ============
echo "==> Base packages"
apt-get update
apt-get install -y curl wget tar gzip ca-certificates ufw jq
echo "==> Creating thanos user"
id -u thanos &>/dev/null || useradd --system --no-create-home --shell /usr/sbin/nologin thanos
echo "==> Downloading Thanos ${THANOS_VERSION}"
cd /tmp
ARCH="linux-amd64"
TARBALL="thanos-${THANOS_VERSION}.${ARCH}.tar.gz"
wget -q "https://github.com/thanos-io/thanos/releases/download/v${THANOS_VERSION}/${TARBALL}"
tar xzf "${TARBALL}"
install -m 0755 "thanos-${THANOS_VERSION}.${ARCH}/thanos" /usr/local/bin/thanos
rm -rf "thanos-${THANOS_VERSION}.${ARCH}" "${TARBALL}"
echo "==> Object store config"
mkdir -p /etc/thanos /var/lib/thanos
chown thanos:thanos /var/lib/thanos
cat > /etc/thanos/objstore.yml <<EOF
type: S3
config:
bucket: "${S3_BUCKET}"
endpoint: "${S3_ENDPOINT}"
region: "${S3_REGION}"
access_key: "${S3_ACCESS_KEY}"
secret_key: "${S3_SECRET_KEY}"
insecure: false
signature_version2: false
put_user_metadata: {}
http_config:
idle_conn_timeout: 90s
response_header_timeout: 2m
insecure_skip_verify: false
trace:
enable: false
list_objects_version: "v2"
bucket_lookup_type: auto
part_size: 67108864
EOF
chown thanos:thanos /etc/thanos/objstore.yml
chmod 600 /etc/thanos/objstore.yml
echo "==> Querier unit"
STORE_FLAGS=""
for h in "${PROM_HOSTS[@]}"; do
STORE_FLAGS+="--endpoint=${h} "
done
cat > /etc/systemd/system/thanos-querier.service <<EOF
[Unit]
Description=Thanos Querier
After=network.target
[Service]
Type=simple
User=thanos
Group=thanos
ExecStart=/usr/local/bin/thanos query \\
--http-address=0.0.0.0:10902 \\
--grpc-address=0.0.0.0:10903 \\
--query.replica-label=replica \\
--query.replica-label=prometheus_replica \\
--query.auto-downsampling \\
--query.timeout=2m \\
--query.max-concurrent=20 \\
--query.lookback-delta=5m \\
${STORE_FLAGS} \\
--endpoint=127.0.0.1:10905 \\
--endpoint=127.0.0.1:10907
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
EOF
echo "==> Store gateway unit"
cat > /etc/systemd/system/thanos-store.service <<'EOF'
[Unit]
Description=Thanos Store Gateway
After=network.target
[Service]
Type=simple
User=thanos
Group=thanos
ExecStart=/usr/local/bin/thanos store \
--data-dir=/var/lib/thanos/store \
--objstore.config-file=/etc/thanos/objstore.yml \
--http-address=0.0.0.0:10904 \
--grpc-address=0.0.0.0:10905 \
--chunk-pool-size=4GB \
--store.grpc.series-max-concurrency=20 \
--max-time=-2w
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
EOF
echo "==> Compactor unit"
cat > /etc/systemd/system/thanos-compactor.service <<EOF
[Unit]
Description=Thanos Compactor
After=network.target
[Service]
Type=simple
User=thanos
Group=thanos
ExecStart=/usr/local/bin/thanos compact \\
--data-dir=/var/lib/thanos/compactor \\
--objstore.config-file=/etc/thanos/objstore.yml \\
--http-address=0.0.0.0:10906 \\
--wait \\
--retention.resolution-raw=${RAW_RETENTION} \\
--retention.resolution-5m=${DOWNSAMPLE_5M_RETENTION} \\
--retention.resolution-1h=${DOWNSAMPLE_1H_RETENTION} \\
--compact.concurrency=2 \\
--downsample.concurrency=2 \\
--consistency-delay=30m \\
--deduplication.replica-label=replica \\
--deduplication.replica-label=prometheus_replica
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
EOF
echo "==> Ruler unit (optional global recording/alerting rules)"
mkdir -p /etc/thanos/rules
cat > /etc/thanos/rules/global.yml <<'EOF'
groups:
- name: thanos-global
interval: 30s
rules:
- record: job:request_latency:p99_5m
expr: histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (job, le))
EOF
chown -R thanos:thanos /etc/thanos/rules
cat > /etc/systemd/system/thanos-ruler.service <<EOF
[Unit]
Description=Thanos Ruler
After=network.target
[Service]
Type=simple
User=thanos
Group=thanos
ExecStart=/usr/local/bin/thanos rule \\
--data-dir=/var/lib/thanos/ruler \\
--objstore.config-file=/etc/thanos/objstore.yml \\
--rule-file=/etc/thanos/rules/*.yml \\
--query=127.0.0.1:10902 \\
--label=replica="r0" \\
--label=ruler_cluster="primary" \\
--http-address=0.0.0.0:10908 \\
--grpc-address=0.0.0.0:10907 \\
--eval-interval=30s
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
EOF
echo "==> Starting services"
mkdir -p /var/lib/thanos/{store,compactor,ruler}
chown -R thanos:thanos /var/lib/thanos
systemctl daemon-reload
systemctl enable --now thanos-store thanos-compactor thanos-ruler thanos-querier
echo "==> Firewall (querier 10902, store grpc 10905, ruler grpc 10907)"
ufw allow 10902/tcp || true
ufw allow 10903/tcp || true
ufw allow 10904/tcp || true
ufw allow 10906/tcp || true
ufw allow 10908/tcp || true
echo "==> Generating sidecar unit (copy to each Prometheus host)"
cat > /tmp/thanos-sidecar.service.template <<'EOF'
[Unit]
Description=Thanos Sidecar (next to Prometheus)
After=network.target prometheus.service
Wants=prometheus.service
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/bin/thanos sidecar \
--prometheus.url=http://127.0.0.1:9090 \
--tsdb.path=/var/lib/prometheus \
--objstore.config-file=/etc/thanos/objstore.yml \
--http-address=0.0.0.0:10911 \
--grpc-address=0.0.0.0:10901 \
--shipper.upload-compacted=false \
--reloader.config-file=/etc/prometheus/prometheus.yml \
--reloader.rule-dir=/etc/prometheus/rules
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
EOF
echo
echo "========================================"
echo "DONE — central Thanos services up"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(10902|10903|10904|10905|10906|10907|10908)\b' || true
echo
echo "Bucket inspection:"
sudo -u thanos /usr/local/bin/thanos tools bucket inspect \
--objstore.config-file=/etc/thanos/objstore.yml --sort-by=FROM || true
echo
echo "Stores known to querier:"
curl -fs http://127.0.0.1:10902/api/v1/stores | jq '.data[] | {name, lastError}' || true
echo
echo "Next: on each Prometheus host"
echo " 1) install thanos binary (same release)"
echo " 2) copy /etc/thanos/objstore.yml from this box"
echo " 3) add external_labels.cluster + external_labels.replica to prometheus.yml"
echo " 4) drop the unit at /tmp/thanos-sidecar.service.template"
echo " 5) systemctl enable --now thanos-sidecar"
After running on the central host, the sidecar piece is what most people get wrong. Each Prometheus needs two things in its config:
global:
external_labels:
cluster: "prod-east" # MUST differ across servers
replica: "0" # 0 for primary, 1 for HA replica
If two Prometheus servers ship blocks with the same cluster label and replica: 0, the compactor halts with an overlap and refuses to compact anything until you intervene. Set the labels once, never reuse them, and min-block-duration = max-block-duration = 2h in Prometheus so the sidecar can claim blocks cleanly.
Run this stack under our monitoring + on-call tier with retention, downsampling, and S3 lifecycle policies tuned for your real query patterns — or book a long-term metrics scoping call if you want us to migrate an existing Prometheus fleet.