热门标签 | 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



推荐阅读
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • Python第三方库安装的多种途径及注意事项
    本文详细介绍了Python第三方库的几种常见安装方法,包括使用pip命令、集成开发环境(如Anaconda)以及手动文件安装,并提供了每种方法的具体操作步骤和适用场景。 ... [详细]
  • Nginx 反向代理与负载均衡实验
    本实验旨在通过配置 Nginx 实现反向代理和负载均衡,确保从北京本地代理服务器访问上海的 Web 服务器时,能够依次显示红、黄、绿三种颜色页面以验证负载均衡效果。 ... [详细]
  • 全能终端工具推荐:高效、免费、易用
    介绍一款备受好评的全能型终端工具——MobaXterm,它不仅功能强大,而且完全免费,适合各类用户使用。 ... [详细]
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 解决PHP与MySQL连接时出现500错误的方法
    本文详细探讨了当使用PHP连接MySQL数据库时遇到500内部服务器错误的多种解决方案,提供了详尽的操作步骤和专业建议。无论是初学者还是有经验的开发者,都能从中受益。 ... [详细]
  • 使用Vultr云服务器和Namesilo域名搭建个人网站
    本文详细介绍了如何通过Vultr云服务器和Namesilo域名搭建一个功能齐全的个人网站,包括购买、配置服务器以及绑定域名的具体步骤。文章还提供了详细的命令行操作指南,帮助读者顺利完成建站过程。 ... [详细]
  • 本文介绍如何使用Python进行文本处理,包括分词和生成词云图。通过整合多个文本文件、去除停用词并生成词云图,展示文本数据的可视化分析方法。 ... [详细]
  • 阿里云ecs怎么配置php环境,阿里云ecs配置选择 ... [详细]
  • 全面解析运维监控:白盒与黑盒监控及四大黄金指标
    本文深入探讨了白盒和黑盒监控的概念,以及它们在系统监控中的应用。通过详细分析基础监控和业务监控的不同采集方法,结合四个黄金指标的解读,帮助读者更好地理解和实施有效的监控策略。 ... [详细]
  • 深入解析Serverless架构模式
    本文将详细介绍Serverless架构模式的核心概念、工作原理及其优势。通过对比传统架构,探讨Serverless如何简化应用开发与运维流程,并介绍当前主流的Serverless平台。 ... [详细]
  • 本文详细介绍了如何在云服务器上配置Nginx、Tomcat、JDK和MySQL。涵盖从下载、安装到配置的完整步骤,帮助读者快速搭建Java Web开发环境。 ... [详细]
  • 本文档汇总了Python编程的基础与高级面试题目,涵盖语言特性、数据结构、算法以及Web开发等多个方面,旨在帮助开发者全面掌握Python核心知识。 ... [详细]
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社区 版权所有