Promethues监控+Grafana出图工具

目录

简介 

架构

组件

Prometheus server 

exporter

Pushgateway

Alertmanager

Prometheus web UI

metrics

获取数据的方式

prometheus 和 zabbix的区别?

用容器安装prometheus

实验

第一步:源码安装promehtues

第二步:在node节点服务器上安装exporter程序

第三步: 在prometheus server里添加我们在哪些机器里安装了exporter程序,这样就可以知道去哪里pull数据

使用prometheus监控容器

prometheus 监控MySQL服务器

Grafana出图工具

简介

实验

第一步:安装

garafana页面操作 


简介 

Prometheus是一个开源系统监控和警报工具包,最初是在SoundCloud上构建的。自 2012 年成立以来,许多公司和组织都采用了 Prometheus,该项目拥有非常活跃的开发人员和用户社区。它现在是一个独立的开源项目,独立于任何公司进行维护。为了强调这一点,并澄清项目的治理结构,Prometheus于2016年加入了云原生计算基金会,成为继Kubernetes之后的第二个托管项目。

Prometheus 收集其指标并将其存储为时间串行数据,即指标信息与记录它的时间戳一起存储,以及称为标签的可选键值对。

 

为什么要用监控软件?

工具或者软件去帮助我们7*24监控我们的服务器和软件,是否还在正常的工作。如果不工作,马上告警(短信,电话,微信,钉钉),及时处理。

 

监控的价值:

防范事故与未然,减少事故带来的损失

及时的发现问题,提醒工程师尽快的去解决问题,减少公司的损失

提升了产品的品质,增强了用户的信任

 

有命令去查看CPU,内存,磁盘,网络,进程,也可以查看,为什么还需要监控软件,prometheus,zabbix? 

1.时间上的优势
    命令获取数据不能随时得到数据,监控软件可以时时刻刻提我们去获取数据
    命令在输入的时候,可以获取,不输入就不能获取
2.操作更加简化,界面效果更加直观,门槛更加低
3.数据图形化--》可视化效果好
4.可操作性,继承性--》员工

 

监控软件:  第3方开源的监控软件

1.cacti  仙人掌: 出图比较好
2.nagios  监控脚本特别多
3.zabbix  集合cacti+nagios的优点:  --》企业里使用非常多   ---》php
4.open-falcon  小米公司开源的监控软件: 京东,滴滴,小米,字节等
5.prometheus :开源的监控软件
                使用go语言
                并发性能非常好
                资源消耗比较少

架构

Prometheus 直接从检测的作业中抓取指标 

组件

  • Prometheus server 

Prometheus Server是Prometheus组件中的核心部分,负责实现对监控数据的获取,存储以及查询。其中包含了三个组件:

Retrieval:  获取监控数据

TSDB: 时序数据库(Time Series Database),我们可以简单的理解为一个优化后用来处理时间序列数据的软件,并且数据中的数组是由时间进行索引的。具备以下特点:

  • 大部分时间都是顺序写入操作,很少涉及修改数据
  • 删除操作都是删除一段时间的数据,而不涉及到删除无规律数据
  • 读操作一般都是升序或者降序

自定义的PromQL语言,实现对数据的查询以及分析。promQL : select ,insert等  Prometheus 内部的SQL语句

HTTP server:为告警和出图提供查询接口

 

  • exporter

理解为植入客户机的一个程序,程序专门收集客户机的数据,发送给prometheus 服务器 

收集数据,采集数据  木马程序 : 安装到被监控的机器上   agent 代理 

Prometheus的一类数据采集组件的总称。它负责从目标处搜集数据,并将其转化为Prometheus支持的格式。与传统的数据采集组件不同的是,它并不向中央服务器发送数据,而是等待中央服务器主动前来抓取。

node_exporter  通用的exporter
mysqld_exporter   定制的exporter

exporter : 是prometheus的客户端程序,需要安装到被监控的服务器上。exporter是一个程序,需要去定制,但是prometheus平台给我们开发了很多通用的或者定制的exporter

exporter会到客户机(被监控的服务器上)收集指定的指标数据,例如:cpu的使用率,内存的使用率,磁盘的使用情况,网络的带宽使用情况等等数据

cpu,内存,磁盘,网络带宽--》通用常规的指标

nginx,MySQL,redis软件的内部的数据---》定制的exporter

  • Pushgateway

中间件(代理),临时存放数据的软件

支持临时性Job主动推送指标的中间网关

对于短暂运行的任务,负责接收和缓存时间序列数据,同时也是一个数据源

 

  • Alertmanager

处理报警

 

  • Prometheus web UI

其他各种支持的工具

用处:

采集数据:exporter   pushgateway  中间件(代理)
存储数据:tsdb
提供数据:http server 
显示数据: grafana
告警、报警:alertmanager

metrics

指标,就是各种各样的数据,例如cpu的使用率,内存的使用率,网络的带宽,磁盘等数据

获取数据的方式

1.pull  server --》pull ---》client   主动的获取数据,避免大并发
2.push  client --->push---》server   client 主动送数据过来,数据会非常新,会出现大量的数据同时push过来

push和pull 那个更加好?

1.数据的实时性--》得到最新鲜的数据 --》push
2.服务器的负载,网络的负载--》可控性 --》pull

prometheus 和 zabbix的区别?

主要使用场景区别是,Zabbix适合用于虚拟机、物理机的监控,因为每个监控指标是以 IP 地址作为标识进行区分的。而Prometheus的监控指标是由多个 label 组成,IP地址并不是唯一的区分指标,Prometheus 强大在可以支持自动发现规则,因此适合于容器环境。
从自定义监控项角度而言,Prometheus 开发难度较大,zabbix配合shell脚本更加方便。Prometheus在监控虚拟机上业务时,可能需要安装多个 exporter,而zabbix只需要安装一个 Agent。
Prometheus 采用拉数据方式,即使采用的是push-gateway,prometheus也是从push-gateway拉取数据。而Zabbix可以推可以拉。

对比项PrometheuszabbixPromethtues优势zabbix优势

管理

二进制文件启动LNMP+编译轻量级server-

配置

配置文件图形化更好的支持自动化配置学习成本低
client丰富的client库zabbix-自定义脚本为各自中间件、应用提供专业的exporter,监控项更全面支持自定义监控项,对监控设计者的格局要求较高
数据存储方式Prometheus TSDBMySQL监控数据以时间为维度统计情况较多,时序数据库更适合于监控数据的存储,按时间索引性能更高MySQL较常用,学习成本低

数据处理

PromQLMySQLPromQL计算函数丰富,统计维度广同上
二次开发丰富的sdkapi

提供了Go、Java/Scaia、Python、Ruby等sdk,二次开发更便捷

api配置较为常用,学习成本低
对云环境的支持原生支持容器监控更适合物理机监控自动发现容器,更好的配置k8s-
告警方式可按照标签分组,收敛在次数上收敛告警收敛方式更多样化-
监控项值支持数字支持数字字符串-可做日志监控

 

用容器安装prometheus

安装

要先安装好docker环境才能进行下面操作

[root@docker1 ~]#  docker run -d -p 9090:9090 --name sc-prom-1   prom/prometheus
Unable to find image 'prom/prometheus:latest' locally
latest: Pulling from prom/prometheus
50783e0dfb64: Pull complete
daafb1bca260: Pull complete
bafa8e139cea: Pull complete
0d2e6df8577f: Pull complete
e3d4e14499bc: Pull complete
a3f71f7c721c: Pull complete
aca108eacfe0: Pull complete
b6aee8ea9d2f: Pull complete
950d9a06ee14: Pull complete
d009d09c576e: Pull complete
50100a62d658: Pull complete
34487f1a8146: Pull complete
Digest: sha256:aa1687dd552ed98df598cc0fed2effbc62a0f05236bc2253c65520ddd4f2afce
Status: Downloaded newer image for prom/prometheus:latest
70732675d454a3ee21cb037e78e19745a0ad05a394f89b874073e8664fe96c56
[root@docker1 ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS             NAMES
70732675d454   prom/prometheus        "/bin/prometheus --c…"   7 seconds ago   Up 5 seconds   0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   sc-prom-1
24252a8a11d0   wordpress:latest       "docker-entrypoint.s…"   25 hours ago    Up 3 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp           my_wordpress-wordpress-1
4f69af84c549   mariadb:10.6.4-focal   "docker-entrypoint.s…"   25 hours ago    Up 3 minutes   3306/tcp, 33060/tcp             my_wordpress-db-1

访问prometheus

http://host:9090
http://host:9090/metrics

 


实验

步骤:
    1.启动docker prometheus server的容器
    2.到另外一台linux server上安装exporter,并且启动
    3.在prometheus server的配置文件里添加被监控的服务器
    4.重启prometheus server对应的容器
    5.查看效果 

第一步:源码安装promehtues

先在电脑上下载好源码包

https://github.com/prometheus/prometheus/releases/download/v2.41.0/prometheus-2.41.0.linux-amd64.tar.gz

1.上传下载的源码包到linux服务器

[root@sc-prom ~]# mkdir /prom
[root@sc-prom ~]# cd /prom
[root@sc-prom prom]# ls
prometheus-2.34.0.linux-amd64.tar.gz

2.解压源码包

[root@sc-prom prom]# tar xf prometheus-2.34.0.linux-amd64.tar.gz
[root@sc-prom prom]# ls
prometheus-2.34.0.linux-amd64  prometheus-2.34.0.linux-amd64.tar.gz

3.修改解压后的压缩包名字

[root@sc-prom prom]# mv prometheus-2.34.0.linux-amd64 prometheus
[root@sc-prom prom]# ls
prometheus  prometheus-2.34.0.linux-amd64.tar.gz

4.临时和永久修改PATH变量,添加prometheus的路径

[root@sc-prom prometheus]# PATH=/prom/prometheus:$PATH
[root@web1 prometheus]# vim /etc/profile
PATH=/prom/prometheus:$PATH   #添加
[root@sc-prom prometheus]# cat /root/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
PATH=/prom/prometheus:$PATH   #添加

5.执行prometheus程序

[root@prometheus prometheus]# nohup prometheus  --config.file=/prom/prometheus/prometheus.yml &
[1] 8431

6.查看prometheus的进程

[root@prometheus prometheus]# ps aux|grep prome  
root       8431  4.5  2.4 782084 46204 pts/0    Sl   11:21   0:00 prometheus --config.file=/prom/prometheus/prometheus.yml
root       8439  0.0  0.0 112824   980 pts/0    S+   11:21   0:00 grep --color=auto prome

7.查看prometheus监听的端口号

[root@prometheus prometheus]# netstat -anplut|grep prome
tcp6       0      0 :::9090                 :::*                    LISTEN      8431/prometheus     
tcp6       0      0 ::1:9090                ::1:51738               ESTABLISHED 8431/prometheus     
tcp6       0      0 ::1:51738               ::1:9090                ESTABLISHED 8431/prometheus     

8.关闭服务器上的firewalld服务

[root@prometheus prometheus]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@prometheus prometheus]# systemctl disable firewalld 
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@web1 prometheus]# getenforce 
Disabled

9.把prometheus做成一个服务来进行管理,非常方便

[root@prometheus prometheus]# vim /usr/lib/systemd/system/prometheus.service 
[Unit]
Description=prometheus
[Service]
ExecStart=/prom/prometheus/prometheus --config.file=/prom/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

10.重新加载systemd相关的服务

[root@prometheus prometheus]# systemctl daemon-reload 重新加载systemd相关的服务

第一次因为是使用nohup 方式启动的prometheus,还是需要使用后kill 的方式杀死第一次启动的进程
后面可以使用service方式管理prometheus了

[root@prometheus prometheus]#  service prometheus stop
Redirecting to /bin/systemctl stop prometheus.service
[root@prometheus prometheus]# ps aux|grep prome
root       8431  0.2  3.2 782340 61472 pts/0    Sl   11:21   0:01 prometheus --config.file=/prom/prometheus/prometheus.yml
root       8650  0.0  0.0 112824   980 pts/0    S+   11:35   0:00 grep --color=auto prome
[root@prometheus prometheus]# kill -9 8431
[root@prometheus prometheus]# ps aux|grep prome
root       8652  0.0  0.0 112824   976 pts/0    R+   11:35   0:00 grep --color=auto prome
[1]+  已杀死               nohup prometheus --config.file=/prom/prometheus/prometheus.yml

[root@prometheus prometheus]#  service prometheus start
Redirecting to /bin/systemctl start prometheus.service
[root@prometheus prometheus]# ps aux|grep prome
root       8671 14.0  2.4 782084 45764 ?        Ssl  11:35   0:00 /prom/prometheus/prometheus --config.file=/prom/prometheus/prometheus.yml
root       8679  0.0  0.0 112824   980 pts/0    S+   11:35   0:00 grep --color=auto prome

11.设置Prometheus开机启动

[root@web1 system]# systemctl enable  prometheus
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.

12. 访问Prometheus 的web server
http:/host:9090
http://host:9090/metrics

第二步:在node节点服务器上安装exporter程序

1.下载node_exporter-1.4.0-rc.0.linux-amd64.tar.gz源码,上传到节点服务器上

2.解压

[root@node-liangrui ~]# ls
anaconda-ks.cfg  name_ip.txt                                  sanchuang1290
feng.txt         node_exporter-1.4.0-rc.0.linux-amd64.tar.gz  sanchuang1965
input.sh         onekey_install_changjinghu.sh                sanchuang{random.randint(1,10000)}
liang            sanchuang
[root@node-liangrui ~]# tar xf node_exporter-1.4.0-rc.0.linux-amd64.tar.gz
[root@node-liangrui ~]# ls
anaconda-ks.cfg  name_ip.txt                                  sanchuang
feng.txt         node_exporter-1.4.0-rc.0.linux-amd64         sanchuang1290
input.sh         node_exporter-1.4.0-rc.0.linux-amd64.tar.gz  sanchuang1965
liang            onekey_install_changjinghu.sh                sanchuang{random.randint(1,10000)}

3.单独存放到/node_exporter文件夹

[root@node-liangrui ~]# mv node_exporter-1.4.0-rc.0.linux-amd64 /node_exporter
[root@node-liangrui ~]#
[root@node-liangrui ~]# cd /node_exporter/
[root@node-liangrui node_exporter]# ls
LICENSE  node_exporter  NOTICE

4.修改PATH变量

[root@node-liangrui node_exporter]# PATH=/node_exporter/:$PATH
[root@node-liangrui node_exporter]# vim /root/.bashrc
PATH=/node_exporter/:$PATH #添加

5.执行node exporter 代理程序agent

[root@node-liangrui node_exporter]#nohup node_exporter --web.listen-address 0.0.0.0:8090  &

启动node_exporter 监听8090端口

具体的端口号,可以自己定义,只要不和其他的服务冲突就可以

[root@node-liangrui node_exporter]# ps aux|grep node
root      24958  0.6  0.6 716288  6476 pts/0    Sl+  11:45   0:00 node_exporter --web.listen-address 0.0.0.0:8090
root      24981  0.0  0.0 112824   988 pts/1    R+   11:46   0:00 grep --color=auto node
[root@node-liangrui node_exporter]# netstat -anplult|grep 8090
tcp6       0      0 :::8090                 :::*                    LISTEN      24958/node_exporter

访问node节点上的metrics
http://host:8090/metrics

第三步: 在prometheus server里添加我们在哪些机器里安装了exporter程序,这样就可以知道去哪里pull数据


利用webhook技术获得metrics指标数据 

1.在prometheus服务器上添加抓取数据的配置,添加node节点服务器,将抓取的数据存储到时序数据库里

[root@sc-prom prometheus]# vim prometheus.yml

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
      #添加下面的配置采集node-liangrui服务器的metrics
  - job_name: "node-liangrui"
    static_configs:
      - targets: ["192.168.1.194:8090"]
  - job_name: "LB1"
    static_configs:
      - targets: ["192.168.227.144:8090"]
  - job_name: "LB2"
    static_configs:
      - targets: ["192.168.227.148:8090"]

2.重启prometheus服务,需要先杀死原来启动的进程,然后再运行程序

[root@prometheus prometheus]# service  prometheus restart
Redirecting to /bin/systemctl restart prometheus.service

[root@sc-prom prometheus]# ps aux|grep prome
root       4355  0.1  3.2 782596 60924 ?        Sl   11:24   0:03 ./prometheus --config.file=prometheus.yml
root       4478  0.0  0.0 112824   988 pts/1    S+   11:56   0:00 grep --color=auto prome
[root@sc-prom prometheus]# kill -9 4355
[root@sc-prom prometheus]#  nohup prometheus --config.file=prometheus.yml &

3.登录prometheus查看是否添加targets成功

http://host:9090/targets


使用prometheus监控容器

需要一台docker 宿主机,docker 容器去安装prometheus

1.下载compose软件

curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

2.授予可执行权限

[root@ansible ~]# chmod +x /usr/local/bin/docker-compose 
[root@ansible ~]# which docker-compose
/usr/local/bin/docker-compose

3.编辑prometheus的配置文件

[root@harbor bin]# mkdir /prom_docker
[root@harbor bin]# cd /prom_docker/
[root@harbor prom_docker]#

新建prometheus.yml文件

[root@ansible sc]# vim prometheus.yml
scrape_configs:
- job_name: cadvisor
  scrape_interval: 5s
  static_configs:
  - targets:
    - cadvisor:8080

新建docker-compose.yaml文件

[root@harbor prom_docker]# vim docker-compose.yaml
[root@harbor prom_docker]# cat docker-compose.yaml
version: '3.2'
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
    - 9090:9090
    command:
    - --config.file=/etc/prometheus/prometheus.yml
    volumes:
    - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
    depends_on:
    - cadvisor
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    container_name: cadvisor
    ports:
    - 8080:8080
    volumes:
    - /:/rootfs:ro
    - /var/run:/var/run:rw
    - /sys:/sys:ro
    - /var/lib/docker/:/var/lib/docker:ro
    depends_on:
    - redis
  redis:
    image: redis:latest
    container_name: redis
    ports:
    - 6379:6379

4.上传cadvisor.tar镜像文件到宿主机里,可以到香港的服务器里去下载镜像

docker pull gcr.io/cadvisor/cadvisor:latest
[root@harbor prom_docker]# ls
cadvisor.tar  docker-compose.yaml  prometheus.yml
[root@harbor prom_docker]#
[root@harbor prom_docker]# docker load < cadvisor.tar
ace0eda3e3be: Loading layer [==================================================>]  5.843MB/5.843MB
33bb68b99ee4: Loading layer [==================================================>]  102.4MB/102.4MB
d3174d703c76: Loading layer [==================================================>]  13.25MB/13.25MB
8b7599e512b6: Loading layer [==================================================>]  44.19MB/44.19MB
Loaded image: gcr.io/cadvisor/cadvisor:latest
[root@harbor prom_docker]#
[root@harbor prom_docker]# docker images |grep gcr
gcr.io/cadvisor/cadvisor        latest         68c29634fe49   21 months ago   163MB

5.使用docker compose启动容器

[root@harbor prom_docker]# docker-compose up -d
[+] Running 3/3
 ⠿ Container redis       Started                                                                                        1.0s
 ⠿ Container cadvisor    Started                                                                                        1.9s
 ⠿ Container prometheus  Started                                                                                        2.7s
[root@harbor prom_docker]# docker-compose up -d
[+] Running 3/3
 ⠿ Container redis       Running                                                                                        0.0s
 ⠿ Container cadvisor    Running                                                                                        0.0s
 ⠿ Container prometheus  Started                                                                                        0.8s
[root@harbor prom_docker]# docker-compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
cadvisor            "/usr/bin/cadvisor -…"   cadvisor            running (healthy)   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
prometheus          "/bin/prometheus --c…"   prometheus          running             0.0.0.0:9090->9090/tcp, :::9090->9090/tcp
redis               "docker-entrypoint.s…"   redis               running             0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

6.访问cadvisor和prometheus

http://host:8080/   访问cadvisor 
http://host:9090/  访问prometheus

7.停止docker-compose

[root@ansible sc]# docker-compose stop  停止容器
Stopping prometheus ... done
Stopping cadvisor   ... done
Stopping redis      ... done
[root@ansible sc]#docker-compose down  --》 down               Stop and remove resources 停止并且删除容器

8.安装grafana ,使用能显示docker容器的模板

使用容器安装grafana

[root@harbor prom_docker]# docker run -d -p 3001:3000 --name sc-grafana-1 grafana/grafana

导入下面的模板
https://grafana.com/grafana/dashboards/11600-docker-container/
11600
https://grafana.com/grafana/dashboards/10566-docker-and-os-metrics/


prometheus 监控MySQL服务器

在一台服务器上使用脚本或者yum安装mysqld,

然后安装mysqld_exporter,然后在prometheus里添加mysqld这台被监控的服务器

在MySQL服务器上安装mysqld_exporter 让prometheus监控数据

1.准备环境,安装好mysql程序,启动起来

[root@sc-mysqld ~]# ls
anaconda-ks.cfg  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz  onekey_install_mysql_binary_v3.sh  simkai.ttf
[root@sc-mysqld ~]# ps aux|grep mysqld
root     24617  0.0  0.1  11824  1604 pts/0    S    09:09   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/sc-mysqld.pid
mysql    24771  0.0 20.4 1544136 205380 pts/0  Sl   09:09   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=sc-mysqld.err --open-files-limit=8192 --pid-file=/data/mysql/sc-mysql.pid --socket=/data/mysql/mysql.sock --port=3306
root     28383  0.0  0.0 112824   988 pts/0    S+   10:37   0:00 grep --color=auto mysqld
[root@sc-mysqld ~]# netstat -anplut|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      24771/mysqld        
[root@sc-mysqld ~]# mysql -uroot -pSanchuang123#
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@(none) 10:38  mysql>

2.上传prometheus的agent软件mysqld_exporter到被监控的mysql服务器

exporter 是一个采集数据的软件工具,需要人去开发的--》官方给我们提供了---》未来可以自己去开发exporter--》go语言

[root@sc-mysqld ~]# cd /sc
[root@sc-mysqld sc]# ls
mysqld_exporter-0.14.0.linux-amd64.tar.gz   --》需要自己上传

3.解压文件

[root@sc-mysqld sc]# tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz   解压文件
[root@sc-mysqld sc]# ls
mysqld_exporter-0.14.0.linux-amd64  mysqld_exporter-0.14.0.linux-amd64.tar.gz
[root@sc-mysqld sc]# cd mysqld_exporter-0.14.0.linux-amd64
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# ls
LICENSE  mysqld_exporter  NOTICE

4.修改环境变量

[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# PATH=/sc/mysqld_exporter-0.14.0.linux-amd64:$PATH
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# which mysqld_exporter 
/sc/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter

5.在MySQL创建一个用户,授权这个用户让mysqld_exporter使用这个用户去连接mysqld进程,获取mysql里的数据

root@(none) 10:52  mysql>grant select , replication client,process on *.* to 'prom'@'localhost' identified by 'sc123456';
Query OK, 0 rows affected, 1 warning (0.14 sec)

root@(none) 10:53  mysql>exit

6.新建一个配置文件给mysqld_exporter,里面的用户名和密码是上面新建的

[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# cat my.cnf 
[client]
user=prom
password=sc123456
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# 

7.启动mysqld_exporter程序,使用指定的配置文件

[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# nohup mysqld_exporter  --config.my-cnf=/sc/mysqld_exporter-0.14.0.linux-amd64/my.cnf &[1] 28448
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# ps aux|grep mysqld
root     24617  0.0  0.1  11824  1604 pts/0    S    09:09   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/sc-mysqld.pid
mysql    24771  0.0 20.4 1544136 205376 pts/0  Sl   09:09   0:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=sc-mysqld.err --open-files-limit=8192 --pid-file=/data/mysql/sc-mysql.pid --socket=/data/mysql/mysql.sock --port=3306
root     28448  0.6  0.5 712084  5064 pts/0    Sl   10:57   0:00 mysqld_exporter --config.my-cnf=/sc/mysqld_exporter-0.14.0.linux-amd64/my.cnf
root     28452  0.0  0.0 112824   988 pts/0    S+   10:57   0:00 grep --color=auto mysqld
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# 
[root@sc-mysqld mysqld_exporter-0.14.0.linux-amd64]# netstat -anplut|grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      24771/mysqld        
tcp6       0      0 :::9104                 :::*                    LISTEN      28448/mysqld_export 

9104 是mysqld_exporter的端口号

 

8.在prometheus server里添加被监控主机

在server上操作

http://host:9090/targets

添加下面的配置  mysql-server的配置

[root@ansible sc]# pwd
/sc
[root@ansible sc]# cat prometheus.yml 
scrape_configs:
- job_name: cadvisor
  scrape_interval: 5s
  static_configs:
  - targets:
    - cadvisor:8080
- job_name: load balancer
  scrape_interval: 5s
  static_configs:
  - targets:
    - 192.168.2.130:9100
- job_name: mysql-server                     
  scrape_interval: 5s
  static_configs:
  - targets:
    - 192.168.2.104:9104

9.重启容器prometheus

[root@ansible ~]# pwd
/root
[root@ansible ~]# docker-compose restart  --》命令必须在有docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml文件的目录下使用
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml
        
[root@ansible ~]# cd /sc
[root@ansible sc]# ls
cadvisor.tar  docker-compose.yml  prometheus.yml
[root@ansible sc]# docker-compose  restart 重启compose管理的容器
Restarting prometheus ... done
Restarting cadvisor   ... done
Restarting redis      ... done
[root@ansible sc]# 

10.访问prometheus,点status里的target可以看到我们的mysql server

http://host:9090/targets#pool-mysql-server


Grafana出图工具

简介

grafana 是一款采用 go 语言编写的开源应用,主要用于大规模指标数据的可视化展现,是网络架构和应用分析中最流行的时序数据展示工具,目前已经支持绝大部分常用的时序数据库。最好的参考资料就是官网(http://docs.grafana.org/) 

grafana 是非常专业的出图软件: 专门从别的数据库里抽取数据,然后通过图形展示工具,显示出来

美观、强大的可视化监控指标展示工具

实验

grafana 和prometheus server安装在一台服务器上

第一步:安装

1.编辑grafana.repo文件在/etc/yum.repos.d目录下

[root@prometheus ~]# cd "/etc/yum.repos.d"
[root@prometheus yum.repos.d]# vim grafana.repo 
[grafana]
name=grafana
baseurl=https://packages.grafana.com/enterprise/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt

2.安装grafana

[root@prometheus yum.repos.d]# yum install grafana  -y

因为下载速度非常慢,导致下载失败,需要很长的时间

另一种方法:

1.先去官方网站下载

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.1.2-1.x86_64.rpm

2.安装

[root@sc-prom grafana]# ls
grafana-enterprise-8.4.5-1.x86_64.rpm
[root@sc-prom grafana]# yum install grafana-enterprise-8.4.5-1.x86_64.rpm -y

3.启动grafana

[root@sc-prom grafana]# service grafana-server start
Starting grafana-server (via systemctl):                   [  确定  ]
[root@sc-prom grafana]#

4.设置grafana开机启动

[root@prometheus grafana]# systemctl enable grafana-server  
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
[root@prometheus grafana]# 

监听的端口号是3000

[root@sc-prom grafana]# netstat -anplut|grep grafana
tcp        0      0 192.168.1.137:52062     34.120.177.193:443      ESTABLISHED 5413/grafana-server
tcp6       0      0 :::3000                 :::*                    LISTEN      5413/grafana-server

5.登录,在浏览器里登录

http://host:3000/
默认的用户名和密码是
用户名admin
密码admin

garafana页面操作 

1.先配置prometheus的数据源

2.导入grafana的模板
    https://grafana.com/grafana/dashboards
步骤:
    1.需要知道哪些模板可以使用,可以去官方找,也可以百度
    1-node-exporter-for-prometheus-dashboard-cn-v20200628_rev1.json
    2.导入模板

创建文件夹,存放模板 sanchuang
这2个模板非常好用,推荐使用
1860
8919

排错思路:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值