Varnish

このドキュメントでは、Google Cloud Managed Service for Prometheus を使用して Varnish から指標を収集できるように、Google Kubernetes Engine の Deployment を構成する方法について説明します。このドキュメントでは、次の方法について説明します。

  • Varnish のエクスポータを設定して、指標を報告する。
  • エクスポートされた指標を収集するために、Managed Service for Prometheus の PodMonitoring リソースを構成する。
  • Cloud Monitoring のダッシュボードにアクセスして指標を表示する。
  • 指標をモニタリングするようにアラートルールを構成する。

以下の手順は、Managed Service for Prometheus でマネージド コレクションを使用している場合にのみ適用されます。セルフデプロイ コレクションを使用している場合は、Varnish エクスポータのソース リポジトリでインストール情報をご覧ください。

以下の手順は一例であり、ほとんどの Kubernetes 環境で機能します。セキュリティ ポリシーや組織のポリシーの制限により、アプリケーションやエクスポータのインストールに問題がある場合は、オープンソース ドキュメントでサポートを確認することをおすすめします。

Varnish の詳細については、Varnish をご覧ください。

前提条件

Managed Service for Prometheus とマネージド コレクションを使用して Varnish から指標を収集するには、Deployment が次の要件を満たしている必要があります。

  • クラスタで Google Kubernetes Engine バージョン 1.21.4-gke.300 以降を実行している必要があります。
  • マネージド コレクションを有効にして、Managed Service for Prometheus を実行する必要があります。詳細については、マネージド コレクションを使ってみるをご覧ください。

  • Varnish とのインテグレーションに Cloud Monitoring で利用可能なダッシュボードを使用するには、prometheus_varnish_exporter バージョン 1.6.1 以降を使用する必要があります。

    利用可能なダッシュボードの詳細については、ダッシュボードを表示するをご覧ください。

Varnish エクスポータは、varnishstat コマンドの出力を取得します。これには、プロセス名前空間の共有と、Varnish コンテナの /var/lib/varnish ディレクトリへのアクセス権が必要です。

Varnish エクスポータはコンテナ イメージとして公開されていません。独自に作成する必要があります。次の例は、エクスポータと varnishstat コマンドを含むエクスポータ コンテナ イメージをビルドする方法を示しています。

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM debian:stable-slim as stage
WORKDIR /exporter
ADD https://github.com/jonnenauha/prometheus_varnish_exporter/releases/download/1.6.1/prometheus_varnish_exporter-1.6.1.linux-amd64.tar.gz /exporter/exporter.tar.gz
RUN tar -xvf exporter.tar.gz
RUN chmod +x /exporter/prometheus_varnish_exporter-1.6.1.linux-amd64/prometheus_varnish_exporter
FROM varnish:7.2.0
COPY --from=stage /exporter/prometheus_varnish_exporter-1.6.1.linux-amd64/prometheus_varnish_exporter /prometheus_varnish_exporter
ENTRYPOINT [ "/prometheus_varnish_exporter" ]

Varnish エクスポータをインストールする

Varnish ワークロードにサイドカーとして Varnish エクスポータ prometheus_varnish_exporter をインストールすることをおすすめします。サイドカーの使用方法については、マルチコンテナ Pod を使用した Kubernetes での拡張アプリケーションをご覧ください。

prometheus_varnish_exporter をサイドカーとして Varnish にインストールするには、次の例のように Varnish 構成を変更します。

# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: varnish
spec:
  selector:
    matchLabels:
+     app.kubernetes.io/name: varnish
  template:
    metadata:
      name: varnish
      labels:
+       app.kubernetes.io/name: varnish
    spec:
      containers:
      - name: varnish
        image: varnish:7.2.0
+       volumeMounts:
+         - name: shared-data
+           mountPath: /var/lib/varnish
+       env:
+       - name: VARNISH_HTTP_PORT
+         value: "8080"
+     - name: exporter
+       image: <custom-docker-image>
+       volumeMounts:
+         - name: shared-data
+           mountPath: /var/lib/varnish
+           readOnly: true
+       ports:
+       - containerPort: 9131
+         name: prometheus
+     volumes:
+       - name: shared-data
+         emptyDir: {}
+     shareProcessNamespace: true

+ 記号で始まるすべての行を構成に追加する必要があります。

構成の変更をローカル ファイルから適用するには、次のコマンドを実行します。

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

Terraform を使用して構成を管理することもできます。

PodMonitoring リソースを定義する

ターゲット ディスカバリの場合、Managed Service for Prometheus Operator には、同じ Namespace 内の Varnish エクスポータに対応する PodMonitoring リソースが必要です。

次の PodMonitoring 構成を使用できます。

# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: varnish
  labels:
    app.kubernetes.io/name: varnish
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  endpoints:
  - port: prometheus
    scheme: http
    interval: 30s
    path: /metrics
  selector:
    matchLabels:
      app.kubernetes.io/name: varnish

ラベルセレクタとポートが、Varnish エクスポータをインストールするで使用されているセレクタとポートと一致していることを確認します。

構成の変更をローカル ファイルから適用するには、次のコマンドを実行します。

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

Terraform を使用して構成を管理することもできます。

ルールとアラートを定義する

次の Rules 構成を使用して、Varnish 指標に関するアラートを定義できます。

# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: Rules
metadata:
  name: varnish-rules
  labels:
    app.kubernetes.io/component: rules
    app.kubernetes.io/name: varnish-rules
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  groups:
  - name: varnish
    interval: 30s
    rules:
    - alert: VarnishBackendConnectionFailure
      annotations:
        description: |-
          Varnish backend connection failure
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Varnish backend connection failure (instance {{ $labels.instance }})
      expr: varnish_backend_fail > 0
      for: 5m
      labels:
        severity: critical
    - alert: VarnishHighCacheEvictions
      annotations:
        description: |-
          Varnish high cache evictions
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Varnish high cache evictions (instance {{ $labels.instance }})
      expr: rate(varnish_main_n_obj_purged[5m]) >= 10
      for: 5m
      labels:
        severity: warning
    - alert: VarnishHighServerLimit
      annotations:
        description: |-
          Varnish high server limit
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Varnish high server limit (instance {{ $labels.instance }})
      expr: varnish_main_threads_failed > 0
      for: 5m
      labels:
        severity: critical
    - alert: VarnishSessionsDropped
      annotations:
        description: |-
          Varnish sessions dropped
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Varnish sessions dropped (instance {{ $labels.instance }})
      expr: varnish_main_sessions{type="dropped"} > 0
      for: 5m
      labels:
        severity: critical

構成の変更をローカル ファイルから適用するには、次のコマンドを実行します。

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

Terraform を使用して構成を管理することもできます。

ルールをクラスタに適用する方法については、マネージド ルールの評価とアラートをご覧ください。

アラートのしきい値は、アプリケーションに合わせて調整できます。

構成を確認する

Metrics Explorer を使用すると、Varnish エクスポータが正しく構成されていることを確認できます。Cloud Monitoring が指標を取り込むまでに 1~2 分かかる場合があります。

指標が取り込まれていることを確認します。

  1. Google Cloud コンソールのナビゲーション パネルで [Monitoring] を選択し、次に [ Metrics Explorer] を選択します。

    Metrics Explorer に移動

  2. クエリビルダー ペインのツールバーで、[MQL] または [PROMQL] という名前のボタンを選択します。
  3. [言語] で [PromQL] が選択されていることを確認します。言語切り替えボタンは、クエリの書式設定と同じツールバーにあります。
  4. 次のクエリを入力して実行します。
    up{job="varnish", cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

ダッシュボードを表示する

Cloud Monitoring インテグレーションには、Varnish Prometheus の概要ダッシュボードが含まれています。ダッシュボードは、インテグレーションを構成すると自動的にインストールされます。インテグレーションをインストールすることなく、ダッシュボードの静的プレビューを表示することもできます。

インストールされているダッシュボードを表示する手順は次のとおりです。

  1. Google Cloud コンソールのナビゲーション パネルで、[Monitoring] を選択してから、[ダッシュボード] を選択します。

    [ダッシュボード] に移動

  2. [ダッシュボード リスト] タブを選択します。
  3. [統合] カテゴリを選択します。
  4. ダッシュボードの名前(Varnish Prometheus Overview など)をクリックします。

ダッシュボードの静的プレビューを表示する手順は次のとおりです。

  1. Google Cloud コンソールのナビゲーション パネルで、[Monitoring] を選択してから、[インテグレーション] を選択します。

    [インテグレーション] に移動

  2. [デプロイメント プラットフォーム] フィルタの [Kubernetes Engine] をクリックします。
  3. Varnish インテグレーションを見つけ、[詳細を表示] をクリックします。
  4. [ダッシュボード] タブを選択します。

トラブルシューティング

指標の取り込みに関する問題のトラブルシューティングについては、取り込み側の問題のトラブルシューティングエクスポータからの収集に関する問題をご覧ください。