使用Ansible工具在CentOS 7上构建Prometheus和Grafana
使用Ansible在CentOS 7上构建Prometheus和Grafana。
Prometheus和Grafana的简介
-
- 10分で理解する Prometheus
-
- 10分で理解するGrafana
-
- prometheus.io/docs/introduction/overview/
- grafana.com/docs/grafana/latest/guides/basic_concepts
建立的经过
-
- PrometheusとGrafanaを手動で構築するのが面倒なので、自動化したかった。
- prometheus.ymlの編集、各サービスの自動起動有効化とサービスの起動設定を手動で行うのが面倒なので、自動化したかった。
目标人群
- CentOS 7 にて、PrometheusとGrafanaの構築を簡単に行いたい方
前提条件
-
- Ansibleはインストール済みであるとする
-
- config、inventoryの設定は完了済みであるとする
-
- 鍵生成、鍵交換、疎通確認は完了済みであるとする
- 今回は、proxy環境下ではないものとする
执行环境 (shí
# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
# ansible --version
ansible 2.9.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
执行本攻略手册后所获得的结果
-
- Prometheusが使用可能となる。
-
- Grafanaが使用可能となる。
-
- Node_exporterが使用可能となる。
- Pushgatewayが使用可能となる。
在执行本playbook之后获得的环境版本信息
項目VersionURLprometheus2.14.0http://localhost:9090/grafana6.5.1-1http://localhost:3000/node_exporter0.18.1http://localhost:9100/pushgateway1.0.0http://localhost:9091/
这本playbook的概述
-
- 各パッケージのインストール
-
- prometheus ユーザーとグループの作成
-
- 各ファイルのダウンロード
-
- grafanaのインストール
-
- モニタリングデータを配置用ディレクトリを作成
-
- シンボリックリンクの作成
-
- prometheus.yml の設定
-
- 各サービスの設定
-
- systemctl daemon-reload の実行
- 各サービスの自動起動設定の有効化とサービスの起動
这本playbook的执行方法
# ansible-playbook SetupPrometheusGrafana.yml
構築するplaybookは、PrometheusとGrafanaです。
- name: Setup Prometheus Grafana
hosts: localhost
tasks:
#--------------------------------------------------#
# 各パッケージのインストール
#--------------------------------------------------#
- name: Install the ius-release.rpm from a remote repo
yum:
name: https://centos7.iuscommunity.org/ius-release.rpm
state: present
- name: Install a list of packages
yum:
name:
- python36u
- python36u-libs
- python36u-devel
- python36u-pip
state: present
- name: Install a list of packages for Python 3.6
pip:
name:
- pip
- prometheus_client
- requests
executable: pip3.6
#--------------------------------------------------#
# prometheus ユーザーとグループの作成
#--------------------------------------------------#
- name: Ensure group "prometheus" exists
group:
name: prometheus
state: present
- name: Add the user 'prometheus' with a bash shell, appending the group 'prometheus' to the user's groups
user:
name: prometheus
shell: /bin/bash
groups: prometheus
append: yes
#--------------------------------------------------#
# 各ファイルのダウンロード
# - prometheus
# - node_exporter
# - pushgateway
#--------------------------------------------------#
- name: Unarchive prometheus that needs to be downloaded
unarchive:
src: https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz
dest: /usr/local/src
owner: prometheus
group: prometheus
remote_src: yes
- name: Unarchive node_exporter that needs to be downloaded
unarchive:
src: https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
dest: /usr/local/src
owner: prometheus
group: prometheus
remote_src: yes
- name: Unarchive pushgateway that needs to be downloaded
unarchive:
src: https://github.com/prometheus/pushgateway/releases/download/v1.0.0/pushgateway-1.0.0.linux-amd64.tar.gz
dest: /usr/local/src
owner: prometheus
group: prometheus
remote_src: yes
#--------------------------------------------------#
# grafanaのインストール
#--------------------------------------------------#
- name: Install the Grafana rpm from a remote repo
yum:
name: https://dl.grafana.com/oss/release/grafana-6.5.1-1.x86_64.rpm
state: present
#--------------------------------------------------#
# モニタリングデータを配置用ディレクトリを作成
#--------------------------------------------------#
- name: make directory /var/lib/prometheus
file:
path: /var/lib/prometheus
state: directory
owner: prometheus
group: prometheus
mode: '0755'
#--------------------------------------------------#
# シンボリックリンクの作成
# - prometheus
# - node_exporter
# - pushgateway
#--------------------------------------------------#
- name: Create prometheus symbolic link
file:
src: /usr/local/src/prometheus-2.14.0.linux-amd64/prometheus
dest: /usr/local/prometheus
state: link
- name: Create node_exporter symbolic link
file:
src: /usr/local/src/node_exporter-0.18.1.linux-amd64/node_exporter
dest: /usr/local/node_exporter
state: link
- name: Create pushgateway symbolic link
file:
src: /usr/local/src/pushgateway-1.0.0.linux-amd64/pushgateway
dest: /usr/local/pushgateway
state: link
#--------------------------------------------------#
# prometheus.yml の設定
#--------------------------------------------------#
- name: Remove prometheus.yml
file:
path: /usr/local/src/prometheus-2.14.0.linux-amd64/prometheus.yml
state: absent
- name: Insert/Update prometheus.yml in /usr/local/src/prometheus-2.14.0.linux-amd64/
blockinfile:
path: /usr/local/src/prometheus-2.14.0.linux-amd64/prometheus.yml
create: yes
owner: prometheus
group: prometheus
mode: '0755'
marker: ""
block: |
# 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:
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
- job_name: node_exporter
static_configs:
- targets:
- localhost:9100
- job_name: pushgateway
static_configs:
- targets:
- localhost:9091
#--------------------------------------------------#
# prometheus.service の設定
#--------------------------------------------------#
- name: Remove prometheus.service
file:
path: /etc/systemd/system/prometheus.service
state: absent
- name: Insert/Update prometheus.service in /etc/systemd/system/
blockinfile:
path: /etc/systemd/system/prometheus.service
create: yes
marker: ""
mode: '0755'
block: |
[Unit]
Description=Prometheus - Monitoring system and time series database
Documentation=https://prometheus.io/docs/introduction/overview/
[Service]
Restart=always
User=prometheus
Type=simple
ExecStart=/usr/local/src/prometheus-2.14.0.linux-amd64/prometheus \
--config.file=/usr/local/src/prometheus-2.14.0.linux-amd64/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus/data
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
#--------------------------------------------------#
# node_exporter.service の設定
#--------------------------------------------------#
- name: Remove node_exporter.service
file:
path: /etc/systemd/system/node_exporter.service
state: absent
- name: Insert/Update node_exporter.service in /etc/systemd/system/
blockinfile:
path: /etc/systemd/system/node_exporter.service
create: yes
marker: ""
mode: '0755'
block: |
[Unit]
Description=node_exporter for Prometheus
[Service]
Restart=always
User=prometheus
ExecStart=/usr/local/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
#--------------------------------------------------#
# pushgateway.service の設定
#--------------------------------------------------#
- name: Remove pushgateway.service
file:
path: /etc/systemd/system/pushgateway.service
state: absent
- name: Insert/Update pushgateway.service in /etc/systemd/system/
blockinfile:
path: /etc/systemd/system/pushgateway.service
create: yes
marker: ""
mode: '0755'
block: |
[Unit]
Description=pushgateway for Prometheus
[Service]
Restart=always
User=prometheus
ExecStart=/usr/local/pushgateway
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
#--------------------------------------------------#
# systemctl daemon-reload の実行
#--------------------------------------------------#
- name: just force systemd to reread configs
systemd:
daemon_reload: yes
#--------------------------------------------------#
# 各サービスの自動起動設定の有効化とサービスの起動
# - prometheus.service
# - node_exporter.service
# - pushgateway.service
# - grafana-server.service
#--------------------------------------------------#
- name: Systemctl enabled prometheus & systemctl start prometheus
systemd:
name: prometheus
state: started
enabled: yes
- name: Systemctl enabled node_exporter & systemctl start node_exporter
systemd:
name: node_exporter
state: started
enabled: yes
- name: Systemctl enabled pushgateway & systemctl start pushgateway
systemd:
name: pushgateway
state: started
enabled: yes
- name: Systemctl enabled grafana-server & systemctl start grafana-server
systemd:
name: grafana-server
state: started
enabled: yes
执行Playbook时的日志
[root@localhost playbook]# ansible-lint SetupPrometheusGrafana.yml -v
Examining SetupPrometheusGrafana.yml of type playbook
[root@localhost playbook]# ansible-playbook SetupPrometheusGrafana.yml
PLAY [Setup Prometheus Grafana] ***********************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Install the ius-release.rpm from a remote repo] *************************************************************************************************************************************************************
ok: [localhost]
TASK [Install a list of packages] *********************************************************************************************************************************************************************************
changed: [localhost]
TASK [Install a list of packages for Python 3.6] ******************************************************************************************************************************************************************
ok: [localhost]
TASK [Ensure group "prometheus" exists] ***************************************************************************************************************************************************************************
changed: [localhost]
TASK [Add the user 'prometheus' with a bash shell, appending the group 'prometheus' to the user's groups] *********************************************************************************************************
changed: [localhost]
TASK [Unarchive prometheus that needs to be downloaded] ***********************************************************************************************************************************************************
changed: [localhost]
TASK [Unarchive node_exporter that needs to be downloaded] ********************************************************************************************************************************************************
changed: [localhost]
TASK [Unarchive pushgateway that needs to be downloaded] **********************************************************************************************************************************************************
changed: [localhost]
TASK [Install the Grafana rpm from a remote repo] *****************************************************************************************************************************************************************
changed: [localhost]
TASK [make directory /var/lib/prometheus] *************************************************************************************************************************************************************************
ok: [localhost]
TASK [Create prometheus symbolic link] ****************************************************************************************************************************************************************************
ok: [localhost]
TASK [Create node_exporter symbolic link] *************************************************************************************************************************************************************************
ok: [localhost]
TASK [Create pushgateway symbolic link] ***************************************************************************************************************************************************************************
ok: [localhost]
TASK [Remove prometheus.yml] **************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Insert/Update prometheus.yml in /usr/local/src/prometheus-2.14.0.linux-amd64/] ******************************************************************************************************************************
changed: [localhost]
TASK [Remove prometheus.service] **********************************************************************************************************************************************************************************
ok: [localhost]
TASK [Insert/Update prometheus.service in /etc/systemd/system/] ***************************************************************************************************************************************************
changed: [localhost]
TASK [Remove node_exporter.service] *******************************************************************************************************************************************************************************
ok: [localhost]
TASK [Insert/Update node_exporter.service in /etc/systemd/system/] ************************************************************************************************************************************************
changed: [localhost]
TASK [Remove pushgateway.service] *********************************************************************************************************************************************************************************
ok: [localhost]
TASK [Insert/Update pushgateway.service in /etc/systemd/system/] **************************************************************************************************************************************************
changed: [localhost]
TASK [just force systemd to reread configs] ***********************************************************************************************************************************************************************
ok: [localhost]
TASK [Systemctl enabled prometheus & systemctl start prometheus] **************************************************************************************************************************************************
changed: [localhost]
TASK [Systemctl enabled node_exporter & systemctl start node_exporter] ********************************************************************************************************************************************
changed: [localhost]
TASK [Systemctl enabled pushgateway & systemctl start pushgateway] ************************************************************************************************************************************************
changed: [localhost]
TASK [Systemctl enabled grafana-server & systemctl start grafana-server] ******************************************************************************************************************************************
changed: [localhost]
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=27 changed=16 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
确认访问Grafana
确认对Prometheus的访问
# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
- job_name: node_exporter
static_configs:
- targets:
- localhost:9100
- job_name: pushgateway
static_configs:
- targets:
- localhost:9091
- prometheus
- node_exporter
- pushgateway
确认访问Node Exporter
确认访问 Pushgateway
总结
使用Ansible可以在CentOS 7上搭建Prometheus和Grafana。接下来,我想根据参考书籍学习如何使用Prometheus和Grafana。
我已经学会如何使用Ansible的roles,所以希望能够利用roles创建playbook并考虑目录结构。在Qiita中,我打算创建整体playbook,在GitHub或GitLab上发布使用roles的playbook。
请提供参考链接。
本次使用的模块
-
- 【docs.ansible.com】yum – Manages packages with the yum package manager
-
- 【docs.ansible.com】pip – Manages Python library dependencies
-
- 【docs.ansible.com】group – Add or remove groups
-
- 【docs.ansible.com】user – Manage user accounts
-
- 【docs.ansible.com】unarchive – Unpacks an archive after (optionally) copying it from the local machine
-
- 【docs.ansible.com】file – Manage files and file properties
-
- 【docs.ansible.com】blockinfile – Insert/update/remove a text block surrounded by marker lines
- 【docs.ansible.com】systemd – Manage services
普罗米修斯,Grafana 相关
-
- prometheus.io/download/
-
- grafana.com/grafana/download
-
- Installing on RPM-based Linux (CentOS, Fedora, OpenSuse, RedHat)
-
- PrometheusとPushgatewayでGoogle Finance APIの株価をPushしてモニタリングしてみた
-
- Prometheusでのさまざまな監視データ取得法
-
- 入門 Prometheus
- ――インフラとアプリケーションのパフォーマンスモニタリング