This guide provides a comprehensive step-by-step approach for deploying the Thanos project in a production environment. It is intended for expert developers familiar with Thanos and its ecosystem.

Pre-requisites

Ensure the following prerequisites are met before deploying Thanos in production:

  • One or more Prometheus v2.2.1+ installations with persistent disk.
  • Access to an optional object storage of your choice (e.g., GCP, S3, Azure).
  • Familiarity with Docker and Kubernetes, if deploying in those environments.

Building the Docker Image

Thanos deployment can be optimized through Docker. The image is built using a provided Dockerfile. Below is a sample of the Dockerfile used to create a Thanos Docker image:

# By default we pin to amd64 sha. Use make docker to automatically adjust for arm64 versions.
ARG BASE_DOCKER_SHA="14d68ca3d69fceaa6224250c83d81d935c053fb13594c811038c461194599973"
FROM quay.io/prometheus/busybox@sha256:${BASE_DOCKER_SHA}
LABEL maintainer="The Thanos Authors"

COPY /thanos_tmp_for_docker /bin/thanos

RUN adduser \
    -D `#Dont assign a password` \
    -H `#Dont create home directory` \
    -u 1001 `#User id` \
    thanos && \
    chown thanos /bin/thanos
USER 1001
ENTRYPOINT [ "/bin/thanos" ]

Use the docker target in the Makefile to build the Docker image:

.PHONY: docker
docker: ## Build the Thanos Docker image.
    @echo ">> building Docker image"
    docker build -t thanos:latest .

Configuration for Object Storage

If using object storage, a configuration file must be prepared. This file specifies the object storage settings. Below is an example configuration that should be referenced when starting the Thanos component:

type: S3
config:
  bucket: "your-bucket-name"
  endpoint: "s3.amazonaws.com"
  access_key: "your-access-key"
  secret_key: "your-secret-key"
  insecure: false # set to true only for local testing without SSL

Pass this configuration to the Thanos components using the --objstore.config-file flag.

Deploying Thanos with Kubernetes

When deploying Thanos in a Kubernetes environment, you can use the Helm chart for easier setup. The setup generally involves deploying the Sidecar, Store Gateway, and Query components. Example Kubernetes manifests can be found in the project; below is a simplified example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: thanos-sidecar
spec:
  replicas: 1
  selector:
    matchLabels:
      app: thanos-sidecar
  template:
    metadata:
      labels:
        app: thanos-sidecar
    spec:
      containers:
        - name: thanos-sidecar
          image: thanos:latest
          args:
            - sidecar
            - --prometheus.url=http://<prometheus-server>:9090
            - --objstore.config-file=/etc/thanos/objstore.yaml
          volumeMounts:
            - name: objstore-config
              mountPath: /etc/thanos
      volumes:
        - name: objstore-config
          configMap:
            name: objstore-config

ConfigMap for Object Storage

Create a ConfigMap to hold your object storage configuration:

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

Running Thanos Components

Start the necessary Thanos components such as the Store Gateway and Query components. Here is an example command to start the Thanos Query component:

thanos query \
  --http-address=0.0.0.0:10902 \
  --query.replica-label=replica \
  --store=dnssrv+_grpc._tcp.thanos-store:8080 \
  --objstore.config-file=/path/to/objstore.yaml

Using Makefile for Local Development

For local deployment and testing, you can use the following targets in the Makefile:

.PHONY: test-local
test-local: ## Run local tests for development.
    @echo ">> running local tests"
    go test ./... 

Monitoring and Debugging

When running in production, it’s crucial to monitor the Thanos components and their resource usage. Utilize the built-in metrics provided by Thanos to monitor its performance. You can access these metrics via Prometheus or any metrics collector of your choice.

Conclusion

This guide provides the steps necessary to deploy Thanos into a production environment, emphasizing Docker usage and Kubernetes deployment options. Utilize the provided code snippets and configurations to tailor the deployment to your specific infrastructure requirements.

For additional details or to modify configurations, refer to the official documentation and the source code available at github.com/thanos-io/thanos.


Sources:
The content cited herein originates from the official Thanos documentation and associated files from the Thanos GitHub repository, specifically the relevant .yaml, .dockerfile, and .makefile resources required for successful deployment.