7.2. ユーザー定義プロジェクトのメトリクスコレクションの設定
ServiceMonitor
リソースを作成して、ユーザー定義プロジェクトのサービスエンドポイントからメトリクスを収集できます。これは、アプリケーションが Prometheus クライアントライブラリーを使用してメトリクスを /metrics
の正規の名前に公開していることを前提としています。
このセクションでは、ユーザー定義のプロジェクトでサンプルサービスをデプロイし、次にサービスのモニター方法を定義する ServiceMonitor
リソースを作成する方法を説明します。
7.2.1. サンプルサービスのデプロイ
ユーザー定義のプロジェクトでサービスのモニタリングをテストするには、サンプルサービスをデプロイできます。
前提条件
-
cluster-admin
クラスターロールを持つユーザーとして、または namespace の管理権限を持つユーザーとして、クラスターにアクセスできる。
手順
-
サービス設定の YAML ファイルを作成します。この例では、
prometheus-example-app.yaml
という名前です。 以下のデプロイメントおよびサービス設定の詳細をファイルに追加します。
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
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
メトリクスを公開します。設定をクラスターに適用します。
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc apply -f prometheus-example-app.yaml
$ oc apply -f prometheus-example-app.yaml
サービスをデプロイするには多少時間がかかります。
Pod が実行中であることを確認できます。
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc -n ns1 get pod
$ oc -n ns1 get pod
出力例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NAME READY STATUS RESTARTS AGE prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m
NAME READY STATUS RESTARTS AGE prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m
7.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
という名前のサービスモニターを作成し、ns1
namespace のprometheus-example-app
サービスによって公開されるメトリクスを収集します。Copy to Clipboard Copied! Toggle word wrap Toggle overflow apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: prometheus-example-monitor namespace: ns1 spec: endpoints: - interval: 30s port: web scheme: http selector: matchLabels: app: prometheus-example-app
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: prometheus-example-monitor namespace: ns1
1 spec: endpoints: - interval: 30s port: web
2 scheme: http selector:
3 matchLabels: app: prometheus-example-app
注記ユーザー定義の namespace の
ServiceMonitor
リソースは、同じ namespace のサービスのみを検出できます。つまり、ServiceMonitor
リソースのnamespaceSelector
フィールドは常に無視されます。設定をクラスターに適用します。
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc apply -f example-app-service-monitor.yaml
$ oc apply -f example-app-service-monitor.yaml
ServiceMonitor
をデプロイするのに多少時間がかかります。ServiceMonitor
リソースが実行されていることを確認します。Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc -n <namespace> get servicemonitor
$ oc -n <namespace> get servicemonitor
出力例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NAME AGE prometheus-example-monitor 81m
NAME AGE prometheus-example-monitor 81m
7.2.3. サービスエンドポイント認証設定の例
ServiceMonitor
および PodMonitor
カスタムリソース定義 (CRD) を使用して、ユーザー定義のプロジェクト監視用のサービスエンドポイントの認証を設定できます。
次のサンプルは、ServiceMonitor
リソースのさまざまな認証設定を示しています。各サンプルでは、認証認証情報やその他の関連設定を含む対応する Secret
オブジェクトを設定する方法を示します。
7.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>
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
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
リソースは拒否されます。
7.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>
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
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
7.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>
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
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