LoginSignup
3
1

More than 3 years have passed since last update.

Thanos Operatorを使ってみる

Last updated at Posted at 2020-07-05

tldr

k8sのモニタリング環境をThanos Operatorを使って構築してみました。

環境

Kubernetes

1.16.10-gke.8

Helm

version.BuildInfo{Version:"v3.2.4", GitCommit:"0ad800ef43d3b826f31a5ad8dfbb4fe05d143688", GitTreeState:"clean", GoVersion:"go1.13.12"}

Thanosとは

公式ページを参照ください。

Thanos Operatorとは

Thanosのコンポーネント群をシンプルかつより安全に構築・運用するためにのサービスです。
Thanos Helm Chartを公開しているbanzai cloudが開発しています。

環境構築

Prometheus + Thanos sidecarはPrometheus Operatorを使って構築します。

Prometheus Operatorやk8sまわりのexporterをどんと用意する方法は

  • kube-prometheus
  • Prometheus Operator helm chart

がありますが、2つとも目標としていることはk8sのモニタリングの各コンポーネントを簡単に管理することです。雰囲気そんな感じです。
今回はPrometheus Operator helm chartを使用します。

クラスタ用意

省略。GKEを使います

モニタリング用のNamespaceを作成。

kubectl create namespace monitoring

Thanosのデプロイ

Thanosがメトリクスを永続化するGCS Bucket作成します。

gsutil mb -c multi_regional -l Asia gs://${PROJECT_ID}-thanos

Thanos用のサービスアカウントを作成してキーをダウンロードします。

export SERVICE_ACCOUNT_JSON_THANOS="serviceaccount-key-thanos.json"
gcloud iam service-accounts create thanos --display-name "Thanos"
export SERVICE_ACCOUNT_ID_THANOS=thanos@${PROJECT_ID}.iam.gserviceaccount.com

gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  --member serviceAccount:${SERVICE_ACCOUNT_ID_THANOS} \
  --role 'roles/storage.objectCreator'
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  --member serviceAccount:${SERVICE_ACCOUNT_ID_THANOS} \
  --role 'roles/storage.objectViewer'

gcloud iam service-accounts keys create ${SERVICE_ACCOUNT_JSON_THANOS} --iam-account=${SERVICE_ACCOUNT_ID_THANOS}

Bucketとサービスアカウントのキーが入ったSecretファイルを用意します。

type: GCS
config:
  bucket: "XXXX-thanos"
  service_account: |-
    {
      "type": "service_account",
      "project_id": "XXXX",
      "private_key_id": "XXX",
      "private_key": "-----BEGIN PRIVATE KEY-----\nXXX\n-----END PRIVATE KEY-----\n",
      "client_email": "thanos@XXXX.iam.gserviceaccount.com",
      "client_id": "XXXX",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/thanos@XXXX.iam.gserviceaccount.com"
    }

シークレットを作成

kubectl create secret generic thanos -n monitor --from-file=object-store.yaml=./kube/monitor/thanos/objectstore-secret.yaml

Thanos Operatorをデプロイ

helm install thanos-operator --namespace monitor banzaicloud-stable/thanos-operator --set manageCrds=false

Thanosクラスタのデプロイ

kubectl apply -n monitor -f ./kube/monitor/thanos/thanos.yaml
kubectl apply -n monitor -f ./kube/monitor/thanos/objectstore.yaml
kubectl apply -n monitor -f ./kube/monitor/thanos/storeendpoint.yaml

Prometheusのデプロイ

Thanosをサイドカーで追加します。

prometheus:
  prometheusSpec:
    thanos:
      image: quay.io/thanos/thanos:v0.12.2
      version: v0.12.2
      objectStorageConfig:
        name: thanos
        key: object-store.yaml
helm install prometheus-operator -n monitor stable/prometheus-operator -f ./kube/monitor/prometheus-operator/values.yaml

全部立ち上がったのを確認

NAME                                                       READY   STATUS    RESTARTS   AGE
alertmanager-prometheus-operator-alertmanager-0            2/2     Running   0          7m12s
objectstore-sample-bucket-855b8bc7fc-snzml                 1/1     Running   0          11m
objectstore-sample-compactor-6ff654c4b5-xgcmf              1/1     Running   0          11m
prometheus-operator-grafana-8589c4455b-rrktb               2/2     Running   0          7m25s
prometheus-operator-kube-state-metrics-66b4c95cd9-wh6s5    1/1     Running   0          7m25s
prometheus-operator-operator-5866d665cb-674q9              2/2     Running   0          7m25s
prometheus-operator-prometheus-node-exporter-gt5qm         1/1     Running   0          7m25s
prometheus-prometheus-operator-prometheus-0                4/4     Running   1          7m2s
thanos-operator-84b5b97494-7f56f                           1/1     Running   0          22m
thanos-sample-query-7b765646c-jpwcq                        1/1     Running   0          10m
thanos-sample-storeendpoint-sample-rule-0                  1/1     Running   0          10m
thanos-sample-storeendpoint-sample-store-9754c664f-tkq2x   1/1     Running   0          10m

QueryのUIにアクセスするとprometheusがちゃんと認識されており、クエリもできたました。

感想

構築自体は結構簡単でした。これでk8sのモニタリングクラスタを構築するのがだいぶ楽になった気がします。
実際に本番で試してみてどこまでシンプルな状態を保ったまま構築・運用できるか試してみたいです。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1