github.com/thanos-io/thanos@v0.32.5/scripts/quickstart.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # Starts three Prometheus servers scraping themselves and sidecars for each.
     4  # Two query nodes are started and all are clustered together.
     5  
     6  trap 'kill 0' SIGTERM
     7  
     8  MINIO_ENABLED=${MINIO_ENABLED:-""}
     9  MINIO_EXECUTABLE=${MINIO_EXECUTABLE:-"minio"}
    10  MC_EXECUTABLE=${MC_EXECUTABLE:-"mc"}
    11  PROMETHEUS_EXECUTABLE=${PROMETHEUS_EXECUTABLE:-"prometheus"}
    12  THANOS_EXECUTABLE=${THANOS_EXECUTABLE:-"thanos"}
    13  S3_ENDPOINT=""
    14  
    15  if [ ! $(command -v "$PROMETHEUS_EXECUTABLE") ]; then
    16    echo "Cannot find or execute Prometheus binary $PROMETHEUS_EXECUTABLE, you can override it by setting the PROMETHEUS_EXECUTABLE env variable"
    17    exit 1
    18  fi
    19  
    20  if [ ! $(command -v "$THANOS_EXECUTABLE") ]; then
    21    echo "Cannot find or execute Thanos binary $THANOS_EXECUTABLE, you can override it by setting the THANOS_EXECUTABLE env variable"
    22    exit 1
    23  fi
    24  
    25  # Start local object storage, if desired.
    26  # NOTE: If you would like to use an actual S3-compatible API with this setup
    27  #       set the S3_* environment variables set in the Minio example.
    28  if [ -n "${MINIO_ENABLED}" ]; then
    29    if [ ! $(command -v "$MINIO_EXECUTABLE") ]; then
    30      echo "Cannot find or execute Minio binary $MINIO_EXECUTABLE, you can override it by setting the MINIO_EXECUTABLE env variable"
    31      exit 1
    32    fi
    33  
    34    if [ ! $(command -v "$MC_EXECUTABLE") ]; then
    35      echo "Cannot find or execute Minio client binary $MC_EXECUTABLE, you can override it by setting the MC_EXECUTABLE env variable"
    36      exit 1
    37    fi
    38  
    39    export MINIO_ROOT_USER="THANOS"
    40    export MINIO_ROOT_PASSWORD="ITSTHANOSTIME"
    41    export MINIO_ENDPOINT="127.0.0.1:9000"
    42    export MINIO_BUCKET="thanos"
    43    export S3_ACCESS_KEY=${MINIO_ROOT_USER}
    44    export S3_SECRET_KEY=${MINIO_ROOT_USER}
    45    export S3_BUCKET=${MINIO_BUCKET}
    46    export S3_ENDPOINT=${MINIO_ENDPOINT}
    47    export S3_INSECURE="true"
    48    export S3_V2_SIGNATURE="true"
    49    mkdir -p data/minio
    50  
    51    ${MINIO_EXECUTABLE} server ./data/minio \
    52      --address ${MINIO_ENDPOINT} &
    53    sleep 3
    54    # create the bucket
    55    ${MC_EXECUTABLE} config host add tmp http://${MINIO_ENDPOINT} ${MINIO_ACCESS_KEY} ${MINIO_SECRET_KEY}
    56    ${MC_EXECUTABLE} mb tmp/${MINIO_BUCKET}
    57    ${MC_EXECUTABLE} config host rm tmp
    58  
    59    cat <<EOF >data/bucket.yml
    60  type: S3
    61  config:
    62    bucket: $S3_BUCKET
    63    endpoint: $S3_ENDPOINT
    64    insecure: $S3_INSECURE
    65    signature_version2: $S3_V2_SIGNATURE
    66    access_key: $S3_ACCESS_KEY
    67    secret_key: $S3_SECRET_KEY
    68  EOF
    69  fi
    70  
    71  # Setup alert / rules config file.
    72  cat >data/rules.yml <<-EOF
    73  	groups:
    74  	  - name: example
    75  	    rules:
    76  	    - record: job:go_threads:sum
    77  	      expr: sum(go_threads) by (job)
    78  EOF
    79  
    80  STORES=""
    81  
    82  # Start three Prometheus servers monitoring themselves.
    83  for i in $(seq 0 2); do
    84    rm -rf data/prom"${i}"
    85    mkdir -p data/prom"${i}"/
    86  
    87    cat >data/prom"${i}"/prometheus.yml <<-EOF
    88  		global:
    89  		  external_labels:
    90  		    prometheus: prom-${i}
    91  		rule_files:
    92  		  - 'rules.yml'
    93  		scrape_configs:
    94  		- job_name: prometheus
    95  		  scrape_interval: 5s
    96  		  static_configs:
    97  		  - targets:
    98  		    - "localhost:909${i}"
    99  		    - "localhost:5909${i}"
   100  		    - "localhost:5909${i}"
   101  		    - "localhost:5909${i}"
   102  		- job_name: thanos-sidecar
   103  		  scrape_interval: 5s
   104  		  static_configs:
   105  		  - targets:
   106  		    - "localhost:109${i}2"
   107  		- job_name: thanos-store
   108  		  scrape_interval: 5s
   109  		  static_configs:
   110  		  - targets:
   111  		    - "localhost:10906"
   112  		- job_name: thanos-receive
   113  		  scrape_interval: 5s
   114  		  static_configs:
   115  		  - targets:
   116  		    - "localhost:10909"
   117  		    - "localhost:11909"
   118  		    - "localhost:12909"
   119  		- job_name: thanos-query
   120  		  scrape_interval: 5s
   121  		  static_configs:
   122  		  - targets:
   123  		    - "localhost:10904"
   124  		    - "localhost:10914"
   125  	EOF
   126  
   127    cp data/rules.yml data/prom${i}/rules.yml
   128  
   129    ${PROMETHEUS_EXECUTABLE} \
   130      --config.file data/prom"${i}"/prometheus.yml \
   131      --storage.tsdb.path data/prom"${i}" \
   132      --log.level warn \
   133      --web.enable-lifecycle \
   134      --storage.tsdb.min-block-duration=2h \
   135      --storage.tsdb.max-block-duration=2h \
   136      --web.listen-address 0.0.0.0:909"${i}" &
   137  
   138    sleep 0.25
   139  done
   140  
   141  sleep 0.5
   142  
   143  OBJSTORECFG=""
   144  if [ -n "${MINIO_ENABLED}" ]; then
   145    OBJSTORECFG="--objstore.config-file      data/bucket.yml"
   146  fi
   147  
   148  # Start one sidecar for each Prometheus server.
   149  for i in $(seq 0 2); do
   150    if [ -z ${CODESPACE_NAME+x} ]; then
   151      PROMETHEUS_URL="http://localhost:909${i}"
   152    else
   153      PROMETHEUS_URL="https://${CODESPACE_NAME}-909${i}.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
   154    fi
   155    ${THANOS_EXECUTABLE} sidecar \
   156      --debug.name sidecar-"${i}" \
   157      --log.level debug \
   158      --grpc-address 0.0.0.0:109"${i}"1 \
   159      --grpc-grace-period 1s \
   160      --http-address 0.0.0.0:109"${i}"2 \
   161      --http-grace-period 1s \
   162      --prometheus.url "${PROMETHEUS_URL}" \
   163      --tsdb.path data/prom"${i}" \
   164      ${OBJSTORECFG} &
   165  
   166    STORES="${STORES} --store 127.0.0.1:109${i}1"
   167  
   168    sleep 0.25
   169  done
   170  
   171  sleep 0.5
   172  
   173  if [ -n "${GCS_BUCKET}" -o -n "${S3_ENDPOINT}" ]; then
   174    cat >groupcache.yml <<-EOF
   175  		type: GROUPCACHE
   176  config:
   177    self_url: http://localhost:10906
   178    peers:
   179      - http://localhost:10906
   180    groupcache_group: groupcache_test_group
   181  blocks_iter_ttl: 0s
   182  metafile_exists_ttl: 0s
   183  metafile_doesnt_exist_ttl: 0s
   184  metafile_content_ttl: 0s
   185  	EOF
   186  
   187    ${THANOS_EXECUTABLE} store \
   188      --debug.name store \
   189      --log.level debug \
   190      --grpc-address 0.0.0.0:10905 \
   191      --grpc-grace-period 1s \
   192      --http-address 0.0.0.0:10906 \
   193      --http-grace-period 1s \
   194      --data-dir data/store \
   195      --store.caching-bucket.config-file=groupcache.yml \
   196      ${OBJSTORECFG} &
   197  
   198    STORES="${STORES} --store 127.0.0.1:10905"
   199  fi
   200  
   201  sleep 0.5
   202  
   203  if [ -n "${REMOTE_WRITE_ENABLED}" ]; then
   204  
   205    for i in $(seq 0 1 2); do
   206      ${THANOS_EXECUTABLE} receive \
   207        --debug.name receive${i} \
   208        --log.level debug \
   209        --tsdb.path "./data/remote-write-receive-${i}-data" \
   210        --grpc-address 0.0.0.0:1${i}907 \
   211        --grpc-grace-period 1s \
   212        --http-address 0.0.0.0:1${i}909 \
   213        --http-grace-period 1s \
   214        --receive.replication-factor 1 \
   215        --tsdb.min-block-duration 5m \
   216        --tsdb.max-block-duration 5m \
   217        --label "receive_replica=\"${i}\"" \
   218        --label 'receive="true"' \
   219        --receive.local-endpoint 127.0.0.1:1${i}907 \
   220        --receive.hashrings '[{"endpoints":["127.0.0.1:10907","127.0.0.1:11907","127.0.0.1:12907"]}]' \
   221        --remote-write.address 0.0.0.0:1${i}908 \
   222        ${OBJSTORECFG} &
   223  
   224      STORES="${STORES} --store 127.0.0.1:1${i}907"
   225    done
   226  
   227    for i in $(seq 0 1 2); do
   228      mkdir -p "data/local-prometheus-${i}-data/"
   229      cat <<EOF >data/local-prometheus-${i}-data/prometheus.yml
   230  global:
   231    external_labels:
   232      prometheus: prom${i}
   233      replica: 1
   234  # When the Thanos remote-write-receive component is started,
   235  # this is an example configuration of a Prometheus server that
   236  # would scrape a local node-exporter and replicate its data to
   237  # the remote write endpoint.
   238  scrape_configs:
   239    - job_name: test
   240      scrape_interval: 1s
   241      static_configs:
   242      - targets:
   243          - fake
   244  remote_write:
   245  - url: http://localhost:1${i}908/api/v1/receive
   246  EOF
   247      ${PROMETHEUS_EXECUTABLE} \
   248        --web.listen-address ":5909${i}" \
   249        --config.file data/local-prometheus-${i}-data/prometheus.yml \
   250        --storage.tsdb.path "data/local-prometheus-${i}-data/" &
   251    done
   252  fi
   253  
   254  sleep 0.5
   255  
   256  QUERIER_JAEGER_CONFIG=$(
   257    cat <<-EOF
   258  		type: JAEGER
   259  		config:
   260  		  service_name: thanos-query
   261  		  sampler_type: ratelimiting
   262  		  sampler_param: 2
   263  	EOF
   264  )
   265  
   266  REMOTE_WRITE_FLAGS=""
   267  if [ -n "${STATELESS_RULER_ENABLED}" ]; then
   268    cat >data/rule-remote-write.yaml <<-EOF
   269    remote_write:
   270    - url: "http://localhost:10908/api/v1/receive"
   271      name: "receive-0"
   272  EOF
   273  
   274    REMOTE_WRITE_FLAGS="--remote-write.config-file=data/rule-remote-write.yaml"
   275  fi
   276  
   277  # Start Thanos Ruler.
   278  ${THANOS_EXECUTABLE} rule \
   279    --data-dir data/ \
   280    --eval-interval "30s" \
   281    --rule-file "data/rules.yml" \
   282    --alert.query-url "http://0.0.0.0:9090" \
   283    --query "http://0.0.0.0:10904" \
   284    --query "http://0.0.0.0:10914" \
   285    --http-address="0.0.0.0:19999" \
   286    --grpc-address="0.0.0.0:19998" \
   287    --label 'rule="true"' \
   288    "${REMOTE_WRITE_FLAGS}" \
   289    ${OBJSTORECFG} &
   290  
   291  STORES="${STORES} --store 127.0.0.1:19998"
   292  
   293  # Start two query nodes.
   294  for i in $(seq 0 1); do
   295    ${THANOS_EXECUTABLE} query \
   296      --debug.name query-"${i}" \
   297      --log.level debug \
   298      --grpc-address 0.0.0.0:109"${i}"3 \
   299      --grpc-grace-period 1s \
   300      --http-address 0.0.0.0:109"${i}"4 \
   301      --http-grace-period 1s \
   302      --query.replica-label prometheus \
   303      --tracing.config="${QUERIER_JAEGER_CONFIG}" \
   304      --query.replica-label receive_replica \
   305      ${STORES} &
   306  done
   307  
   308  sleep 0.5
   309  
   310  if [ -n "${GCS_BUCKET}" -o -n "${S3_ENDPOINT}" ]; then
   311    ${THANOS_EXECUTABLE} tools bucket web \
   312      --debug.name bucket-web \
   313      --log.level debug \
   314      --http-address 0.0.0.0:10933 \
   315      --http-grace-period 1s \
   316      ${OBJSTORECFG} &
   317  fi
   318  
   319  sleep 0.5
   320  
   321  echo "all started; waiting for signal"
   322  
   323  wait