4.5. 複数の namespace 内のスクレイプターゲット


複数の namespace でターゲットをスクレイプするには、MonitoringStack オブジェクトに namespace とリソースセレクターを設定します。

前提条件

  • cluster-admin クラスターロールを持つユーザーとして、または namespace の管理権限を持つユーザーとして、クラスターにアクセスできる。
  • Cluster Observability Operator がインストールされている。

手順

  1. 以下の namespace オブジェクトと MonitoringStack YAML ファイルをデプロイします。

    MonitoringStack の例

    apiVersion: v1
    kind: Namespace
    metadata:
      name: ns1-coo
      labels:
        monitoring.rhobs/stack: multi-ns
    ---
    apiVersion: monitoring.rhobs/v1alpha1
    kind: MonitoringStack
    metadata:
      name: example-coo-monitoring-stack
      namespace: ns1-coo
    spec:
      logLevel: debug
      retention: 1d
      resourceSelector:
          matchLabels:
            k8s-app: prometheus-coo-example-monitor
      namespaceSelector:
          matchLabels:
            monitoring.rhobs/stack: multi-ns
    Copy to Clipboard Toggle word wrap

  2. アラートは常に実行中の状態で、namespace ns1-coo にサンプルアプリケーションをデプロイします。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-coo-example-app
      name: prometheus-coo-example-app
      namespace: ns1-coo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-coo-example-app
      template:
        metadata:
          labels:
            app: prometheus-coo-example-app
        spec:
          containers:
          - image: ghcr.io/rhobs/prometheus-example-app:0.4.2
            imagePullPolicy: IfNotPresent
            name: prometheus-coo-example-app
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: prometheus-coo-example-app
      name: prometheus-coo-example-app
      namespace: ns1-coo
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
        name: web
      selector:
        app: prometheus-coo-example-app
      type: ClusterIP
    ---
    apiVersion: monitoring.rhobs/v1
    kind: ServiceMonitor
    metadata:
      labels:
        k8s-app: prometheus-coo-example-monitor
      name: prometheus-coo-example-monitor
      namespace: ns1-coo
    spec:
      endpoints:
      - interval: 30s
        port: web
        scheme: http
      selector:
        matchLabels:
          app: prometheus-coo-example-app
    ---
    apiVersion: monitoring.rhobs/v1
    kind: PrometheusRule
    metadata:
      name: example-alert
      namespace: ns1-coo
      labels:
        k8s-app: prometheus-coo-example-monitor
    spec:
      groups:
      - name: example
        rules:
        - alert: VersionAlert
          for: 1m
          expr: version{job="prometheus-coo-example-app"} > 0
          labels:
            severity: warning
    Copy to Clipboard Toggle word wrap
  3. monitoring.rhobs/stack: multi-ns のラベルが付けられた別の namespace に、同じサンプルアプリケーションをデプロイします。

    apiVersion: v1
    kind: Namespace
    metadata:
      name: ns2-coo
      labels:
        monitoring.rhobs/stack: multi-ns
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-coo-example-app
      name: prometheus-coo-example-app
      namespace: ns2-coo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-coo-example-app
      template:
        metadata:
          labels:
            app: prometheus-coo-example-app
        spec:
          containers:
          - image: ghcr.io/rhobs/prometheus-example-app:0.4.2
            imagePullPolicy: IfNotPresent
            name: prometheus-coo-example-app
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: prometheus-coo-example-app
      name: prometheus-coo-example-app
      namespace: ns2-coo
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
        name: web
      selector:
        app: prometheus-coo-example-app
      type: ClusterIP
    ---
    apiVersion: monitoring.rhobs/v1
    kind: ServiceMonitor
    metadata:
      labels:
        k8s-app: prometheus-coo-example-monitor
      name: prometheus-coo-example-monitor
      namespace: ns2-coo
    spec:
      endpoints:
      - interval: 30s
        port: web
        scheme: http
      selector:
        matchLabels:
          app: prometheus-coo-example-app
    Copy to Clipboard Toggle word wrap

検証

  1. Prometheus インスタンスが新規ターゲットを追加し、アラートが実行中であることを確認します。port-forward コマンドを使用して、Prometheus または Monitoringstack インスタンスによってデプロイされた Alertmanager ユーザーインターフェイスを公開します。

    Prometheus

    $ oc port-forward -n ns1-coo pod/prometheus-example-coo-monitoring-stack-0 9090
    Copy to Clipboard Toggle word wrap

    Alertmanager

    $ oc port-forward -n ns1-coo pod/alertmanager-example-coo-monitoring-stack-0 9093
    Copy to Clipboard Toggle word wrap

  2. http://localhost:9090/targets または http://localhost:9093/#/alerts を参照して、ターゲットがスクレイピングされ、アラートが実行されていることを確認します。
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat