はじめに
よくGCPに触れるものです。
Kubernetesのメトリクス監視によく用いられることで頻繁に耳にするPrometheusとGrafana。
Prometheusはコンテナからデータを取得して簡易的にグラフ化する機能があります。
ですがあくまで簡易的なものなので、監視用コンソールを作成できるGrafanaをあわせて用います。
現時点であまり触れたことがないので、GKE環境にPrometheusとGrafana環境を構築する方法を備忘録として書きたいと思います。
GKE環境構築
ここを1から始めるとなると大変なのでGCPのチュートリアルに用意されているものを使いましょう。
このチュートリアルでExposing the sample app to the internetの項目まで進めます。
ここで一つ注意点として、Deploymentを作成する際に--namespace test-app
とNamespaceを指定しておきましょう。
Namespaceは以下のようなコマンドで作成します。
$ kubectl create namespace test-app
ここで出てくる用語やKubernetes周りの用語は以下にまとめました。
用語 | 意味 |
---|---|
Node | クラスタで実行するコンテナを配置するサーバー |
Pod | コンテナの集合体。コンテナ実行方式を定義。 |
Namespace | クラスタ内で作る仮想的な空間 |
ReplicaSet | 同じ仕様のPodを複数生成・管理する |
Deployment | ReplicaSetの世代管理する |
Service | Podへのアクセス定義 |
Ingress | Serviceをクラスタ外に公開する |
チュートリアル通りにクラスタが作成でき、Podが正常に起動したらPrometheusとGrafanaの環境を構築しましょう。
Prometheus
まず監視リソースを作成するNamespaceを作成しましょう。
$ kubectl create namespace monitor
次に今回使うChartリポジトリをとってきましょう。
helm v3を使っています。
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
このリポジトリの中には様々なChartが含まれています。
試しにChart一覧を見てみましょう。
$ helm search repo prometheus NAME CHART VERSION APP VERSION DESCRIPTION prometheus-community/kube-prometheus-stack 12.12.0 0.44.0 kube-prometheus-stack collects Kubernetes manif... prometheus-community/prometheus 13.2.0 2.24.0 Prometheus is a monitoring system and time seri... prometheus-community/prometheus-adapter 2.10.1 v0.8.2 A Helm chart for k8s prometheus adapter prometheus-community/prometheus-blackbox-exporter 4.10.1 0.18.0 Prometheus Blackbox Exporter prometheus-community/prometheus-cloudwatch-expo... 0.12.1 0.8.0 A Helm chart for prometheus cloudwatch-exporter ...
今回使うChartはprometheus-community/prometheusです。
このChartから編集可能なパラメーターを取得します。
$ helm inspect values prometheus-community/prometheus > prometheus-value.yaml
ChartはService、Deployment、Ingressなどを構築するテンプレート郡をパッケージとしてまとめたものです。
その各パラメーターを編集するためにファイルに落とし込むコマンドがhelm inspect values
です。
今回は特にパラメーターを編集せずにPrometheusを立ち上げますが、以下のようにファイルも一緒に読み込むコマンドを実行します。
$ helm install --name-template prometheus --namespacemonitor -f prometheus-values.yaml prometheus-community/prometheus NAME: prometheus LAST DEPLOYED: Fri Jan 15 20:23:08 2021 NAMESPACE: monitor STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster: prometheus-server.monitor.svc.cluster.local Get the Prometheus server URL by running these commands in the same shell: export POD_NAME=$(kubectl get pods --namespace monitor -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace monitor port-forward $POD_NAME 9090 The Prometheus alertmanager can be accessed via port 80 on the following DNS name from within your cluster: prometheus-alertmanager.monitor.svc.cluster.local Get the Alertmanager URL by running these commands in the same shell: export POD_NAME=$(kubectl get pods --namespace monitor -l "app=prometheus,component=alertmanager" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace monitor port-forward $POD_NAME 9093 ################################################################################# ###### WARNING: Pod Security Policy has been moved to a global property. ##### ###### use .Values.podSecurityPolicy.enabled with pod-based ##### ###### annotations ##### ###### (e.g. .Values.nodeExporter.podSecurityPolicy.annotations) ##### ################################################################################# The Prometheus PushGateway can be accessed via port 9091 on the following DNS name from within your cluster: prometheus-pushgateway.monitor.svc.cluster.local Get the PushGateway URL by running these commands in the same shell: export POD_NAME=$(kubectl get pods --namespace monitor -l "app=prometheus,component=pushgateway" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace monitor port-forward $POD_NAME 9091 For more information on running Prometheus, visit: https://prometheus.io/
実際にPrometheusのPodが作成されたか確認をしましょう。
$ kubectl get pods --namespace monitor NAME READY STATUS RESTARTS AGE prometheus-alertmanager-69bbbcc5fd-mhkgd 2/2 Running 0 6m16s prometheus-kube-state-metrics-f9db4cbdf-6m85h 1/1 Running 0 6m16s prometheus-node-exporter-7bhnw 1/1 Running 0 6m17s prometheus-node-exporter-8sh6s 1/1 Running 0 6m17s prometheus-node-exporter-dfn7q 1/1 Running 0 6m17s prometheus-pushgateway-86d5954b97-csmm2 1/1 Running 0 6m16s prometheus-server-9d5694867-cf9jr
作成されたPrometheusをweb上で見てみましょう。
出力結果にもありますが、該当のPodを見つけ出し、ローカルからポートフォワーディングします。
$ kubectl get pods --namespace monitor -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}" $ kubectl --namespace monitor port-forward <pod name> 9090
ローカルホストに9090ポートでアクセスすると、
このような画面が見れればPrometheusは構築できました。
Grafana
Grafanaの構築方法もPrometheusと変わりません。
同じmonitorというNamespaceにリソースを作成します。
Grafanaのチャート一覧も見てみましょう。
$ helm repo add grafana https://grafana.github.io/helm-charts
$ helm search repo grafana NAME CHART VERSION APP VERSION DESCRIPTION grafana/grafana 6.1.17 7.3.5 The leading tool for querying and visualizing t... stable/grafana 5.5.7 7.1.1 DEPRECATED - The leading tool for querying and ... grafana/fluent-bit 2.2.0 v2.1.0 Uses fluent-bit Loki go plugin for gathering lo... grafana/loki 2.3.0 v2.1.0 Loki: like Prometheus, but for logs. grafana/loki-canary 0.2.0 2.1.0 Helm chart for Grafana Loki Canary ...
$ helm inspect values grafana/grafana >grafana-value.yaml
$ helm install --name-template grafana --namespace monitor -f grafana-values.yaml grafana/grafana NAME: grafana LAST DEPLOYED: Fri Jan 15 20:40:51 2021 NAMESPACE: monitor STATUS: deployed REVISION: 1 NOTES: 1. Get your 'admin' user password by running: kubectl get secret --namespace monitor grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo 2. The Grafana server can be accessed via port 80 on the following DNS name from within your cluster: grafana.monitor.svc.cluster.local Get the Grafana URL to visit by running these commands in the same shell: export POD_NAME=$(kubectl get pods --namespace monitor -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace monitor port-forward $POD_NAME 3000 3. Login with the password from step 1 and the username: admin ################################################################################# ###### WARNING: Persistence is disabled!!! You will lose your data when ##### ###### the Grafana pod is terminated. ##### #################################################################################
Podが作成されたか確認をしましょう。
$ kubectl get pods --namespace monitor NAME READY STATUS RESTARTS AGE grafana-648fc9d4d8-xqjpb 1/1 Running 0 51s
次にPrometheusと同じようにGrafanaもwebから見てみましょう。
先程の出力結果を参考に
$ kubectl get pods --namespace monitor -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}" $ kubectl --namespace monitor port-forward <pod name> 3000
ローカルホストに3000ポートでアクセスすると、
ここでIDとパスワードを要求されます。
IDはadmin で、パスワードは先程Grafanaをhelm installした際に取得方法が書かれています。
$ kubectl get secret --namespace monitor grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo xxxxxxxxxxxx
そのIDとパスワードを入力してログインします。
これでGrafanaの構築は完了しました。
Grafanaでメトリクスを見る
GrafanaはPrometheusで監視しているリソースの様々なメトリクスを作成できます。
そのメトリクスを組み合わせてオリジナルのダッシュボードを作成できます。
まずはData Sourceに作成したPrometheusを指定します。
HTTP URLに作成したPrometheusのサーバーを指定します。
次にDashboardを作成しましょう。
ダッシュボードに乗せるグラフを作成します。
Data Sourceを指定します。
その下のMetricsのクエリを記述する箇所があるので記述します。
今回はサンプルとしてPrometheusからクエリを参考にします。
まずPrometheusの画面からStatusを指定して、Targetを選択し、クラスター内のPodをターゲットにできているかを確認します。
確認ができたらGraphを指定してクエリを見ます。
出てきたクエリの中から適当なものをコピーします。
ちなみにPrometheusでもグラフは見れます。
Grafanaの画面に戻ってクエリをペーストします。
表示されたグラフが先程Prometheusで見れたものと同じであれば完了です。
おわりに
今回は簡単なPrometheusとGrafanaを構築してみました。
Chartのカスタマイズや、Prometheus、Grafanaの設定はやりこみ要素が多いので今度そちらについても触れればと思います。