一、部署prometheus及监控仪表盘
简介
Prometheus是开源监控报警系统和时序列数据库(TSDB)。
Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控,输出被监控组件信息的HTTP接口被叫做exporter。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系统信息(包括磁盘、内存、CPU、网络等等)。
1 基础环境
环境/组件 版本 下载地址 操作系统 CentOS 7.3 http://archive.kernel.org/centos-vault/7.3.1611/isos/x86_64/CentOS-7-x86_64-DVD-1611.iso
Prometheus 2.6.0 Download | Prometheus
go 1.12.3 https://mirrors.xlhy1.com/source-code/go1.12.3.linux-amd64.tar.gz
Grafana 5.4.2 https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm
2 安装go
2.1 安装
rz #上传go1.12.3.linux-amd64.tar.gz压缩包
tar -C /usr/local/ -xzvf go1.12.3.linux-amd64.tar.gz
2.2 配置环境变量
vim /etc/profile #打开配置文件
export PATH=$PATH:/usr/local/go/bin #添加内容
source /etc/profile #刷新环境变量配置文件
2.3 验证版本
go version
3. 安装Prometheus
3.1 安装
rz #上传 prometheus-2.6.0.linux-amd64.tar.gz
tar -C /usr/local/ -xzvf prometheus-2.6.0.linux-amd64.tar.gz #解压缩到指定目录
cd /usr/local/ #进入目录
ln -sv /usr/local/prometheus-2.6.0.linux-amd64/ /usr/local/prometheus #创建软连接
3.2 修改prometheus配置文件
vim /usr/local/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"
- "/usr/local/prometheus/rules/node.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'
static_configs:
- targets: ['192.168.0.4:9090']
3.3 启动prometheus
nohup /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &
3.4 访问
浏览器访问:ip:9090/targets
注意:如果是云服务器,配置文件中的ip为ifconfig命令查到的IP,浏览器访问的ip为公网ip
4. 安装Grafana
4.1 安装
rz #上传 grafana-5.4.2-1.x86_64.rpm
rpm -ivh --nodeps grafana-5.4.2-1.x86_64.rpm #编译安装
4.2 可能在安装grafana时有依赖库,需要搭建个本地yum源安装fontconfig、urw-fonts
yum install fontconfig
yum install urw-fonts
4.3 启动Grafana
systemctl daemon-reload
systemctl enable grafana-server.service #设为开机自启动
systemctl start grafana-server.service
4.4 访问grafana
浏览器访问IP:3000端口,即可打开grafana页面,默认用户名密码都是admin,初次登录会要求修改默认的登录密码
4.5 点击主界面的“Add data source”
4.6 选择Prometheus
4.7 Dashboards页面选择“Prometheus 2.0 Stats”
4.8 Settings页面填写普罗米修斯地址并保存
4.9 切换到我们刚才添加的“Prometheus 2.0 Stats”即可看到整个监控页面
二、Prometheus + Grafana监控之主机性能
监控linux机器(node-exporter) https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
1.被监控的机器安装node-exporter
1.1 解压安装
rz #上传node_exporter-0.17.0.linux-amd64.tar.gz
tar -xvf -C /usr/local/ node_exporter-0.17.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv node_exporter-0.17.0.linux-amd64 node_exporter
1.2 启动node-exporter
nohup /usr/local/node_exporter/node_exporter --web.listen-address=":9100" &
启动成功后,可以访问 http://1.117.225.137:9100/metrics(IP和端口要改成相应环境的,公网ip)
2.prometheus添加监控项
2.1 修改prometheus配置文件
vim /usr/local/prometheus/prometheus.yml #打开配置文件
#默认node-exporter端口为9100
添加配置文件内容如下:
# 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.
# 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"
- "/usr/local/prometheus/rules/node.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'
static_configs:
- targets: ['192.168.0.4:9090']
- job_name: 'prometheus-yjsx'
static_configs:
- targets: ['1.117.225.137:9100'] #公网ip
2.2 重启普罗米修斯
[root@xplinux ~]# ps axu | grep prometheus #查看进程
root 6969 0.0 4.1 146340 41780 pts/1 Sl 15:15 0:01 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
root 14193 0.0 0.0 112708 980 pts/1 R+ 16:35 0:00 grep --color=auto prometheus
kill -9 6969 #结束进程
nohup /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml & #启动
3. Grafana导入画好的dashboard
Upload上传node-exporter_rev5.json文件(也可以直接使用官方模板,需要此node-exporter_rev5.json文件的可以私信我获取)
修改名字,选择我们前文创建好的数据源,点击导入即可
如果没有任何显示,是grafana缺少相关显示需要用到的插件piechart
grafana的默认插件目录是,可以将下载好的插件解压到这个目录,重启grafana即可 piechart插件下载链接:
cd /var/lib/grafana/plugins #进入目录
rz #上传插件 grafana-piechart-panel-5f249d5.zip
unzip grafana-piechart-panel-5f249d5.zip #解压缩
service grafana-server restart #重启Grafana
ls /usr/sbin/grafana-cli plugins #查看已安装插件
再刷新grafana页面,即可看到我们刚才设置好的node监控
三、Prometheus + Grafana监控之Kafka
监控kafka(kafka_exporter)
从 GitHub - danielqsj/kafka_exporter: Kafka exporter for Prometheus 下载
1. 安装kafka_exporter
注:1个kafka集群只需要1个exporter,在集群上的任意1台服务器部署。
kafka_exporter-1.2.0.linux-amd64.tar下载地址:忘求了,家人们可以百度找找
rz #上传kafka_exporter-1.2.0.linux-amd64.tar
tar -xvf kafka_exporter-1.2.0.linux-amd64.tar -C /usr/local/
cd /usr/local/
ln -sv kafka_exporter-1.2.0.linux-amd64 kafka_exporter #创建软连接
cd kafka_exporter
1.1 启动kafka_exporter
nohup ./kafka_exporter --kafka.server=172.16.1.224:9092 &
启动成功后,可以访问 http://172.16.1.224:9308/metrics/ ,(IP和端口要改成相应环境的)
看抓取的信息如下:
2. 修改Prometheus配置
2.1 修改prometheus组件的prometheus.yml加入kafka监控
vim /usr/local/prometheus-2.27.1/prometheus.yml #打开prometheus配置文件
添加内容如下:图片已丢失!!!
2.2 启动验证
先kill掉Prometheus进程,用以下命令重启它,然后查看targets:
cd /usr/local/prometheus
nohup ./prometheus --config.file=prometheus.yml &
State=UP,说明成功
3. Grafana配置
导入仪表盘模板
通过浏览器访问:IP地址:3000
图片已丢失!!!
👑👑👑结束语👑👑👑