8.2. ユーザー定義プロジェクトのメトリクスコレクションの設定
ServiceMonitor リソースを作成して、ユーザー定義プロジェクトのサービスエンドポイントからメトリクスを収集できます。これは、アプリケーションが Prometheus クライアントライブラリーを使用してメトリクスを /metrics の正規の名前に公開していることを前提としています。
このセクションでは、ユーザー定義のプロジェクトでサンプルサービスをデプロイし、次にサービスのモニター方法を定義する ServiceMonitor リソースを作成する方法を説明します。
8.2.1. サンプルサービスのデプロイ リンクのコピーリンクがクリップボードにコピーされました!
ユーザー定義のプロジェクトでサービスのモニタリングをテストするには、サンプルサービスをデプロイできます。
前提条件
-
cluster-adminクラスターロールを持つユーザーとして、または namespace の管理権限を持つユーザーとして、クラスターにアクセスできる。
手順
-
サービス設定の YAML ファイルを作成します。この例では、
prometheus-example-app.yamlという名前です。 以下のデプロイメントおよびサービス設定の詳細をファイルに追加します。
apiVersion: v1 kind: Namespace metadata: name: ns1 --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: prometheus-example-app name: prometheus-example-app namespace: ns1 spec: replicas: 1 selector: matchLabels: app: prometheus-example-app template: metadata: labels: app: prometheus-example-app spec: containers: - image: ghcr.io/rhobs/prometheus-example-app:0.4.2 imagePullPolicy: IfNotPresent name: prometheus-example-app --- apiVersion: v1 kind: Service metadata: labels: app: prometheus-example-app name: prometheus-example-app namespace: ns1 spec: ports: - port: 8080 protocol: TCP targetPort: 8080 name: web selector: app: prometheus-example-app type: ClusterIPこの設定は、
prometheus-example-appという名前のサービスをユーザー定義のns1プロジェクトにデプロイします。このサービスは、カスタムversionメトリクスを公開します。設定をクラスターに適用します。
$ oc apply -f prometheus-example-app.yamlサービスをデプロイするには多少時間がかかります。
Pod が実行中であることを確認できます。
$ oc -n ns1 get pod出力例
NAME READY STATUS RESTARTS AGE prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m
8.2.2. サービスのモニター方法の指定 リンクのコピーリンクがクリップボードにコピーされました!
サービスが公開するメトリクスを使用するには、OpenShift Container モニタリングを、/metrics エンドポイントからメトリクスを収集できるように設定する必要があります。これは、サービスのモニタリング方法を指定する ServiceMonitor カスタムリソース定義、または Pod のモニタリング方法を指定する PodMonitor CRD を使用して実行できます。前者の場合は Service オブジェクトが必要ですが、後者の場合は不要です。これにより、Prometheus は Pod によって公開されるメトリクスエンドポイントからメトリクスを直接収集することができます。
この手順では、ユーザー定義プロジェクトでサービスの ServiceMonitor リソースを作成する方法を説明します。
前提条件
-
cluster-adminクラスターロールまたはmonitoring-editクラスターロールのあるユーザーとしてクラスターにアクセスできる。 - ユーザー定義プロジェクトのモニタリングが有効化されている。
この例では、
prometheus-example-appサンプルサービスをns1プロジェクトにデプロイしている。注記prometheus-example-appサンプルサービスは TLS 認証をサポートしません。
手順
-
example-app-service-monitor.yamlという名前の新しい YAML 設定ファイルを作成します。 ServiceMonitorリソースを YAML ファイルに追加します。以下の例では、prometheus-example-monitorという名前のサービスモニターを作成し、ns1namespace のprometheus-example-appサービスによって公開されるメトリクスを収集します。apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: prometheus-example-monitor namespace: ns11 spec: endpoints: - interval: 30s port: web2 scheme: http selector:3 matchLabels: app: prometheus-example-app注記ユーザー定義の namespace の
ServiceMonitorリソースは、同じ namespace のサービスのみを検出できます。つまり、ServiceMonitorリソースのnamespaceSelectorフィールドは常に無視されます。設定をクラスターに適用します。
$ oc apply -f example-app-service-monitor.yamlServiceMonitorをデプロイするのに多少時間がかかります。ServiceMonitorリソースが実行されていることを確認します。$ oc -n <namespace> get servicemonitor出力例
NAME AGE prometheus-example-monitor 81m
8.2.3. サービスエンドポイント認証設定の例 リンクのコピーリンクがクリップボードにコピーされました!
ServiceMonitor および PodMonitor カスタムリソース定義 (CRD) を使用して、ユーザー定義のプロジェクト監視用のサービスエンドポイントの認証を設定できます。
次のサンプルは、ServiceMonitor リソースのさまざまな認証設定を示しています。各サンプルでは、認証認証情報やその他の関連設定を含む対応する Secret オブジェクトを設定する方法を示します。
8.2.3.1. ベアラートークンを使用した YAML 認証の例 リンクのコピーリンクがクリップボードにコピーされました!
以下の例は、ns1 namespace の example-bearer-auth という名前の Secret オブジェクトのベアラートークン設定を示しています。
ベアラートークンシークレットの例
apiVersion: v1
kind: Secret
metadata:
name: example-bearer-auth
namespace: ns1
stringData:
token: <authentication_token>
- 1
- 認証トークンを指定します。
以下の例は、ServiceMonitor CRD のベアラートークン認証設定を示しています。この例では、example-bearer-auth という名前の Secret オブジェクトを使用しています。
ベアラートークンの認証設定の例
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-example-monitor
namespace: ns1
spec:
endpoints:
- authorization:
credentials:
key: token
name: example-bearer-auth
port: web
selector:
matchLabels:
app: prometheus-example-app
bearerTokenFile を使用してベアラートークンを設定しないでください。bearerTokenFile 設定を使用する場合、ServiceMonitor リソースは拒否されます。
8.2.3.2. Basic 認証用のサンプル YAML リンクのコピーリンクがクリップボードにコピーされました!
次のサンプルは、ns1 の example-basic-auth という名前の Secret オブジェクトの Basic 認証設定を示しています。
Basic 認証シークレットの例
apiVersion: v1
kind: Secret
metadata:
name: example-basic-auth
namespace: ns1
stringData:
user: <basic_username>
password: <basic_password>
以下の例は、ServiceMonitor CRD の Basic 認証設定を示しています。この例では、example-basic-auth という名前の Secret オブジェクトを使用しています。
Basic 認証の設定例
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-example-monitor
namespace: ns1
spec:
endpoints:
- basicAuth:
username:
key: user
name: example-basic-auth
password:
key: password
name: example-basic-auth
port: web
selector:
matchLabels:
app: prometheus-example-app
8.2.3.3. OAuth 2.0 を使用した YAML 認証のサンプル リンクのコピーリンクがクリップボードにコピーされました!
以下の例は、ns1 namespace の example-oauth2 という名前の Secret オブジェクトの OAuth 2.0 設定を示しています。
OAuth 2.0 シークレットの例
apiVersion: v1
kind: Secret
metadata:
name: example-oauth2
namespace: ns1
stringData:
id: <oauth2_id>
secret: <oauth2_secret>
以下の例は、ServiceMonitor CRD の OAuth 2.0 認証設定を示しています。この例では、example-oauth2 という名前の Secret オブジェクトを使用します。
OAuth 2.0 認証の設定例
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-example-monitor
namespace: ns1
spec:
endpoints:
- oauth2:
clientId:
secret:
key: id
name: example-oauth2
clientSecret:
key: secret
name: example-oauth2
tokenUrl: https://example.com/oauth2/token
port: web
selector:
matchLabels:
app: prometheus-example-app