Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。随着发展,愈来愈多公司和组织接受采用Prometheus,社区也十分活跃,他们便将它独立成开源项目,而且有公司来运做。google SRE的书内也曾提到跟他们BorgMon监控系统类似的实现是Prometheus。如今最多见的Kubernetes容器管理系统中,一般会搭配Prometheus进行监控。相似的产品还有influxdb, 若是用于监控用途是一般会配合grafana进行数据展现和报警。node
更详细的说明和帮助,能够参考prometheus官网: https://prometheus.io/mysql
如官网描述的同样,prometheus主要是在当咱们须要单纯记录以时间进行分片的数据时使用,不管是系统级别的监控,或是咱们的高度动态的服务架构。针对微服务场景,prometheus也能很好的支持多为数据的收集和查询。linux
固然,prometheus也有局限性,若是咱们的监控或系统,须要百分百的准确性就不太适合。git
须要了解更多关于prometheus的知识,强烈建议好好阅读官网的文档。github
本文的重点是如何在CentOS 7中安装和配置Prometheus,咱们要按生产级的要求来部署Prometheus。如下就开始咱们的部署工做, 按步骤进行操做。web
以root或具备sudo权限的用户登陆CentOS系统,执行如下命令升级系统(非必要):sql
yum update -y
vim /etc/sysconfig/selinux
将SELINUX=enforcing
改成ELINUX=disabled
, 而后重启系统reboot
。shell
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cd /data/software/ wget https://github.com/prometheus/prometheus/releases/download/v2.11.1/prometheus-2.11.1.linux-amd64.tar.gz tar -zxvf prometheus-2.11.1.linux-amd64.tar.gz
useradd --no-create-home --shell /bin/false prometheus
mkdir /etc/prometheus mkdir /var/lib/prometheus
chown prometheus:prometheus /etc/prometheus chown prometheus:prometheus /var/lib/prometheus
cd /data/software/prometheus-2.11.1.linux-amd64 cp prometheus /usr/local/bin/ cp promtool /usr/local/bin/ # 设置权限(更改全部者) chown prometheus:prometheus /usr/local/bin/prometheus chown prometheus:prometheus /usr/local/bin/promtool cp -r consoles /etc/prometheus cp -r console_libraries /etc/prometheus chown -R prometheus:prometheus /etc/prometheus/consoles chown -R prometheus:prometheus /etc/prometheus/console_libraries
mkdir /etc/prometheus/ cp prometheus.xml /etc/prometheus/ or vim /etc/systemd/system/prometheus.service chown prometheus:prometheus /etc/prometheus/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']
vi /etc/systemd/system/prometheus.service
[Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries [Install] WantedBy=multi-user.target
# reload服务配置 systemctl daemon-reload # 启动服务 systemctl start prometheus # 查看服务状态 systemctl status prometheus
若是一切正常应该能够看到以下输出, 说明服务以及正常启动。vim
[root@ ~]# systemctl status prometheus ● prometheus.service - Prometheus Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor prese t: disabled) Active: active (running) since Mon 2019-07-15 17:43:53 CST; 1h 34min ago Main PID: 22807 (prometheus) CGroup: /system.slice/prometheus.service └─22807 /usr/local/bin/prometheus --config.file /etc/prometheus/pr... Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.660Z...)" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.660Z...)" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.661Z...)" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.663Z...." Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...IC Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...d" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...ml Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.695Z...90 Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.698Z...ml Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.698Z...." Hint: Some lines were ellipsized, use -l to show in full.
systemctl enable prometheus
也能够试一下重启系统,看服务会不会自动开启。
由于咱们的配置文件中配置的端口为9090
, 因此若是你的系统有设置防火墙,这须要将这个端口开放出来。
firewall-cmd --zone=public --add-port=9090/tcp --permanent # 重启防火墙服务 systemctl reload firewalld
由于我是在虚拟机里安装的,直接在宿主机浏览器中输入: http://192.168.56.2:9090
,若是出现如下页面这表示服务正常开启。
exporter是prometheus用来收集数据的客户端工具,在prometheus的官网上https://prometheus.io/download/ 能够下载到不少服务的exporter采集器。如下是咱们常常用到的一些采集器:
Name | Description | URL |
---|---|---|
Node Exporter | 针对服务器进行监控的探测器(探针) | https://github.com/prometheus/node_exporter |
Alert Manager | 报警相关 | https://github.com/prometheus/alertmanager |
Mysql Exporter | MySQL服务监控相关的探测器 | https://github.com/prometheus/mysqld_exporter |
Blackbox Exporter | 黑盒监控解决方案,其容许用户经过:HTTP、HTTPS、DNS、TCP以及ICMP的方式对网络进行探测 | https://github.com/prometheus/blackbox_exporter |
jmx_exporter | JMX服务的探测器 | https://github.com/prometheus/jmx_exporter |
具体的能够到咱们使用时再行在https://prometheus.io/download/或github中寻找须要的插件进行安装配置,这里咱们就简单介绍几个经常使用的插件的安装和使用。
node_exporter是一个服务器端agent,负责采集服务器基础监控项。下面咱们就来一步一步安装node_exporter收集所在服务器的性能。
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz tar -zvxf node_exporter-0.18.1.linux-amd64.tar.gz
useradd -rs /bin/false nodeusr
cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/
vim /etc/systemd/system/node_exporter.service
内容以下:
[Unit] Description=Node Exporter After=network.target [Service] User=nodeusr Group=nodeusr Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target
systemctl daemon-reload systemctl start node_exporter
查看服务状态,若是正常的话会显示以下:
[root@ software]# systemctl status node_exporter ● node_exporter.service - Node Exporter Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2019-07-15 20:10:48 CST; 20min ago Main PID: 30530 (node_exporter) CGroup: /system.slice/node_exporter.service └─30530 /usr/local/bin/node_exporter Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - sockstat" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - stat" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - textfile" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - time" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - timex" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - uname" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - vmstat" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - xfs" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - zfs" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg="Listening on :9100" source="node_exporter.go:170"
systemctl enable node_exporter
若是防火墙服务有开启的话,就须要开通端口: 9100
firewall-cmd --zone=public --add-port=9100/tcp --permanent systemctl restart firewalld
在宿主机浏览器输入http://192.168.56.2:9100/metrics
vim /etc/prometheus/prometheus.yml
在prometheus.xml中的scrape配置项中加入如下内容:
- job_name: 'node_exporter_centos' scrape_interval: 5s static_configs: - targets: ['192.168.56.2:9100']
最终的配置文件以下:
而后从新启动prometheus服务。
systemctl restart prometheus
在宿主机的浏览器中输入prometheus的地址, http://192.168.56.2:9090/graph
export VERSION=0.18.0 curl -LO https://github.com/prometheus/alertmanager/releases/download/v$VERSION/alertmanager-$VERSION.darwin-amd64.tar.gz tar xvf alertmanager-$VERSION.darwin-amd64.tar.gz