Http 负载均衡的日志保护很多内容,目前包括这些字段:
logName,Resource.type,Resource.labels.url_map_name,Resource.labels.forwarding_rule_name,Resource.labels.backend_service_name,Resource.labels.target_proxy_name,Resource.labels.zone,Resource.labels.project_id,textPayload,Jsonpayload_type_loadbalancerlogentry._type,Jsonpayload_type_loadbalancerlogentry.statusdetails,Timestamp,receiveTimestamp,Severity,insertId,httpRequest.requestMethod,httpRequest.requestUrl,httpRequest.requestSize,httpRequest.status,httpRequest.responseSize,httpRequest.userAgent,httpRequest.remoteIp,httpRequest.serverIp,httpRequest.referer,httpRequest.cacheLookup,httpRequest.cacheHit,httpRequest.cacheValidatedWithOriginServer,httpRequest.cacheFillBytes,httpRequest.protocol,Operation.id,Operation.producer,Operation.first,Operation.last,trace,spanId,traceSampled,sourceLocation.file,sourceLocation.line,sourceLocation.function
通过这些字段,可以查询 Http 负载均衡的流量、CDN Cache 的流量、http status 等各种信息。
本文将介绍如何实现把 http 负载均衡的日志导入到 BigQuery,并实现查询。
一 创建 http 负载均衡
通过 Google 的 console 或命令行创建一个 http 的负载均衡:
二 把 http 负载均衡的日志导入 BigQuery
创建好后,把负载均衡的日志通过 stackdriver 导入到 BigQuery 中。
1 logging export
在 STACKDRIVER 中选择 logging,选择 exports:
2 Edit Export
选择 Cloud HTTP Load Balancer,
在 Edit Export 中起一个 export 的名字,tobigquery
Sink Service 选 BigQuery
Sind Destination 选择创建一个新的 BigQuery dataset
3 new BigQuery dataset
给新的 dataset 起一个名字,并创建
4 update Export
对创建好的 Export,可以修改内容,比如指定具体的 http 负载均衡:
这里指定了 hw-http-lb-01-forwarding-rule 这个负载均衡规则的 logging。选择好后,点击 update sink:
三 BigQuery 中查询
1 查询准备
过一段时间,在 BigQuery 中(https://bigquery.cloud.google.com)可以看到创建的 DataSet 已经有数据进来:
点击 compose Query:
点击 show options:
去掉 Use Legacy SQL 后点击 hide Options:
2 查询
具体查询:
查询 http status 是大于200,小于400的访问(正常访问)
或者>400的非正常访问。
3 统计流量
通过 SQL 中的 sum 功能统计流量:
统计了 http status 大于等于200,小于400的下行流量为1.7M。
四 总结
通过把 http 负载均衡的日志导入 BigQuery,可以方便的在 BigQuery 中对 Http 的各种属性通过 SQL 语句进行统计和分析。
文章版权归作者所有,未经许可不得转载。
文章信息
相关推荐
