[可以使用新的方案: 拨测API接口+监控方案_一直学下去的博客-CSDN博客]

写在最前面

  我心里也打鼓,是不是应该把这个文章写出来,因为这个项目里面的代码不能拿来就用,需要做修改(其实后端API监控本来就和业务相关,应该没有一个成品可以做到对每一个业务的API都能监控).其次文章里面涉及的知识点比较多,Prometheus,Prometheus-Operator,K8S,Python ,如果你已经具备这些基础,也愿意花时间来读这篇文章,请继续。如果按照步骤做了有问题,可以留言,我会回复的。

简介:

目前公司有一个项目,使用前后端分离。前端采样VUE,后端使用Java Spring全家桶,后端的接口为Restful API.为了能第一时间发现后端服务的故障和检测后端API的响应时间,自己使用Python+Prometheus_Client(python sdk) 写了一个exporter,然后对接prometheus,并配置告警。后端有故障的时候,可以第一时间发现,而不是等用户有感觉来才发现。 

后端项目的接口认证采用在header中附加token的形式,用户第一次登录的时候会返回token。

PS: 前端项目运行在nginx容器中,已经使用blackbox的http模块监控

实施步骤:

1. 将代码克隆到本地,或者使用pycharm导入

git clone https://gitee.com/kevinliu_CQ/api-monitoring.git

2. 修改代码中的配置

   a. 修改config_dir 下面的app.yaml文件。里面的域名和接口地址都需要修改,以我的项目为例,请查看描述信息

config:
     testset: 'APP API monitoring'  #描述性信息,代码中未使用
     timeout: 15 #调用API的超时时间
     scrape_interval: 15 #检测API的时间间隔,单位为秒
     base_url: 'https://app.×××.com/app' #API接口的URL,我的项目是微服务,这个app是微服务的url前缀
token:
     base_url: 'https://app.××××.com/app' #获取token的URL地址
     url: '/login/pwdLogin' #获取token的接口地址
     method: "POST" #获取接口的HTTP方法
     body: '' #获取接口时在post请求的payload内容
     params: {"mobile": "17320491234","pwd": "your_password"} #这个是在请求的URL中的参数列表,HTTP请求的参数可以放在URL里面,也可以放在payload里面,我的应用时放在URL里面的
     headers: {} # POST请求的header 
     token_key_in_request: 'authKey' # 在post请求中后端认证时在header里面读取的值名字,后面截图说明
     token_key_in_response: 'token' #在用户第一次登录后返回数据里面token的值的名字,
verify:
    base_url: 'https://app.****.com/app' #验证token的URL,因为token一般都有较长时间的有效期,不能监控一次就取一次token
    url: '/user/userAddress/list' #验证token的接口地址
    method: "GET" #验证token的HTTP方法
    token_key_in_request: 'authKey' # header里面token值的名字
    headers: {'Content-Type': 'application/json'} #HTTP请求的header
cases: #所有需要监控的API接口地址
- UserAddress:  #接口名字
    url: '/user/userAddress/list' #接口地址,相对于config段里base_url
    method: "GET" #请求方式
    headers: {'Content-Type': 'application/json'} # HTTP headers
- UserBalance:
    url: '/user/getBalance'
    method: "GET"
    headers: {'Content-Type': 'application/json'}
- UserRecord:
    url: '/user/userRecord/getTypeList'
    method: "GET"
    headers: {'Content-Type': 'application/json'}
- UserBankCard:
    url: '/user/bankCard/payList'
    method: "GET"
    headers: {'Content-Type': 'application/json'}

b. 如果有多个微服务API需要监控,直接添加另外一个文件就可以了,比如我的im.yaml这个文件,im是另外一个微服务

3. 制作Docker镜像,我这里的镜像上传到私有habor仓库中。

docker build -t harbor.***.work/monitor/api-monitoring:latest .
docker push harbor.***.work/monitor/api-monitoring:latest

4.修改k8s.yaml文件中的镜像地址,镜像地址和步骤3里面的地址一致

5. 在k8s中部署服务

kubectl apply -f k8s.yaml

6. 查看运行的容器

[root@controller ~]# kubectl get po -n monitoring
NAME                                READY   STATUS    RESTARTS   AGE
api-monitoring-5584fb9bbb-l2xb8     1/1     Running   0          32h
[root@controller ~]# kubectl get svc -n monitoring
NAME               TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)    AGE
api-monitoring     ClusterIP   192.168.180.128   <none>        80/TCP     3d8h
[root@controller ~]# 

7. 查看prometheus采集到的数据,API 和API_ResponseTime,这两个Metrics分别代表API是否可用(1正常,0不正常)以及API的响应时间,单位是秒

[root@controller ~]# curl 192.168.180.128
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 113122.0
python_gc_objects_collected_total{generation="1"} 10240.0
python_gc_objects_collected_total{generation="2"} 987.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"} 7069.0
python_gc_collections_total{generation="1"} 642.0
python_gc_collections_total{generation="2"} 58.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="6",patchlevel="1",version="3.6.1"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 9.0904576e+08
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 3.485696e+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.5912509406e+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 967.65
# 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 1.048576e+06
# HELP request_process_sencods Time spent processing request
# TYPE request_process_sencods summary
request_process_sencods_count 15472.0
request_process_sencods_sum 637.3630767939612
# HELP request_process_sencods_created Time spent processing request
# TYPE request_process_sencods_created gauge
request_process_sencods_created 1.5912509420943658e+09
# HELP API API Status
# TYPE API gauge
API{apiModule="IMFriend"} 1.0
API{apiModule="IMQrcode"} 1.0
API{apiModule="UserRecord"} 1.0
API{apiModule="UserBankCard"} 1.0
API{apiModule="UserBalance"} 1.0
API{apiModule="UserAddress"} 1.0
# HELP API_ResponseTime API Response Time
# TYPE API_ResponseTime gauge
API_ResponseTime{apiModule="IMFriend"} 0.0
API_ResponseTime{apiModule="IMQrcode"} 0.0
API_ResponseTime{apiModule="UserRecord"} 0.0
API_ResponseTime{apiModule="UserBankCard"} 0.0
API_ResponseTime{apiModule="UserBalance"} 0.0
API_ResponseTime{apiModule="UserAddress"} 0.0
[root@controller ~]# 

8.接下来就是在Prometheus中配置监控target和告警规则,我的集群里面使用Prometheus-Operator部署的Prometheus,所以我使用CRD来配置告警规则。

kubectl create secret generic prometheus-operator-prometheus-scrape-confg --from-file=additional-scrape-configs.yaml --dry-run -oyaml -nmonitoring > additional.yaml && kubectl apply -f additional.yaml
kubectl apply -f alertrule.yaml

9.在prometheus中查看监控target和告警规则已经生效

 10.Prometheus的告警会发送给Alertmanager,告警的媒介就自己配置了。 我配置了一个阿里云的电话告警,也是自己写的一个服务

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐