首页 > 其他分享 >58、K8S-监控机制-Prometheus-自定义metrics

58、K8S-监控机制-Prometheus-自定义metrics

时间:2023-04-11 12:44:42浏览次数:56  
标签:58 自定义 python request metrics process prometheus gc total

Kubernetes学习目录

1、安装python环境

1.1、下载python软件

wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz

1.2、安装依赖包

yum install gcc gcc-c++ glibc-devel glibc zlib-devel zlib openssl-devel openssl sqlite-devel readline-devel bzip2-devel libffi-devel -y

1.3、编译安装软件

tar xvf Python-3.9.16.tgz && cd Python-3.9.16 && ./configure --prefix=/usr/local/Python-3.9.16 --with-ssl && make && make install
ln -s /usr/local/Python-3.9.16 /usr/local/python3

1.4、配置Python动态链接库

cat >/etc/ld.so.conf.d/Python-3.9.16.conf <<'EOF'
/usr/local/python3/lib/
EOF
ldconfig 

1.5、配置环境变量

cat > /etc/profile.d/Python-3.9.16.sh <<'EOF'
#!/bin/bash
export PYTHON_HOME=/usr/local/python3
export PATH=$PATH:${PYTHON_HOME}/bin
EOF
source /etc/profile.d/Python-3.9.16.sh 

1.6、配置pip下载源

cd ~ && mkdir .pip && cat > .pip/pip.conf <<'EOF'
[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install] 
trusted-host=pypi.douban.com
EOF

1.7、配置虚拟环境

1.7.1、安装软件

pip3 install virtualenv
pip3 install --no-deps stevedore virtualenvwrapper

1.7.2、配置环境变量

mkdir /data//venv
echo 'export WORKON_HOME=/data/venv' >> .bashrc
echo 'export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3' >> .bashrc
echo 'export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python3/bin/virtualenv' >> .bashrc
echo 'source /usr/local/python3/bin/virtualenvwrapper.sh' >> .bashrc
source .bashrc

注意:virtualenv 和 virtualenvwrapper.sh 的路径位置

1.7.3、创建虚拟环境

mkvirtualenv -p python3 flask_env
pip install flask prometheus_client
pip list

# 关于虚拟环境管理,请看文章:https://www.cnblogs.com/ygbh/p/17305785.html

2、部署Flask项目代码

2.1、拷贝代码

mkdir /opt/my_metrics && cd /opt/my_metrics/
cat > my_metric.py <<'EOF'
# -*- coding: utf-8 -*-
"""
   File Name : flask_metric
   Description :
   Author : Administrator
   date : 2023/4/10
   Change Activity : 2023/4/10:
"""
from prometheus_client import Counter, Summary, start_http_server
from wsgiref.simple_server import make_server

from flask import Flask, jsonify

app = Flask(__name__)

request_time = Summary('request_processing_seconds', 'Time spent processing request')  # 请求时间的记录
counter_time = Counter('request_count', 'Total request cout of the host', ['method', 'endpoint'])  # 请求数量+标签统计
counter_time.labels('get', '/').inc()


@app.route('/metrics')
@request_time.time()
def requests_count():
    counter_time.labels('get', '/').inc()
    return jsonify({'return': 'success OK!'})


if __name__ == '__main__':
    start_http_server(8000)
    httpd = make_server('0.0.0.0', 8001, app)
    httpd.serve_forever()
EOF

2.2、进入虚拟环境跑起代码

(flask_env) [root@register ~]# python3 /opt/my_metrics/my_metric.py &

# 注意:本代码,开启两个端口:8000和8001
8000 是prometheus_client端口,用于被prometheus捉取数据的
8001 是flask接口,用到我们测试访问次数递增之用。

2.3、访问prometheus_client端口8000

(flask_env) [root@register ~]# curl http://192.168.10.33:8000/metric
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 105.0
python_gc_objects_collected_total{generation="1"} 280.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 74.0
python_gc_collections_total{generation="1"} 6.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="9",patchlevel="16",version="3.9.16"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 4.66591744e+08
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 2.4743936e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.6811862109e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.19
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 7.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1024.0
# HELP request_processing_seconds Time spent processing request
# TYPE request_processing_seconds summary
request_processing_seconds_count 0.0
request_processing_seconds_sum 0.0
# HELP request_processing_seconds_created Time spent processing request
# TYPE request_processing_seconds_created gauge
request_processing_seconds_created 1.6811862113408284e+09
# HELP request_count_total Total request cout of the host
# TYPE request_count_total counter
request_count_total{endpoint="/",method="get"} 1.0
# HELP request_count_created Total request cout of the host
# TYPE request_count_created gauge
request_count_created{endpoint="/",method="get"} 1.681186211340873e+09

2.4、编写自动随机访问flask 8000的端口的shell脚本

cd /opt/my_metrics/ && cat > curl_metrics.sh<<'EOF'
#!/bin/bash
# 获取随机数
while true
do
  sleep_num=$(($RANDOM%5+1))
  curl_num=$(($RANDOM%50+1))
  for c_num in `seq $curl_num`
  do
    curl -s http://192.168.10.33:8001/metrics >>/dev/null 2>&1
  done
  sleep $sleep_num
done
EOF

3、将自定义metric增加到prometheus

3.1、配置prometheus.yml

]# vi /data/server/prometheus/etc/prometheus.yml
scrape_configs:- job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "node_exporter"
    static_configs:
    - targets: ['192.168.10.29:9100','192.168.10.30:9100']

# 增加如下配置 - job_name: "custom_metric" static_configs: - targets: ['192.168.10.33:8000']

3.2、检查语法是否正常

]# promtool check config /data/server/prometheus/etc/prometheus.yml 
Checking /data/server/prometheus/etc/prometheus.yml
 SUCCESS: /data/server/prometheus/etc/prometheus.yml is valid prometheus config file syntax

3.3、重启prometheus服务

systemctl restart prometheus

3.4、到web查询target状态

显示正常

3.5、查询指标值

request_count_total{instance="192.168.10.33:8000"}
在prometheus上面可以看到正常的数据收集。

4、总结

metric基础
   - prometheus管理数据的核心要点
   - 必须由服务端提供标准接口
定制metric
   - 借助于接口开发模块进行定制开发
prometheus集成
   - job配置、重启服务、数据监测

 

标签:58,自定义,python,request,metrics,process,prometheus,gc,total
From: https://www.cnblogs.com/ygbh/p/17305663.html

相关文章

  • Oracle自定义splitstr
     CREATEORREPLACEFUNCTIONSPLITSTR(P_STRINGINVARCHAR2,P_DELIMITERINVARCHAR2)RETURNSTR_SPLITPIPELINEDASV_LENGTHNUMBER:=LENGTH(P_STRING);V_STARTNUMBER:=1;V_INDEXNUMBER;BEGINWHILE......
  • 小程序自定义省市二级联动
    conststr=[{id:"110000",name:"北京市",city:[{id:"110100",name:"北京市",area:[{id:"110101",name:"东城区"},{id:"110102",name:"西城区"},{id:"110105",name:"朝阳区"},{id:"......
  • Java实现自定义LRU算法
    classLRUCache{//key->Node<key,val>privateHashMap<Integer,Node>map;//Node(k1,v1)<->Node(k2,v2)privateDoubleListcache;//最大容量privateintcap;publicLRUCache(intcapacity){this.c......
  • 6.自定义Form表单验证
    在web程序中存在大量的表单数据验证,而我们通过self.get_argument(arg)进行获取用户输入参数验证会存在的重复代码 1.自定义表单app.py文件代码如下:MyForm类对象属性中封装需要验证的字段并与浏览器中验证的字段保持一致,MyForm类中check_valid方法获取用户输入的参数并进行验证......
  • 区块链学习(9)-自定义修饰词modifier
    在Solidity中,修饰词(modifier)是一种代码重用和逻辑抽象的方法,用于修改函数的行为。它可以在函数执行前进行预处理(如检查条件、权限等),或在函数执行后进行后处理。修饰词在智能合约中非常有用,尤其是用于访问控制、状态检查和重入保护等场景。修饰词定义和使用:要定义一个修饰词,需要使用......
  • [golang]使用logrus自定义日志模块
    简介logrus是一个第三方日志库,性能虽不如zap和zerolog,但方便易用灵活。logrus完全兼容标准的log库,还支持文本、JSON两种日志输出格式。特点相较于标准库,logrus有更细致的日志级别,从高到低分别是:trace>debug>info>warn>error>fatal>panic支持自定义日志格式,内置支......
  • Context响应,重定向,自定义函数,Abort
    前言:Context对象提供了很多内置的响应形式,JSON、HTML、Protobuf、MsgPack、Yaml、String等。它会为每一种形式都单独定制一个渲染器。Context是Gin最重要的部分。它允许我们在中间件之间传递变量,管理流程,验证请求的JSON并呈现JSON响应。正文: content响应字符串,json,及......
  • ES搜索框架--自定义评分规则
    一、评分规则需求按照用户画像(不同的标签分数)和用户省份在用户查询时,对查询结果进行自定义评分二、ES自定义评分方式参考:博客:https://blog.csdn.net/W2044377578/article/details/128636611官网:https://www.elastic.co/guide/en/elasticsearch/guide/master/function-score-query.......
  • vue iview table实现动态自定义表头
    一、前言众所周知,iview中有一个表格组件Table,用于展示多条结构类似的数据。之前遇到过一个需求,要手动控制table的表头显示。就是假如table表格一共有10列数据,可以通过设置勾选,决定显示多少列二、代码为了代码的复用性,将配置页面单独抽成了组件,所以代码中会有组件之间传值父组件......
  • GORM自定义类型
    GORM自定义类型官网地址官方也有一个用来收集自定义的类型的仓库:https://github.com/go-gorm/datatypes场景时间类型初始的时间类型只有time.time类型,而我们习惯输入和展示的结构是形如2023-04-0812:12:12这种。这种格式需要被程序转化为time.time类型被orm使用,从数据库......