実戦: Prometheus+Grafana が Linux サーバーと Springboot プロジェクトを監視

序文

プロジェクトを運用環境に配信することが終了を意味するのではなく、オンライン サービスの運用と保守の監視を意味することは誰もが知っていると思います。運用保守監視には、主にデプロイメントサーバーのリソース状態、各サブサービスのリソース状態、ガベージコレクションやスループットなどのほか、障害アラームなどの機能が含まれます。もちろん、ブリックムーバーとして、フルリンクの運用および保守監視コンポーネントである Prometheus についても理解する必要があります。

知識の蓄積

プロメテウスとは

Prometheus は、オープンソースのサービス監視システムおよび時系列データベースです。
ここに画像の説明を挿入

特徴:
高次元データモデル
カスタムクエリ言語
ビジュアルデータ表示
効率的なストレージ戦略
簡単な運用と保守
多様なクライアント開発ライブラリの提供
警告とアラーム
データエクスポート

グラファナとは

Grafana は、クロスプラットフォームのオープンソース測定分析および視覚化ツールであり、主に収集されたデータのクエリと視覚化に使用されます。

ここに画像の説明を挿入

Grafana は、高速かつ柔軟なクライアント側グラフ、さまざまな視覚化インジケーターとログを備えたパネル プラグイン、ヒート マップ、折れ線グラフ、グラフなどの豊富なダッシュボード プラグインなど、豊富な視覚的プレゼンテーション方法を提供します。

Grafana を使用すると、ユーザーはダッシュボードのフロントエンドをすばやく表示および編集できます。複数のデータ ソースの同時接続をサポートし、時系列時空間データベース (TSDB) データを美しいグラフに変換できます。

データの収集と監視を完了する方法

Linux の主要なパラメータ情報はノード エクスポータを通じて収集され、Springboot マイクロサービス プロジェクトはアクチュエータ監視構成を提供します。Prometheus サーバーは、エクスポータによって収集されたサーバー データをアクティブに取得してメイン メモリと CPU パラメータを表示し、アクチュエータ インターフェイスをアクティブに呼び出して表示用のさまざまな動作パラメータを取得します。

運用保守担当者のさまざまな視覚化チャートの要件を満たすために、Grafana 測定プラットフォームと視覚分析ツールにアクセスし、そのさまざまなプラグインとダッシュボード テンプレートを使用して、監視運用と保守の品質を最大化できます。

環境構築

Prometheus+Grafana 環境の構築には、デプロイ管理に Docker コンテナ化を使用します。

アラームや監視項目などの設定を自由に変更できることを考慮して、GrafanaとPrometheusの主要な設定をコンテナの外にマウントします。

文件树:
[root@localhost アプリ]# ツリー
.
§── docker-compose-prometheus.yaml
っていつ── grafana
│ §── データ
│ │ └── grafana
│ └── grafana.ini
└── prometheus
§── app.json
└── prometheus.yml

docker と docker-compose のインストール

#安装docker社区版
yum install docker-ce
#版本查看
docker version
#docker-compose插件安装
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#可执行权限
chmod +x /usr/local/bin/docker-compose
#版本查看
docker-compose version

docker-compose 構成

[root@localhost アプリ]# pwd
/app
[root@localhost アプリ]#

1. Prometheus 構成ファイルを作成する

可参考 httpspro://metheus.io/docs/prometheus/latest/getting_started

[root@localhost app]# mkdir prometheus
[root@localhost app]# vim prometheus/app.json
[
    {
        "targets": [
            "10.10.18.16:8888"
        ],
        "labels": {
            "instance": "demo",
            "service": "demo-service"
        }
    }
]
[root@localhost app]# vim prometheus/prometheus.yml
global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m

scrape_configs:
  - job_name: spring-boot # springboot项目
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /actuator/prometheus
    scheme: http
    file_sd_configs:
      - files:
          - ./*.json
        refresh_interval: 1m

  - job_name: prometheus   # prometheus
    static_configs:
      - targets: ['prometheus:9090']
        labels:
          instance: prometheus


  - job_name: linux  # 采集node exporter监控数据,即linux
    static_configs:
      - targets: ['node-exporter:9100']
        labels:
          instance: localhost
          

2. Grafana 設定ファイルを作成する

[root@localhost app]# mkdir -p ./grafana/data/grafana
[root@localhost app]# chmod -R 777 ./grafana/

Grafana 設定ファイルのダウンロード:
https://github.com/grafana/grafana/blob/main/conf/sample.ini
ダウンロードした設定ファイルの名前を grafana.ini に変更し、新しく作成したフォルダーの下に置きます。

データベース構成を変更します。

[root@localhost app]# vim grafana.ini
#################################### Database ############################
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url property.

# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = 10.10.10.202:6456
name = grafana
user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
passw0ord =MyNewPass2021
# Use either URL or the previous fields to configure the database
# Example: mysql://user:secret@host:port/database
url =mysql://root:[email protected]:6456/grafana


# Max idle conn setting default is 2
max_idle_conn = 2

# Max conn setting default is 0 (mean not set)
max_open_conn =100

# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours)
conn_max_lifetime = 14400
# Set to true to log the sql calls and execution times.
log_queries =

# For "postgres", use either "disable", "require" or "verify-full"
# For "mysql", use either "true", "false", or "skip-verify".
ssl_mode = disable

# Database drivers may support different transaction isolation levels.
# Currently, only "mysql" driver supports isolation levels.
# If the value is empty - driver's default isolation level is applied.
# For "mysql" use "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ" or "SERIALIZABLE".
isolation_level =

ca_cert_path =
client_key_path =
client_cert_path =
server_cert_name =

# For "sqlite3" only, path relative to data_path setting
path = grafana.db

# For "sqlite3" only. cache mode setting used for connecting to the database
cache_mode = private

# For "sqlite3" only. Enable/disable Write-Ahead Logging, https://sqlite.org/wal.html. Default is false.
wal = false

# For "mysql" only if migrationLocking feature toggle is set. How many seconds to wait before failing to lock the database for the migrations, default is 0.
locking_attempt_timeout_sec = 0

# For "sqlite" only. How many times to retry query in case of database is locked failures. Default is 0 (disabled).
query_retries = 0

# For "sqlite" only. How many times to retry transaction in case of database is locked failures. Default is 5.
transaction_retries = 5

# Set to true to add metrics and tracing for database queries.
instrument_queries = false
#################################### SMTP / Emailing #####################
[smtp]
enabled = false
host = localhost:25
user =
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password =
cert_file =
key_file =
skip_verify = false
from_address = [email protected]
from_name = Grafana
ehlo_identity =
startTLS_policy =

[emails]
welcome_email_on_sign_up = false
templates_pattern = emails/*.html
content_types = text/html

3、docker-compose

[root@localhost app]# vim docker-compose-prometheus.yaml
version: "3"
networks: # 网桥
  prometheus:
    ipam:
      driver: default
      config:
        - subnet: "172.22.0.0/24"

services:
  prometheus: # prometheus
    image: registry.cn-hangzhou.aliyuncs.com/senfel/prometheus:v2.34.0
    container_name: prometheus
    restart: unless-stopped
    volumes:
      - ./prometheus/app.json:/etc/prometheus/app.json
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command: "--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus"
    ports:
      - "9090:9090"
    depends_on:
      - node-exporter
    networks:
      prometheus:
        ipv4_address: 172.22.0.11

  node-exporter:  # 采集服务器层面的运行指标
    image: registry.cn-hangzhou.aliyuncs.com/senfel/node-exporter:v1.3.1
    container_name: prometheus-node-exporter
    restart: unless-stopped
    volumes:
      - /proc:/host/proc"
      - /sys:/host/sys"
    ports:
      - "9100:9100"
    networks:
      prometheus:
        ipv4_address: 172.22.0.22

  grafana:  # 用于UI展示
    image: registry.cn-hangzhou.aliyuncs.com/senfel/grafana:8.0.0
    container_name: prometheus-grafana
    restart: unless-stopped
    ports:
      - "3000:3000"
    volumes:
      - ./grafana/data/grafana:/var/lib/grafana
      - /etc/localtime:/etc/localtime
      - ./grafana/plugins:/var/lib/grafana/plugins
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
    environment:
      GF_EXPLORE_ENABLED: "true"
      GF_SECURITY_ADMIN_PASSWORD: "admin"
      GF_INSTALL_PLUGINS: "grafana-clock-panel,grafana-simple-json-datasource,alexanderzobnin-zabbix-app"
    depends_on:
      - prometheus
    networks:
      prometheus:
        ipv4_address: 172.22.0.33
           

4. Prometheus+Grafana+Exporter コンテナをデプロイします

[root@localhost app]# docker-compose -f docker-compose-prometheus.yaml up -d

prometheus-node-exporter は最新です
prometheus を再作成中 … 完了
prometheus-grafana を再作成中 … 完了

[root@localhost app]# docker ps

ここに画像の説明を挿入

ブラウザアクセス http://10.10.22.91:3000/login
ここに画像の説明を挿入

監視構成

Grafana がプロメテウス データ ソースを構成する

1. [設定] - [データ ソース] をクリックしてデータ ソースを追加します。
ここに画像の説明を挿入

2. prometheusを選択し、パスを設定します

ここに画像の説明を挿入

Grafana 構成ダッシュボード

Spring Boot 2.1 統計:https://grafana.com/grafana/dashboards/10280
JVM (Micrometer):https://grafana.com/grafana/dashboards/4701
Linux ホスト メトリクス:https://grafana.com/grafana/ダッシュボード/10180-kds-linux-hosts/

Linuxホストメトリクスの監視

1. サイドバーをクリックしてインポートを追加します
ここに画像の説明を挿入

2. テンプレートID 10180を入力します
ここに画像の説明を挿入

3. 「ロード」をクリックしてロードします。
ここに画像の説明を挿入

4. 「インポート」をクリックしてインポートします。
ここに画像の説明を挿入ここに画像の説明を挿入

図に示すように、基本的にはサーバーの主要なパラメータの監視情報がカバーされています。

Spring Bootの監視

prometheus はプロジェクトのインジケーターを積極的に取得するため、モニタリング ポートを公開し、プロジェクトを変更する必要があります。

Spring Boot による監視構成の追加
1. pom 依存関係の導入

org.springframework.boot
spring-boot-starter-actuator


io.micrometer
micrometer-registry-prometheus

2. アプリケーション構成の追加

management:
  endpoints:
    web:
      exposure:
        include: health,prometheus

3. テスト監視情報
プロジェクト開始
ポストマンリクエスト 10.10.18.16:8888/actuator/prometheus
ここに画像の説明を挿入

Spring Boot 2.1 統計のインポート
1. サイドバーのインポート ボタンをクリックし、テンプレート ID 10280 を入力します。
ここに画像の説明を挿入

2. [ロード] をクリックしてダッシュボード テンプレートをロードします。
ここに画像の説明を挿入

3. 「インポート」をクリックしてインポートします。
ここに画像の説明を挿入ここに画像の説明を挿入

図に示すように、基本的にすべてのプロジェクト監視情報がカバーされます。

最後に書きます

この実践的なブログ投稿では、主に docker-compose を使用して Prometheus、node-exporter、Grafana を配置して監視環境構築を実現する方法について説明し、Linux システム サーバーのインジケーター監視について説明し、Springboot プロジェクトでフルリンク用のインジケーター インターフェイスを公開します。インジケーターデータのモニタリング。もちろん、Grafana が提供する Linux および Springboot 監視ダッシュボード テンプレートを使用して、運用環境に直接導入することもできます。

おすすめ

転載: blog.csdn.net/weixin_39970883/article/details/132057897