热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PrometheusGrafana实现对LinuxKubernetesNginx的监控系统

2019独角兽企业重金招聘Python工程师标准用Prometheus配合Grafana实现多用途的服务监控系统。一.Prometheus简介Prometheus是一个开源监

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

用Prometheus配合Grafana实现多用途的服务监控系统。

一. Prometheus简介 

Prometheus是一个开源监控系统,它前身是SoundCloud的警告工具包。从2012年开始,许多公司和组织开始使用Prometheus。该项目的开发人员和用户社区非常活跃,越来越多的开发人员和用户参与到该项目中。

具体介绍及架构信息参考Prometheus官网:https://prometheus.io

二. Grafana介绍

grafana是用于可视化大型测量数据的开源程序,他提供了强大和优雅的方式去创建、共享、浏览数据。dashboard中显示了你不同metric数据源中的数据。

具体参考Grafana官网:https://grafana.com

 

三.实现实例

1.监控Openresty(Nginx)

1.1下载安装Openresty及nginx-module-vts监控模块

#安装编译环境
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel#下载openresty和nginx-module-vts监控模块
cd /data/server
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
git clone https://github.com/vozlt/nginx-module-vts.git#解压openresty
tar -zxf openresty-1.13.6.2.tar.gz#添加模块编译
./configure --add-module=/data/server/nginx-module-vts
make && make install

1.2 修改Openresty配置

不指定安装路径的话Openresty默认安装在/usr/local/openresty

进入目录/usr/local/openresty/nginx/conf 修改配置文件

cd /usr/local/openresty/nginx/conf
#先备份
cp nginx.conf nginx.conf.bak
vim nginx.conf

变更配置如下:

主要两个配置,1.指定时间同步,输出日志格式。2.添加http访问路由

user nobody;
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;vhost_traffic_status_zone;log_format main '{ "@timestamp": "$time_local", ''"@fields": { ''"uri":"$request_uri",''"url":"$uri",''"upstream_addr":"$upstream_addr",''"remote_addr": "$remote_addr", ''"remote_user": "$remote_user", ''"body_bytes_sent": "$body_bytes_sent", ''"host":"$host",''"server_addr":"$server_addr",''"request_time": "$request_time", ''"request_time":"$request_time",''"status":"$status",''"request": "$request", ''"request_method": "$request_method", ''"size":$body_bytes_sent,''"upstream_time":"$upstream_response_time"''"http_referrer": "$http_referer", ''"body_bytes_sent":"$body_bytes_sent", ''"http_x_forwarded_for": "$http_x_forwarded_for", ''"http_user_agent": "$http_user_agent" } }';sendfile on;keepalive_timeout 65;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 3;gzip_types text/plain text/css application/x-Javascript text/xml application/xml application/xml+rss text/Javascript application/json application/Javascript;gzip_vary on;proxy_http_version 1.1;proxy_set_header Connection "";server {listen 80;server_name localhost;location / {root html;index index.html index.htm;}location /status {vhost_traffic_status_display;vhost_traffic_status_display_format html;}}
}

测试配置文件是否有误 

./sbin/nginx -t

通过测试后启动Openresty

./sbin/nginx

访问地址http://[ip:port]/status

可以看到如下信息:

1.3配置监控转发模块

这里先使用一个Github上已经开发好的第三方监控模块nginx-vts-exporter

下载地址:https://github.com/hnlq715/nginx-vts-exporter/releases

cd /data/server
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
tar -zxf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
mv nginx-vts-exporter-0.10.3.linux-amd64 nginx-vts-exporter

把nginx-vts-exporter 写成服务(以Centos7.X为例):

在/usr/lib/systemd/system/下创建启动文件

vim /usr/lib/systemd/system/nginx-vts-exporter.service

配置启动可执行文件和参数

[Unit]
Description=nginx-vts-exporter
After=network.target[Service]
Type=simple
ExecStart=/data/server/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri=http://127.0.0.1/status/format/json
Restart=on-failure[Install]
WantedBy=multi-user.target

生效配置文件并启动nginx-vts-exporter

#启动服务,nginx-vts-exporter 默认监听端口为 9913
systemctl enable nginx-vts-exporter
systemctl start nginx-vts-exporter
systemctl status nginx-vts-exporter

1.4 配置Prometheus

配置文件路径:/data/server/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=` to any timeseries scraped from this config.- job_name: 'prometheus'# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ['localhost:9090']- job_name: 'openresty-master'static_configs:- targets: ['localhost:9913']

配置服务来启动

vim /usr/lib/systemd/system/prometheus.service

写入如下内容:

[Unit]
Description=prometheus
After=network.target[Service]
Type=simple
ExecStart=/data/server/prometheus/prometheus --config.file=/data/server/prometheus/prometheus.yml
Restart=on-failure[Install]
WantedBy=multi-user.target

启动prometheus服务

#启动prometheus服务
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus

1.5 安装配置Grafana

下载地址:https://grafana.com/grafana/download?platform=linux

按上述页面说明安装

wget https://dl.grafana.com/oss/release/grafana-6.1.3-1.x86_64.rpm
yum localinstall grafana-6.1.3-1.x86_64.rpm

我比较习惯先修改一下登录密码(默认账号密码 admin admin)

先看一下安装位置

whereis grafana

发现安装在/etc/grafana 去/etc/grafana下编辑grafana.ini文件

修改[security]节点下的password为你期望的值

新版本 安装后输入admin默认密码第一次进入会提示修改密码

启动Grafana服务并查看状态

systemctl start grafana-server
systemctl status grafana-server

访问页面http://[ip]:3000

进入后点击设置data sources  add data sources

选择Prometheus填写如下信息

使用Prometheus 2.0 Stats作为Dashboard效果如下:

 

2.监控Linux主机

2.1监控代理Node exporter 安装

首先在prometheus官网下载 node_exporter。官网地址为: https://prometheus.io/docs/introduction/overview/

#下载 解压
wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
tar -zxf node_exporter-0.17.0.linux-amd64.tar.gz
mv node_exporter-0.17.0.linux-amd64.tar.gz node_exporter
cd node_exporter
#后台启动
nohup ./node_exporter &

服务模式启动:

[Unit]
Description=node-exporter
After=network.target[Service]
Type=simple
ExecStart=/data/monitor/node_exporter/node_exporter
Restart=on-failure[Install]
WantedBy=multi-user.target

 

*查询 node_exporter进程

#默认监听9100
netstat -ntlp|grep 9100
netstat -anp | grep 9100

2.2 prometheus添加代理配置

vim prometheus.yml

添加如下配置:

- job_name: 'tx-server'static_configs:- targets: ['94.191.16.32:9100']

重启prometheus

systemctl restart prometheus

 

3.监控Kubernetes(k8s)

待补充...

 

 

附录:

1.Grafana安装插件

grafana-cli plugins install [michaeldmoore-annunciator-panel]
service grafana-server restart

2.command

#启动nginx服务
systemctl start nginx.service
#设置开机自启动
systemctl enable nginx.service
#停止开机自启动
systemctl disable nginx.service
#查看服务当前状态
systemctl status nginx.service
#重新启动服务
systemctl restart nginx.service
#查看所有已启动的服务
systemctl list-units --type=service

 


转:https://my.oschina.net/mrpei123/blog/3035664



推荐阅读
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文详细介绍了git常用命令及其操作方法,包括查看、添加、提交、删除、找回等操作,以及如何重置修改文件、抛弃工作区修改、将工作文件提交到本地暂存区、从版本库中删除文件等。同时还介绍了如何从暂存区恢复到工作文件、恢复最近一次提交过的状态,以及如何合并多个操作等。 ... [详细]
  • CentOS7.8下编译muduo库找不到Boost库报错的解决方法
    本文介绍了在CentOS7.8下编译muduo库时出现找不到Boost库报错的问题,并提供了解决方法。文章详细介绍了从Github上下载muduo和muduo-tutorial源代码的步骤,并指导如何编译muduo库。最后,作者提供了陈硕老师的Github链接和muduo库的简介。 ... [详细]
  • 寻求更强大的身份和访问管理(IAM)平台的企业正在转向云,并接受身份即服务(IDaaS)的灵活性。要为IAM选择正确的场外解决方案,业务管理人员和IT专业人员必须在实施之前评估安全 ... [详细]
  • 【技术分享】一个 ELF 蠕虫分析
    【技术分享】一个 ELF 蠕虫分析 ... [详细]
  • 交换机配置:intg100unshintvlani1ipadd192.168.56.177qstelseuser-iv4authaaaproinsshupl3qsshuserpyt ... [详细]
  • MySQL5.6.40在CentOS764下安装过程 ... [详细]
  • npminstall-Dbabelcorebabelpreset-envbabelplugin-transform-runtimebabelpolyfillbabel-loader ... [详细]
  • linux下编译安装lnmp
    2019独角兽企业重金招聘Python工程师标准#######################安装依赖#####################安装必要的包:y ... [详细]
  • 使用gitolite搭建一个私有的git服务器,来管理git仓库。有了它,就可以跟小伙伴们愉快地进行远程协作啦。今天又折腾了一遍,在这里把几个关键的步骤记下来,方便以后查阅。准备工 ... [详细]
  • Nextcloudsnap一键安装包: https:github.comextcloudextcloud-snap建议安装Ubuntu系统,因为官方一键安装包用的是Snap,Cen ... [详细]
  • 一、设置时区方法一:使用setup工具setup选择Timezoneconfiguration选择AsiaShanghai空格键勾选上System ... [详细]
  • VS用c语言连接mysql,c语言连接mysql完整演示
    #include#includeintmain(){MYSQL*conn;创建一个指向mysql数据类型的指针connmysql_init(NULL);mysql的初始化if(!c ... [详细]
author-avatar
悸末丶C彼岸花开
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有