Skip to content

Commit

Permalink
prometheus & grafana
Browse files Browse the repository at this point in the history
  • Loading branch information
huataihuang committed Aug 7, 2023
1 parent 0235adf commit cb6cc04
Show file tree
Hide file tree
Showing 23 changed files with 259 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@

.. literalinclude:: grafana_behind_reverse_proxy_sub-path/sub-path_nginx.conf
:caption: nginx反向代理,grafana用sub-path模式 ``/etc/nginx/conf.d/onesre-core.conf``

- 启动服务(和Debian/Ubuntu相同):

.. literalinclude:: install_grafana/ubuntu_start_grafana
:caption: 启动Grafana

参考
=======

- `Run Grafana behind a reverse proxy <https://grafana.com/tutorials/run-grafana-behind-a-proxy/>`_
27 changes: 27 additions & 0 deletions source/kubernetes/monitor/grafana/grafana_mysql_data_source.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.. _grafana_mysql_data_source:

========================
Grafana使用MySQL数据源
========================

Grafana内置提供了MySQL数据源插件,也就是可以直接查询和可视化MySQL兼容的数据库数据。

这在日常运维工作中非常有用,因为大多数后端开发和运维工程师都不擅长前端开发,自己使用框架开发图表虽然也可行,但是毕竟效率较低,且很难达到Grafana这样专业的水准。虽然各大互联网公司都有自己的大数据平台和可视化报表系统,但是专有闭源平台往往非常沉重使用不变。对于个人和中小型公司,实际上使用 Grafana 结合数据库就能构建非常好的BI系统。

准备工作
==========

- 首先完成 :ref:`install_mariadb` ,获得可运行和访问的MySQL数据库:

.. literalinclude:: ../../../mysql/installation/install_mariadb/mariadb_startup
:caption: 快速安装、启动和初始化MariaDB

.. literalinclude:: ../../../mysql/installation/install_mariadb/create_db_user
:caption: 创建一个数据库并创建访问账号及授权

- 比较简单的方式是通过程序脚本、日志系统,向MySQL数据库加载数据

参考
=======

- `Grafana documentation > Data sources > MySQL <https://grafana.com/docs/grafana/latest/datasources/mysql/>`_
1 change: 1 addition & 0 deletions source/kubernetes/monitor/grafana/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Grafana通用可视分析平台
grafana_behind_reverse_proxy.rst
grafana_behind_reverse_proxy_sub-path.rst
grafana_reset_admin_password.rst
grafana_mysql_data_source.rst

推荐阅读:

Expand Down
6 changes: 6 additions & 0 deletions source/kubernetes/monitor/grafana/install_grafana_centos7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ CentOS 7安装Grafana
.. literalinclude:: install_grafana/ubuntu_start_grafana
:caption: 启动Grafana

反向代理
==========

Grafana提供了一个绑定在 ``3000`` 端口的WEB服务,为了方便访问,通常我们会在前端使用 :ref:`nginx` 做反向代理,有以下两种方式:

- :ref:`grafana_behind_reverse_proxy_sub-path` 使用现有域名的 ``sub-path`` ,不需要重新申请域名
- :ref:`grafana_behind_reverse_proxy` 独立的 ``grafana`` 域名

参考
=====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Prometheus社区官方提供了大约十几种 `xxxx_exporters <https://github.c
node_exporter_ipmitool_text_plugin.rst
node_exporter_smartctl_text_plugin.rst
ipmi_exporter.rst
../../../../performance/intel_pcm/pcm-exporter.rst
../../../../performance/intel_pcm/pcm-grafana.rst
../../../gpu/dcgm-exporter.rst
process-exporter.rst
amd_smi_exporter.rst
blackbox_exporter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Prometheus Node Exporter提供了一系列硬件和内核相关metric
.. literalinclude:: node_exporter/install_node_exporter
:caption: 安装Node Exporter执行程序


:strike:`要持续运行可以采用 screen` :

screen -S node_exporter -dm /usr/local/bin/node_exporter
Expand All @@ -36,6 +35,14 @@ Prometheus Node Exporter提供了一系列硬件和内核相关metric
.. literalinclude:: node_exporter/node_exporter.service
:caption: 配置 Node Exporter 服务,通过 :ref:`systemd` 运行

.. note::

运行 Node Exporter 的节点不需要使用 ``root`` 用户账号运行进程,可以采用 :ref:`prometheus_startup` 中配置的 ``prometheus`` 账号:

.. literalinclude:: ../prometheus_startup/add_prometheus_user
:language: bash
:caption: 在操作系统中添加 prometheus 用户

- 启动:

.. literalinclude:: node_exporter/systemd_node_exporter
Expand All @@ -46,10 +53,25 @@ Prometheus Node Exporter提供了一系列硬件和内核相关metric
.. literalinclude:: node_exporter/systemd_node_exporter_output
:caption: 通过 :ref:`systemctl` 启动 ``node_exporter`` 服务的状态观察

快速部署
------------

- 一个简单的脚本,方便快速部署:

.. literalinclude:: node_exporter/install_node-exporter.sh
:language: bash
:caption: 快速在被监控节点部署安装 Node Exporter 的脚本 ``install_node-exporter.sh``

- 执行安装类似 :ref:`homebrew` :

.. literalinclude:: node_exporter/curl_install_node-exporter
:language: bash
:caption: 执行安装

检查
=======

- 一旦 ``node_exporter`` 运行起来,可以见证metrics::
- 一旦 ``node_exporter`` 运行起来,可以检查metrics::

curl http://localhost:9100/metrics

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/bash -c "$(curl -fsSL https://onesre.cloud-atlas.io/download/install_node-exporter.sh)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cd /tmp
version=1.6.1
curl https://onesre.cloud-atlas.io/download/node_exporter-${version}.linux-amd64.tar.gz -o node_exporter-${version}.linux-amd64.tar.gz
tar xvfz node_exporter-${version}.linux-amd64.tar.gz
cd node_exporter-${version}.linux-amd64/
sudo mv node_exporter /usr/local/bin/

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus

curl https://onesre.cloud-atlas.io/download/node_exporter.service -o /etc/systemd/system/node_exporter.service
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
sudo systemctl status node_exporter
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
...
scrape_configs:
...
- job_name: "node"
static_configs:
- targets:
- localhost:9100
- 192.168.6.11:9100
- 192.168.6.12:9100
40 changes: 32 additions & 8 deletions source/mysql/installation/install_mariadb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

MariaDB是开源关系型数据库,和MySQL兼容并且用于替代MySQL。这是因为Oracle收购了SUN公司之后拥有了MySQL,潜在影响了MySQL开源。所以MySQL社区基于原先MySQL fork出了新的开源项目MariaDB,并得到了众多Linux发行版支持。从CentOS 7开始,MiriaDB已经取代了MySQL作为默认数据库系统。

快速起步
==========

实际上在CentOS上安装部署并启动MariaDB只有3条命令,所以可以快速完成:

.. literalinclude:: install_mariadb/mariadb_startup
:caption: 快速安装、启动和初始化MariaDB

安装
=========

- 执行以下命令安装::
- 执行以下命令安装:

sudo yum -y install mariadb-server
.. literalinclude:: install_mariadb/yum_install_mariadb
:caption: 在CentOS上安装 MariaDB

建议同时安装 ``mariadb`` 和 ``mariadb-devel`` 软件包,方便后续开发

Expand All @@ -21,14 +30,18 @@ MariaDB是开源关系型数据库,和MySQL兼容并且用于替代MySQL。这

sudo yum install MariaDB-server MariaDB-client

- 启动数据库::
- 启动数据库:

sudo systemctl start mariadb
sudo systemctl enable mariadb
.. literalinclude:: install_mariadb/start_mariadb
:caption: 启动MariaDB

手工启动数据库
-----------------

.. note::

如果直接使用上文操作系统发行版安装MariaDB数据库,则已经自动初始化数据库了。无需本段手工初始化和手工启动,本段落只适用于需要重建数据库的情况。

- 首先初始化系统数据库目录和系统表::

sudo -u mysql mysql_install_db
Expand Down Expand Up @@ -60,11 +73,12 @@ MariaDB是开源关系型数据库,和MySQL兼容并且用于替代MySQL。这
实际上仔细查看了文件权限发现,原来 ``mysql_install_db`` 命令必须使用 ``mysql`` 用户来执行,否则创建的 ``/var/lib/mysql/mysql`` 目录以及该目录下文件都是属于root用户的。不过,mysql用户账号默认是 ``nologin`` ,所以需要使用 ``sudo -u mysql`` 方式来运行。

数据库安全加固
---------------
====================

- 数据库默认安全加固::
- 使用 ``root`` 用户身份登陆系统,执行以下数据库默认安全加固脚本:

/usr/bin/mysql_secure_installation
.. literalinclude:: install_mariadb/mysql_secure_installation
:caption: 执行初始化安全加固( **重要操作** )

上述脚本命令也可以直接手工执行::

Expand All @@ -75,6 +89,16 @@ MariaDB是开源关系型数据库,和MySQL兼容并且用于替代MySQL。这

在MariaDB的高版本中默认安全策略只允许操作系统的 ``root`` 用户执行上述 ``mysql_secure_installation`` 安全加固脚本,以便首次以无密码方式登陆mysql的root账号。

创建数据库及授权
=====================

- 快速创建一个数据库并创建访问账号及授权:

.. literalinclude:: install_mariadb/create_db_user
:caption: 创建一个数据库并创建访问账号及授权

从MySQL 8.0开始,默认字符集是 ``utf8mb4_0900_ai_ci``

参考
======

Expand Down
4 changes: 4 additions & 0 deletions source/mysql/installation/install_mariadb/create_db_user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create database mydb character set utf8;
grant usage on mydb.* to myapp_user@'%' identified by 'myapp_passwd';
grant all privileges on mydb.* to myapp_user@'%';
flush privileges;
3 changes: 3 additions & 0 deletions source/mysql/installation/install_mariadb/mariadb_startup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sudo yum -y install mariadb-server
sudo sytemctl enable --now mariadb
sudo /usr/bin/mysql_secure_installation
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/bin/mysql_secure_installation
1 change: 1 addition & 0 deletions source/mysql/installation/install_mariadb/start_mariadb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sudo sytemctl enable --now mariadb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sudo yum -y install mariadb-server
11 changes: 3 additions & 8 deletions source/mysql/installation/install_mysql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,10 @@ Homebrew安装

前者通过 :ref:`docker` 的官方镜像,支持传递环境变量就可以自动配置好MySQL数据库;但是后者则是传统的数据库维护方式,需要我们做数据库初始化才能用于 :ref:`django` 开发。

- 在MySQL中创建数据库 ``mydb`` 并创建 ``myapp_user`` 账号及对应密码 ``myapp_passwd`` ::
- 在MySQL中创建数据库 ``mydb`` 并创建 ``myapp_user`` 账号及对应密码 ``myapp_passwd`` :

create database mydb characterset utf8;

grant usage on mydb.* to myapp_user@'%' identified by 'myapp_passwd';

grant all privileges on mydb.* to myapp_user@'%';

flush privileges;
.. literalinclude:: install_mariadb/create_db_user
:caption: 创建一个数据库并创建访问账号及授权

参考
========
Expand Down
1 change: 1 addition & 0 deletions source/mysql/query/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ MySQL 查询
:maxdepth: 1

mysql_query_date_time.rst
mysql_query_group_by_order_by.rst

.. only:: subproject and html

Expand Down
16 changes: 16 additions & 0 deletions source/mysql/query/mysql_query_group_by_order_by.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.. _mysql_query_group_by_order_by:

==================================
MySQL查询数据GROUP BY和ORDER BY
==================================

MySQL的查询计数非常简单,通过 ``COUNT ... GROUP BY`` 可以对指定字段进行计数,为了方便排序, ``COUNT`` 字段应该有一个 ``alias`` :

.. literalinclude:: mysql_query_group_by_order_by/sql_group_order.sql
:language: sql
:caption: 统计字段数量,并且进行排讯

参考
======

- `Ordering by specific field value first <https://stackoverflow.com/questions/14104055/ordering-by-specific-field-value-first>`_
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select alert,count(*) as num from notifier_alert_statistics group by alert order by num DESC;
10 changes: 10 additions & 0 deletions source/performance/intel_pcm/build_pcm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
.. literalinclude:: build_pcm/deploy_pcm
:caption: 快速部署自己编译的pcm-exporter

也可以通过类似 :ref:`homebrew` 方法执行脚本安装:

.. literalinclude:: build_pcm/install_pcm.sh
:language: bash
:caption: 快速在被监控节点部署安装 Intel PCM 的脚本 ``install_pcm.sh``

.. literalinclude:: build_pcm/curl_install_pcm
:language: bash
:caption: crul 执行安装 脚本 ``intall_pcm.sh``

问题排查
==========

Expand Down
1 change: 1 addition & 0 deletions source/performance/intel_pcm/build_pcm/curl_install_pcm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/bash -c "$(curl -fsSL https://onesre.cloud-atlas.io/download/install_pcm.sh)"
6 changes: 6 additions & 0 deletions source/performance/intel_pcm/build_pcm/install_pcm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
curl https://onesre.cloud-atlas.io/download/pcm.tar.gz -o /pcm.tar.gz
cd /
tar xfz pcm.tar.gz
systemctl daemon-reload
systemctl enable --now pcm-exporter
rm /pcm.tar.gz

0 comments on commit cb6cc04

Please sign in to comment.