記事ディレクトリ
Linuxクラウドコンピューティングアーキテクチャ-prometheus + grafanaクラウドプラットフォーム監視システムを構築
1.プロメテウスの紹介
Prometheus(プロメテウス)は、監視、アラーム、および時系列データベースを組み合わせたオープンソースの監視システムです。ドーカー、メソ、kubernetesコンテナー管理システムで一般的に使用されます。
監視の原則:http
プロトコルを通じて定期的に監視対象コンポーネントのステータスを取得します。監視対象コンポーネントは、SDKやその他の統合プロセスなしで、対応するhttpインターフェイスを提供するだけで済みます。
監視対象コンポーネントがステータス情報を提供するhttpインターフェースは、と呼ばれexporter
ます。
現在、インターネット企業のコンポーネントのほとんどはexporter
直接使用できます。
nagiosとzabbixの監視インターフェイスと比較して、prometheusの監視インターフェイスはより美しいです。
2. Prometheusアーキテクチャの原則
プロメソッドのアーキテクチャは、プロメソッドの全体的なアーキテクチャとそのエコシステムコンポーネントの一部です。
2.1 Prometheusアーキテクチャの構成
prometheusサーバー:prometheusコンポーネントの中核部分であり、監視データの取得、保存、クエリを行います。監視対象は静的構成で管理することも、サービス発見(サービス発見)を設定して監視対象を動的に管理し、これらの対象からデータを取得することもできます。prometheusサーバーは、収集されたモニタリングデータをローカルディスクに時系列で格納するリアルタイムデータベースです。カスタマイズされたpromQLを提供して、データのクエリと分析を実現します。Prometheusサーバーは、他のPrometheusサーバーインスタンスからデータを取得することもできます。
エクスポーター:モニター対象コンポーネントは、データをモニターするためのインターフェースを提供します。そのため、モニター・データは、httpサービスの形式でPrometheusサーバーに表示されます。Prometheusサーバーは、エクスポーターが提供するエンドポイントポートにアクセスすることにより、監視データを取得できます。
exporter
一般に2つのカテゴリに分類されます
。①直接収集:プロメテウスをサポートし、監視対象コンポーネントの情報を直接収集します。②間接
収集:Prometheusは直接サポートされておらず、監視対象コンポーネントのデータは、Prometheusが提供する監視コンポーネントの監視収集プログラムを通じて収集されます。
alertmanager:Prometheusサーバーは、promQLに基づくアラートルールの作成をサポートしています。promQLで定義されたルールが満たされると、アラートが生成されます。一般的に受け入れられている方法は、電子メール、Webhookです。[zabbixのトリガーと同様]
Pushgateway:Prometheusデータ収集は、エクスポーターからデータをプル(プル)するPrometheusサーバーに基づいており、Prometheusとエクスポーター間の通信を維持する必要があります。2つの間の通信が許可されていない場合、pushgatewayを使用して転送することができます。つまり、監視対象コンポーネントがデータをpushgatewayにアクティブにプッシュし、pushgatewayがデータをpromethuesサーバーに送信します。
2.2 Prometheusアーキテクチャのワークフロー
-
promethues server
取得metrics
(インデックス)
①構成済みjobs
からexporter
プルしますmetrics
② プルから受け入れるpushgateway
ためにmetrics
③から送信された受け入れ可能です③promethues server
metrics
-
promethues server
処理metrics
①ローカルストレージmetrics
②定義済みのalerts.rules
実行③ルールをalertmanager
満たし、アラートをプッシュします。ルールを満たさず、新しい時系列のみを記録します。 -
alertmanager
アラームの処理①設定ファイルに従い、受信したアラームを処理し、アラームを発行します。
-
グラフィカルインターフェイスはデータを視覚化します。
3. promethuesクラウドプラットフォーム監視システムをデプロイする
prometheusクラウドプラットフォームモニタリングシステムのトポロジ図:
3.1インストールパッケージをダウンロードする
# 下载以下6个tar包,国内的网不一定下的到。
# 下载地址:https://prometheus.io/download/
# premetheus server和alertmanager下载
https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
https://github.com/prometheus/alertmanager/releases/download/v0.21.0/alertmanager-0.21.0.linux-amd64.tar.gz
# mysqld_exporter、node_exporter、pushgateway下载
https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
https://github.com/prometheus/pushgateway/releases/download/v1.2.0/pushgateway-1.2.0.linux-amd64.tar.gz
# grafana下载
https://dl.grafana.com/oss/release/grafana-7.1.3-1.x86_64.rpm
# 这个地址比较快
https://mirrors.huaweicloud.com/grafana/7.1.3/grafana-7.1.3.linux-amd64.tar.gz
[root@master prometheus]# pwd
/root/prometheus
[root@master prometheus]# ls
alertmanager-0.21.0.linux-amd64.tar.gz node_exporter-1.0.1.linux-amd64.tar.gz
grafana-7.1.3.linux-amd64.tar.gz prometheus-2.20.1.linux-amd64.tar.gz
mysqld_exporter-0.12.1.linux-amd64.tar.gz pushgateway-1.2.0.linux-amd64.tar.gz
3.2 Prometheusサーバーのデプロイ
# 安装epel扩展源
[root@master ~]# yum install epel-release -y
# 安装go语言环境
# go(golang)是goolge开发的一种静态强类型、编译型、并发型、具有垃圾回收功能的编程语言。
[root@master ~]# yum install go -y
[root@master ~]# go version
go version go1.13.14 linux/amd64
# 直接解压到/usr/local/目录下即可使用
[root@master ~]# tar xzf /root/prometheus/prometheus-2.20.1.linux-amd64.tar.gz -C /usr/local/
[root@master ~]# ll -d /usr/local/prometheus-2.20.1.linux-amd64/
drwxr-xr-x. 4 3434 3434 144 8月 6 03:42 /usr/local/prometheus-2.20.1.linux-amd64/
# 修改prometheus的配置文件,最后加即可。
# yml语法注重缩进
vim prometheus.yml
- job_name: system-status # 监控项,监控系统状态
static_configs:
- targets: ['192.168.8.178:9100'] # 监控主机IP和端口号
labels:
instance: server-status
- job_name: mysql-status # 监控mysql状态
static_configs:
- targets: ['192.168.8.178:9104'] #监控主机IP和端口号
labels:
instance: server-mysql # 实例名,用于grafana
# 启动prometheus,并放到后台运行
[root@master ~]# cd /usr/local/prometheus-2.20.1.linux-amd64/
[root@master prometheus-2.20.1.linux-amd64]# ./prometheus --config.file=prometheus.yml &
===========================================
& 前台进程放到后台运行
jobs 查看当前的后台进程
fg 后台序号 后台进程放到前台运行
ctrl+z 前台进程放到后台运行,并停止
bg 后台进程继续运行
ctrl+c 退出前台进程
===========================================
# 如果不用&,表示前台运行。
# 看到下面这句就说明启动成功了
level=info ts=2020-08-17T14:17:25.259Z caller=main.go:652 msg="Server is ready to receive web requests."
# 按了ctrl+c退出,即关闭了。
level=info ts=2020-08-17T14:21:12.348Z caller=main.go:767 msg="See you next time!"
# 由于prometheus服务器占用9090号端口,故防火墙要开放9090号端口
[root@master ~]# firewall-cmd --permanent --zone=public --add-port=9090/tcp
success
[root@master ~]# firewall-cmd --reload
success
# 查看prometheus的web界面
http://192.168.8.177:9090/
nodeとmysqlのエクスポーターが設定されていないため、データを取得できません。
[root@master prometheus-2.20.1.linux-amd64]# cat prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: system-status # 监控项,监控系统状态
static_configs:
- targets: ['192.168.8.178:9100'] # 监控主机IP和端口号
labels:
instance: server-status
- job_name: mysql-status # 监控mysql状态
static_configs:
- targets: ['192.168.8.178:9104'] #监控主机IP和端口号
labels:
instance: server-mysql # 实例名,用于grafana
# 监控多个IP地址
['192.168.8.100:9100','192.168.8.101:9100']
3.3エクスポーターの展開(エクスポートノード)
一般的な輸出業者:
① node_exporter
監視システムのパフォーマンスと運用状況
② mysqld_exporter
mysqlデータベースサービス監視
③ snmp_exporter
監視ネットワークデバイスを
# 在被监控主机上配置以下2个exporter出口节点,即接口。用于采集被监控主机上对应的状态。
# 传输tar包
[root@master prometheus]# scp /root/prometheus/node_exporter-1.0.1.linux-amd64.tar.gz 192.168.8.178:/opt/
[root@master prometheus]# scp /root/prometheus/mysqld_exporter-0.12.1.linux-amd64.tar.gz 192.168.8.178:/opt/
1. 配置node_exporter
[root@client ~]# tar xzf /opt/node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local/
[root@client ~]# /usr/local/node_exporter-1.0.1.linux-amd64/node_exporter &
[root@client ~]# netstat -antup | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 10098/node_exporter
tcp6 0 0 192.168.8.178:9100 192.168.8.177:52894 ESTABLISHED 10098/node_exporter
2. 配置mysqld_exporter
# 如果有类似报错:Access denied for user 'zabbix'@'localhost' (using password: YES)
# 估计要重置对应用户密码。
[root@client ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> grant replication client,process on *.* to 'mysql_monitor'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.05 sec)
MariaDB [mysql]> grant select on *.* to 'mysql_monitor'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> exit
Bye
# 解压mysql_exportor
[root@client ~]# tar xzf /opt/mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
# 使用指定数据库用户进行收集数据
[root@client ~]# vim /usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.cnf
[root@client ~]# cat /usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.cnf
[client]
user=mysql_monitor
password=123456
# 启动mysql_exportor,看到以下内容说明启动成功。
[root@client ~]# /usr/local/mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter-0.12.1.linux-amd64/.my.cnf" &
[1] 20812
[root@client ~]# INFO[0000] Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96) source="mysqld_exporter.go:257"
INFO[0000] Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58) source="mysqld_exporter.go:258"
INFO[0000] Enabled scrapers: source="mysqld_exporter.go:269"
INFO[0000] --collect.global_status source="mysqld_exporter.go:273"
INFO[0000] --collect.global_variables source="mysqld_exporter.go:273"
INFO[0000] --collect.slave_status source="mysqld_exporter.go:273"
INFO[0000] --collect.info_schema.innodb_cmp source="mysqld_exporter.go:273"
INFO[0000] --collect.info_schema.innodb_cmpmem source="mysqld_exporter.go:273"
INFO[0000] --collect.info_schema.query_response_time source="mysqld_exporter.go:273"
INFO[0000] Listening on :9104 source="mysqld_exporter.go:283
ご覧のように、prometheusサーバーはすでにシステムステータス情報とmysqlデータベース情報をリモートホストから取得できます。すべてが稼働しています。
# 查看下端口,监听正常
[root@client ~]# netstat -antup | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 10098/node_exporter
tcp6 0 0 192.168.8.178:9100 192.168.8.177:50488 ESTABLISHED 10098/node_exporter
[root@client ~]# netstat -antup | grep 9104
tcp6 0 0 :::9104 :::* LISTEN 20812/mysqld_export
tcp6 0 0 192.168.8.178:9104 192.168.8.177:39890 ESTABLISHED 20812/mysqld_export
これまでのところ、prometheusクラウドプラットフォームモニタリングシステムが導入されています。
4. grafanaを使用してプロメテウスを美しくする
モニターが上記のインターフェイスを見ている場合、prometheusのインターフェイスはzabbixよりも優れていると言えますか?現時点では、grafana美化ツールを使用して美化する必要があります。
4.1 安装grafana
[root@master ~]# ll /root/prometheus/grafana-7.1.3.linux-amd64.tar.gz
-rw-r--r--. 1 root root 52611015 8月 6 12:55 /root/prometheus/grafana-7.1.3.linux-amd64.tar.gz
[root@master ~]# tar xzf /root/prometheus/grafana-7.1.3.linux-amd64.tar.gz -C /usr/local/
[root@master ~]# cd /usr/local/grafana-7.1.3/
# 后台运行grafana
[root@master grafana-7.1.3]# ./bin/grafana-server &
# 可以看到后台运行的已经有两个程序了。
[root@master grafana-7.1.3]# jobs
[1]- 运行中 ./prometheus --config.file=prometheus.yml &(工作目录:/usr/local/prometheus-2.20.1.linux-amd64)
[2]+ 运行中 ./bin/grafana-server &
# 防火墙开放3000端口号
[root@master grafana-7.1.3]# firewall-cmd --permanent --zone=public --add-port=3000/tcp
success
[root@master grafana-7.1.3]# firewall-cmd --reload
success
# 还是不懂的,可以参考以下网址:
https://grafana.com/docs/grafana/latest/installation/rpm/
訪問:http://192.168.8.177:3000/login
以下を確認できます。
初期アカウントパスワード:admin
/ admin
初期パスワードの変更:
ログインページは次のとおりです。
4.2プロメテウスの美化
グラファナのインターフェースを開いてください。つまり、互換性に問題があります。ここに表示するにはGoogleを使用します。
システムステータス情報とmysqlデータベース情報のダッシュボードを追加します。
上記の検索により、2つのダッシュボードのIDは次のようになり
ます。8919 7362 システムステータスを
インポートするためのダッシュボード:mysqlデータベース情報をインポートするためのダッシュボード:
上記はシステムステータスであり、 mysqlデータベースのステータス情報の監視パネルが利用可能になりました。
5.エフェクト画像
5.1システム状態
5.2 mysqlデータベースのステータス
6.ダッシュボード監視パネルを調整します
ダッシュボードテンプレートをインポートした後は、削除しないことをお勧めします。最初にダッシュボードの監視コンテンツを理解してから、選択的に削除します。追加したいのはクエリを書くことなので、書くのは簡単ではありません。
以上がプロメテウス監視システムの内容ですが、他の実戦監視についてはブロガーに注目してフォローしてもらいます。