本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
查询 Application Load Balancer 日志
是适用于 应用程序负载均衡器 的负载均衡选项,可在使用容器的微服务部署中实现流量分配。Elastic Load Balancing通过查询 应用程序负载均衡器 日志,您可以查看传入和传出 Elastic Load Balancing 实例和后端应用程序的流量来源、延迟和传输字节。有关更多信息,请参阅 https://docs.amazonaws.cn/elasticloadbalancing/latest/application/load-balancer-access-logs.html 中的 Application Load Balancer 的访问日志Application Load Balancer 用户指南。
Prerequisites
-
启用访问日志记录,以便 应用程序负载均衡器 日志可以保存到 Amazon S3 存储桶。
为 ALB 日志创建表
-
将以下
CREATE TABLE
语句复制并粘贴到 Athena 控制台中。将LOCATION 's3://
中的值替换为与您的 Amazon S3 存储桶位置对应的值。有关每个字段的信息,请参阅 https://docs.amazonaws.cn/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-log-entry-format 中的访问日志条目Application Load Balancer 用户指南。your-alb-logs-directory
/AWSLogs/<ACCOUNT-ID>
/elasticloadbalancing/<REGION>
/'CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs ( type string, time string, elb string, client_ip string, client_port int, target_ip string, target_port int, request_processing_time double, target_processing_time double, response_processing_time double, elb_status_code string, target_status_code string, received_bytes bigint, sent_bytes bigint, request_verb string, request_url string, request_proto string, user_agent string, ssl_cipher string, ssl_protocol string, target_group_arn string, trace_id string, domain_name string, chosen_cert_arn string, matched_rule_priority string, request_creation_time string, actions_executed string, redirect_url string, lambda_error_reason string, target_port_list string, target_status_code_list string, classification string, classification_reason string ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe' WITH SERDEPROPERTIES ( 'serialization.format' = '1', 'input.regex' = '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) ([^ ]*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"') LOCATION 's3://
your-alb-logs-directory
/AWSLogs/<ACCOUNT-ID>
/elasticloadbalancing/<REGION>
/'; -
在 Athena 控制台中运行查询。查询完成后,Athena 将注册
alb_logs
表,使其中的数据可以供您发出查询。
ALB 日志的示例查询
以下查询计算按客户端 IP 地址分组的负载均衡器接收到的 HTTP GET 请求的数目:
SELECT COUNT(request_verb) AS count, request_verb, client_ip FROM alb_logs GROUP BY request_verb, client_ip LIMIT 100;
另一个查询显示 Safari 浏览器用户访问的 URLs:
SELECT request_url FROM alb_logs WHERE user_agent LIKE '%Safari%' LIMIT 10;
以下示例演示了如何根据 datetime
解析日志:
SELECT client_ip, sum(received_bytes) FROM alb_logs WHERE parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z') BETWEEN parse_datetime('2018-05-30-12:00:00','yyyy-MM-dd-HH:mm:ss') AND parse_datetime('2018-05-31-00:00:00','yyyy-MM-dd-HH:mm:ss') GROUP BY client_ip;
-
有关更多信息和示例,请参阅 AWS 知识中心文章如何使用 Athena 分析 Application Load Balancer 访问日志?
-
有关使用 Athena 对 ALB 日志进行分区的更多信息,请参阅 上的
athena-add-partitionGitHub。