启用自动扩缩功能后,自动扩缩器会根据您指定的选项制定扩缩决策。每一项扩缩决策都会由 Cloud Logging 记录。为了更好地了解自动扩缩器的扩缩决策,您可以在日志浏览器中查看这些日志。
您可以使用日志浏览器查看与以下内容相关的事件:
- 自动扩缩器的托管式实例组 (MIG) 大小调整建议。如果自动扩缩器为 MIG 计算新的建议大小,则自动扩缩器日志会描述建议大小的变化。请参阅调整大小日志条目。
- 自动扩缩器状态发生更改。例如,如果自动扩缩器原本按预期工作,但后来遇到问题,则自动扩缩器日志会描述状态的更改。请参阅状态更改日志条目。
准备工作
- 阅读自动扩缩文档。
- 准备一个您要查看其日志的活跃自动扩缩器。
- 查看如何使用日志浏览器。
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以选择以下任一选项向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
-
查看自动扩缩器日志
如需查看自动扩缩器日志,请使用 Google Cloud 控制台、gcloud CLI 或 REST。
如果您已启用自动扩缩功能,则可以在Google Cloud console中查看自动扩缩日志。
- 进入日志浏览器页面。
- 点开资源下拉菜单,选择自动扩缩器。
您还可以从 MIG 进入日志浏览器页面,如下所示:
在 Google Cloud 控制台中,前往实例组页面。
点击要查看其日志的 MIG 的名称。
点击 Monitoring 标签页。
在 Monitoring 标签页中,日志面板位于页面底部。点击此面板末尾的图标以打开面板。
如需打开日志浏览器页面,请点击此面板中的在日志浏览器中查看图标。日志浏览器会打开,并使用默认查询来获取 MIG 的所有自动扩缩器日志。
如需查找与自动扩缩相关的所有日志,请使用 logging read
命令。例如:
gcloud logging read "resource.type=autoscaler" --limit 10 \ --format json
如需查看特定 MIG 的自动扩缩器日志,请按如下所示指定 instance_group_manager_name
:
gcloud logging read "resource.type=autoscaler AND \ resource.labels.instance_group_manager_name=example-igm" \ --limit 10 --format json
如需查看与自动扩缩器调整大小操作相关的日志条目,请指定 compute.autoscalers.resize
方法名称:
gcloud logging read "resource.type=autoscaler AND \ resource.labels.instance_group_manager_name=example-igm AND \ protoPayload.methodName=compute.autoscalers.resize" \ --limit 10 --format json
如需查看与自动扩缩器状态更改相关的日志条目,请指定 compute.autoscalers.changeStatus
方法名称:
gcloud logging read "resource.type=autoscaler AND \ resource.labels.instance_group_manager_name=example-igm AND \ protoPayload.methodName=compute.autoscalers.changeStatus" \ --limit 10 --format json
向 Logging V2 API 发出请求。
请求正文必须包含搜索日志时要使用的 filter
参数以及要查看日志的 project
。例如,如需发出请求以获取特定 MIG 的调整大小操作列表,请使用以下命令:
POST https://logging.googleapis.com/v2/entries:list { "filter": "resource.type=autoscaler AND resource.labels.instance_group_manager_name=example-igm AND protoPayload.methodName=compute.autoscalers.resize", "pageSize": 10, "resourceNames": [ "projects/example-project" ] }
如需发出请求以获取特定 MIG 的状态更改列表,请使用以下命令:
POST https://logging.googleapis.com/v2/entries:list { "filter": "resource.type=autoscaler AND resource.labels.instance_group_manager_name=example-igm AND protoPayload.methodName=compute.autoscalers.changeStatus", "pageSize": 10, "resourceNames": [ "projects/example-project" ] }
调整大小日志条目
自动扩缩器为 MIG 计算建议的大小时,Compute Engine 会创建一个日志条目,用于捕获新的和旧的推荐大小。对于可用区级 MIG,日志条目还会捕获建议大小更改的原因。
以下是可用区级 MIG 的日志条目示例。在此示例中,自动扩缩器根据预测性自动扩缩功能计算出虚拟机数为 3
,以实现 35%
的 CPU 利用率目标。但是,自动扩缩政策中配置的实例数上限仅为 2
。因此,自动扩缩器会将建议的大小限制为 2
。
{ insertId: "1l68z7sg4jw7kzo" logName: "projects/example-project/logs/cloudaudit.googleapis.com%2Fsystem_event" protoPayload: { @type: "type.googleapis.com/google.cloud.audit.AuditLog" metadata: { @type: "type.googleapis.com/autoscaler.AutoscalerSizeChangeExplanation" autoscalingMode: "ON" autoscalingReason: { scalingLimit: { limitPolicy: { maxNumReplicas: 2 } limitSize: 2 name: "MAX_INSTANCES" } scalingSignal: { calculatedSize: 3 calculationDetails: { servingSize: 1 signalTarget: 0.35 signalValue: 1.03533 } name: "PREDICTED_CPU_UTILIZATION" signalPolicy: { coolDownPeriodSec: 15 cpuUtilization: { predictiveMethod: "OPTIMIZE_AVAILABILITY" utilizationTarget: 0.35 } } } summary: "The autoscaler's recommended size changed from 1 to 2 because the autoscaler predicted that in 15 seconds the average CPU utilization across 1 serving instance will be 103.533%, which is above the utilization target of 35%. The calculated size was 3 to achieve the target CPU utilization but the size was limited by the maximum number of instances set in the autoscaling policy." } newSize: 2 oldSize: 1 } methodName: "compute.autoscalers.resize" resourceName: "projects/example-project/zones/us-east1-d/autoscalers/example-autoscaler" serviceName: "compute.googleapis.com" } receiveTimestamp: "2022-06-13T22:34:43.045973046Z" resource: { labels: { autoscaler_id: "1234567890123456789" autoscaler_name: "example-autoscaler" instance_group_manager_id: "1357908642148074125" instance_group_manager_name: "example-igm" location: "us-east1-d" project_id: "example-project" } type: "autoscaler" } severity: "INFO" timestamp: "2022-06-13T22:34:42.810216614Z" }
下表介绍了日志条目中的字段:
属性 | 值 |
---|---|
autoscalingMode |
记录条目时的自动扩缩模式。无论是什么模式,自动扩缩器都会计算建议的大小,由日志中的 newSize 表示。根据自动扩缩模式,自动扩缩器会按如下所示调整 MIG 的大小:
|
autoscalingReason |
有关自动扩缩原因的详细信息。仅为可用区级 MIG 显示此字段。 |
autoscalingReason.scalingLimit
|
有关限制 calculatedSize 的自动扩缩功能的详细信息,这是自动扩缩器根据自动扩缩信号计算的大小。可以限制 calculatedSize 的自动扩缩功能是最大或最小实例数、缩容控制机制或稳定期。如果没有对 calculatedSize 应用限制,则日志中不会显示 scalingLimit 字段。 |
autoscalingReason.scalingLimit.limitPolicy
|
限制 calculatedSize 的自动扩缩功能的配置。在稳定限制 calculatedSize 时不设置 limitPolicy 字段,因为稳定是自动扩缩的默认功能。 |
autoscalingReason.scalingLimit.limitSize
|
基于限制 calculatedSize 的自动扩缩功能的虚拟机数量。 |
autoscalingReason.scalingLimit.name
|
限制 calculatedSize 的自动扩缩功能的名称。 |
autoscalingReason.scalingSignal
|
有关用于计算 MIG 应拥有的虚拟机数量的自动扩缩信号的详细信息。如果自动扩缩政策有多个信号,则自动扩缩器会考虑需要最大虚拟机数的信号。 |
autoscalingReason.scalingSignal.calculatedSize
|
基于自动扩缩信号 MIG 应拥有的虚拟机数量。如果应用 scalingLimit ,则自动扩缩器会将 calculatedSize 限制为 scalingLimit.limitSize 。 |
autoscalingReason.scalingSignal.calculationDetails
|
用于确定 calculatedSize 的详细信息。
|
autoscalingReason.scalingSignal.name
|
自动扩缩信号的名称,自动扩缩器基于该信号计算虚拟机的数量,在日志中由 calculatedSize 表示。 |
autoscalingReason.scalingSignal.signalPolicy
|
自动扩缩信号的配置,自动扩缩器基于该信号确定 calculatedSize 。 |
autoscalingReason.summary
|
自动扩缩原因的说明。 |
newSize
|
当前建议的 MIG 大小。根据 autoscalingMode ,自动扩缩器会按如下所示将 MIG 的大小调整为 newSize :
|
oldSize
|
之前建议的 MIG 大小。 |
如需了解日志条目中的其他字段,请参阅审核日志条目的格式。
状态更改日志条目
当自动扩缩器的状态更改时,Compute Engine 会创建一个日志条目,用于捕获旧状态消息和新状态消息。您可以使用这些状态消息找出自动扩缩器未按预期运行的原因,或调试您之前注意到的旧问题。如需了解一些常见的状态消息,请参阅常见的返回状态消息。
protoPayload
中的以下字段描述了新旧状态:
metadata.newStatus.details
:自动扩缩器的新状态。metadata.oldStatus.details
:自动扩缩器的旧状态。
以下是描述状态更改的日志条目的示例。在此示例中,您可能会注意到新的状态消息,指明负载均衡配置存在问题。
{ insertId: "ivho6kg4icqfio" logName: "projects/example-project/logs/cloudaudit.googleapis.com%2Fsystem_event" protoPayload: { metadata: { @type: "type.googleapis.com/autoscaler.AutoscalerStatusChange" newStatus: { details: "The autoscaler is configured to scale based on a load balancing signal but the instance group has not received any utilization data from the load balancer or the utilization is constantly 0. Check that the load balancing configuration is working" } oldStatus: { details: "OK" }, methodName: "compute.autoscalers.changeStatus" resourceName: "projects/example-project/zones/us-east1-b/autoscalers/example-autoscaler" serviceName: "compute.googleapis.com" } receiveTimestamp: "2022-06-08T03:54:12.332329320Z" resource: { labels: { autoscaler_id: "1234567890123456789" autoscaler_name: "example-autoscaler" instance_group_manager_id: "1357908642148074125" instance_group_manager_name: "example-igm" location: "us-east1-b" project_id: "example-project" } type: "autoscaler" } severity: "INFO" timestamp: "2022-06-08T03:54:10.675416812Z" } }
如需了解日志条目中的其他字段,请参阅审核日志条目的格式。
后续步骤
- 使用从日志中接收的信息更新自动扩缩器。
- 了解自动扩缩器如何做出决策。
- 查看自动扩缩选项。