RousselTMLEARNING

Observability

Deploying Thanos on Kubernetes (kube-prometheus-stack, Helm and Operator)

Install Thanos on Kubernetes: sidecar via kube-prometheus-stack, objstore secret, Bitnami/kube-thanos, Query, Store, Compactor, ServiceMonitor and resources.

1. Object storage secret #

Shared by the sidecar, store and compactor.

Creation #

cat > objstore.yml <<'EOF'
type: S3
config:
  bucket: thanos-prod
  endpoint: s3.eu-west-3.amazonaws.com
  region: eu-west-3
  access_key: AKIA...
  secret_key: ...
EOF
kubectl create namespace monitoring
kubectl -n monitoring create secret generic thanos-objstore --from-file=objstore.yml
Common mistake: Never commit this file: use External Secrets, Sealed Secrets or IAM roles bound to the ServiceAccount (IRSA) to avoid static keys.

2. Sidecar with kube-prometheus-stack #

A few lines of values.

values.yaml #

Thanos in Kubernetes
The sidecar is injected into the Prometheus pod by the Operator.
# values-thanos.yaml
prometheus:
  thanosService:
    enabled: true
  thanosServiceMonitor:
    enabled: true
  prometheusSpec:
    replicas: 2
    replicaExternalLabelName: replica
    externalLabels: { cluster: prod-eu }
    retention: 2d
    disableCompaction: true
    thanos:
      objectStorageConfig:
        existingSecret: { name: thanos-objstore, key: objstore.yml }
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm upgrade --install monitoring prometheus-community/kube-prometheus-stack -n monitoring -f values-thanos.yaml
kubectl -n monitoring get pods -l app.kubernetes.io/name=prometheus
kubectl -n monitoring logs prometheus-monitoring-kube-prometheus-prometheus-0 -c thanos-sidecar | tail

3. Query, Store and Compactor with Helm #

Bitnami chart.

values-thanos-components.yaml #

# helm repo add bitnami https://charts.bitnami.com/bitnami
objstoreConfig: |-
  type: S3
  config:
    bucket: thanos-prod
    endpoint: s3.eu-west-3.amazonaws.com
    region: eu-west-3

query:
  enabled: true
  replicaLabel: [replica]
  dnsDiscovery:
    sidecarsService: monitoring-kube-prometheus-thanos-discovery
    sidecarsNamespace: monitoring
queryFrontend: { enabled: true }
storegateway:
  enabled: true
  persistence: { enabled: true, size: 20Gi }
compactor:
  enabled: true
  retentionResolutionRaw: 30d
  retentionResolution5m: 180d
  retentionResolution1h: 2y
  persistence: { enabled: true, size: 50Gi }
helm upgrade --install thanos bitnami/thanos -n monitoring -f values-thanos-components.yaml
kubectl -n monitoring get pods -l app.kubernetes.io/instance=thanos
kubectl -n monitoring port-forward svc/thanos-query-frontend 9090:9090      # http://localhost:9090
kubectl -n monitoring port-forward svc/thanos-query 10902:9090 && curl -s localhost:10902/api/v1/stores | jq '.data | keys'

4. Grafana and checks #

Wiring everything together.

Datasource and checks #

# Source de données Grafana : type Prometheus, URL du Query Frontend
- name: Thanos
  type: prometheus
  url: http://thanos-query-frontend.monitoring.svc:9090
  jsonData: { timeInterval: 15s, prometheusType: Thanos }
# Les deux réplicas sont-ils vus et dédupliqués ?
count(up{job="kubelet"}) by (cluster)
# Le compactor est-il sain ?
thanos_compact_halted == 0
Good practice: Set memory requests/limits on the Store Gateway and the Compactor: they use a lot of memory during large block downloads.

Related articles

Thanos: extending Prometheus (high availability, global view and long retention)

Understand why and how to use Thanos: Prometheus limits, components (Sidecar, Query, Store Gateway, Compactor…

Thanos Sidecar: attaching Thanos to Prometheus and uploading to object storage

Set up Thanos Sidecar: Prometheus settings (external labels, 2h blocks), objstore configuration, Docker Compo…

Thanos Query and Query Frontend: a global, deduplicated view

Configure Thanos Query: StoreAPI endpoints, deduplication by replica label, partial response, web interface, …

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!