目录

一、部署Prometheus

1、环境准备工作

2、puometheus部署

2.1上传prometheus到opt目录下,并解压

2.2修改prometheus配置文件

2.3配置系统启动文件,设置开机自启

2.4开启prometheus,并访问网页验证

 二、部署Exporter(192.168.226.129)

1、监控远程Linux主机192.168.226.129

1.1上传node_exporter到opt目录,并解压

1.2启动node_exporter

 1.3修改prometheus服务器的配置文件

 1.4访问prometheus服务器

 2、监控远程MySQL

2.1上传mysqld_exporter到opt目录下,解压

 2.2进入到mysql数据库,进行授权

 2.3为mysqld_exporter创建个配置文件

2.4启动组件

 2.5浏览器访问一下默认端口9104

 2.6修改prometheus服务器的配置文件

 2.7访问prometheus服务器

 三、部署Grafana进行展示

1、下载安装Grafana(192.168.226.130)

 2、配置数据源

 3、导入模板

4、为数据源做数据展示

 5、导入grafana监控模板

 6、Grafana图形显示MySQL监控数据


一、部署Prometheus

1、环境准备工作

服务器

IP地址

组件

Prometheus服务器

192.168.226.128

Prometheus、node_exporter

agent服务器

192.168.226.129

node_exporter

grafana服务器

192.168.226.130

Grafana

2、puometheus部署

2.1上传prometheus到opt目录下,并解压

tar zxf prometheus-2.27.1.linux-amd64.tar.gz
mv prometheus-2.27.1.linux-amd64 /usr/local/prometheus
cd /usr/local/prometheus/
ls

prometheus kubernetes 配置 prometheus 部署_prometheus

2.2修改prometheus配置文件

cat /usr/local/prometheus/prometheus.yml | grep -v "^#"
global:					#用于prometheus的全局配置,比如采集间隔,抓取超时时间等
  scrape_interval: 15s			#采集目标主机监控数据的时间间隔,默认为1m
  evaluation_interval: 15s 		#触发告警生成alert的时间间隔,默认是1m
  # scrape_timeout is set to the global default (10s).
  scrape_timeout: 10s			#数据采集超时时间,默认10s
 
alerting:				#用于alertmanager实例的配置,支持静态配置和动态服务发现的机制
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093
 
rule_files:				#用于加载告警规则相关的文件路径的配置,可以使用文件名通配机制
  # - "first_rules.yml"
  # - "second_rules.yml"
 
scrape_configs:			#用于采集时序数据源的配置
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"		#每个被监控实例的集合用job_name命名,支持静态配置(static_configs)和动态服务发现的机制(*_sd_configs)
 
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
 
    static_configs:				#静态目标配置,固定从某个target拉取数据
      - targets: ["localhost:9090"]

prometheus kubernetes 配置 prometheus 部署_prometheus_02

2.3配置系统启动文件,设置开机自启

[root@prometheus prometheus]# vim /usr/lib/systemd/system/prometheus.service
 
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target
 
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention=15d \
--web.enable-lifecycle
  
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
 
[Install]
WantedBy=multi-user.target



2.4开启prometheus,并访问网页验证

prometheus kubernetes 配置 prometheus 部署_运维_03

prometheus kubernetes 配置 prometheus 部署_prometheus_04

 

prometheus kubernetes 配置 prometheus 部署_mysql_05

 通过http://192.168.226.128:9090/metrics可以查看到监控的数据

 

prometheus kubernetes 配置 prometheus 部署_服务器_06

 二、部署Exporter(192.168.226.129)

1、监控远程Linux主机192.168.226.129

1.1上传node_exporter到opt目录,并解压

tar zxf node_exporter-1.1.2.linux-amd64.tar.gz
mv node_exporter-1.1.2.linux-amd64 /usr/local/bin/

prometheus kubernetes 配置 prometheus 部署_prometheus_07

1.2启动node_exporter

./node_exporter
 
netstat -natp | grep :9100
 
浏览器访问:http://192.168.226.129:9100/metrics ,可以看到 Node Exporter 采集到的指标数据

prometheus kubernetes 配置 prometheus 部署_grafana_08

 

prometheus kubernetes 配置 prometheus 部署_grafana_09

 1.3修改prometheus服务器的配置文件

vim prometheus.yml

  - job_name: 'nodes'
    static_configs:
    - tarhets: ['192.168.226.129:9100']

prometheus kubernetes 配置 prometheus 部署_grafana_10

 改完配置文件后,重启服务

prometheus kubernetes 配置 prometheus 部署_mysql_11

 1.4访问prometheus服务器

prometheus kubernetes 配置 prometheus 部署_mysql_12

 2、监控远程MySQL

2.1上传mysqld_exporter到opt目录下,解压

tar zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz
mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter

prometheus kubernetes 配置 prometheus 部署_prometheus_13

 2.2进入到mysql数据库,进行授权

进入到数据库

create user 'exporter'@'%' identified by '123456';

grant process,replication client,select on *.* to 'exporter'@'%' identified by '123456';

flush privileges;

prometheus kubernetes 配置 prometheus 部署_服务器_14

 2.3为mysqld_exporter创建个配置文件

vim /usr/local/mysqld_exporter/mysqld_exporter.cnf

[client]

user=exporter

password=123456

2.4启动组件

./mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/mysqld_exporter.cnf

prometheus kubernetes 配置 prometheus 部署_grafana_15

 2.5浏览器访问一下默认端口9104

prometheus kubernetes 配置 prometheus 部署_prometheus_16

 2.6修改prometheus服务器的配置文件

vim prometheus.yml

- job_name: 'node_mysql'
    static_configs:
    - targets: ['192.168.226.129:9104']

prometheus kubernetes 配置 prometheus 部署_grafana_17

 重启服务,看一下状态

prometheus kubernetes 配置 prometheus 部署_运维_18

 2.7访问prometheus服务器

prometheus kubernetes 配置 prometheus 部署_grafana_19

 三、部署Grafana进行展示

1、下载安装Grafana(192.168.226.130)

rpm -ivh /opt/grafana-7.3.6-1.x86_64.rpm
 
systemctl start grafana-server
systemctl enable grafana-server
 
netstat -natp | grep :3000
 
浏览器访问:http://192.168.109.19:3000 ,默认账号和密码为 admin/admin

prometheus kubernetes 配置 prometheus 部署_grafana_20

prometheus kubernetes 配置 prometheus 部署_运维_21

 

prometheus kubernetes 配置 prometheus 部署_grafana_22

 2、配置数据源

下面我们把 Prometheus 服务器收集的数据做为一个数据源添加到 grafana,让 grafana 可以得到 Prometheus 的数据

prometheus kubernetes 配置 prometheus 部署_grafana_23

prometheus kubernetes 配置 prometheus 部署_grafana_24

 

prometheus kubernetes 配置 prometheus 部署_运维_25

 

prometheus kubernetes 配置 prometheus 部署_mysql_26

 3、导入模板

prometheus kubernetes 配置 prometheus 部署_服务器_27

prometheus kubernetes 配置 prometheus 部署_服务器_28

prometheus kubernetes 配置 prometheus 部署_运维_29

prometheus kubernetes 配置 prometheus 部署_grafana_30

4、为数据源做数据展示

prometheus kubernetes 配置 prometheus 部署_运维_31

prometheus kubernetes 配置 prometheus 部署_grafana_32

prometheus kubernetes 配置 prometheus 部署_运维_33

 自定义名称,点击保存在dashboard可以查看

prometheus kubernetes 配置 prometheus 部署_mysql_34

 5、导入grafana监控模板

浏览器访问:https://grafana.com/grafana/dashboards ,在页面中搜索 node exporter ,选择适合的面板,点击 Copy ID 或者 Download JSON
 
在 grafana 页面中,+ Create -> Import ,输入面板 ID 号或者上传 JSON 文件,点击 Load,即可导入监控面板

prometheus kubernetes 配置 prometheus 部署_prometheus_35

prometheus kubernetes 配置 prometheus 部署_运维_36

 

prometheus kubernetes 配置 prometheus 部署_grafana_37

prometheus kubernetes 配置 prometheus 部署_grafana_38

prometheus kubernetes 配置 prometheus 部署_服务器_39

prometheus kubernetes 配置 prometheus 部署_prometheus_40

prometheus kubernetes 配置 prometheus 部署_mysql_41

 6、Grafana图形显示MySQL监控数据

在 grafana 上修改配置文件,并下载安装 mysql 监控的 dashboard(包含相关 json 文件,这些 json 文件可以看作是开发人员开发的一个监控模板)

prometheus kubernetes 配置 prometheus 部署_mysql_42

prometheus kubernetes 配置 prometheus 部署_grafana_43

prometheus kubernetes 配置 prometheus 部署_prometheus_44

 在grafana图形化界面导入相关的json文件
用grafana服务器上的firefox浏览器打开,方便上传,模板这里选择7362

prometheus kubernetes 配置 prometheus 部署_运维_45

 

prometheus kubernetes 配置 prometheus 部署_mysql_46

 

prometheus kubernetes 配置 prometheus 部署_grafana_47

prometheus kubernetes 配置 prometheus 部署_mysql_48