You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GKE中自动扩缩容的cons1persec部署Pod数量、名称及生命周期时间查询方法咨询

Alright, let's tackle this. You want to pull details like pod count, names, creation/deletion times for your cons1persec Deployment in GKE using Google Cloud Logging Explorer. Here are tailored queries for each use case, plus breakdowns so you understand how they work:

1. Get Pod Creation Details

This query filters for pod creation events, extracts the pod name and creation timestamp, and sorts them to show the newest pods first:

resource.type="k8s_container"
resource.labels.project_id="autoscaling-kafka"
resource.labels.location="europe-west1-d"
resource.labels.cluster_name="autoscalekafka"
resource.labels.namespace_name="default"
labels.k8s-pod/app="cons1persec"
jsonPayload.reason="Created"
jsonPayload.metadata.name:*
severity>=DEFAULT
| parse jsonPayload.metadata.name as pod_name
| parse jsonPayload.metadata.creationTimestamp as creation_time
| select pod_name, creation_time
| order by creation_time desc
  • We filter on jsonPayload.reason="Created" to target only pod creation events
  • The parse commands extract the pod name and creation time from the log's JSON payload
  • Sorting by creation_time desc makes it easy to see your most recently spun-up pods
2. Get Pod Deletion Details

Similar to the creation query, this targets deletion events to get pod names and when they were removed:

resource.type="k8s_container"
resource.labels.project_id="autoscaling-kafka"
resource.labels.location="europe-west1-d"
resource.labels.cluster_name="autoscalekafka"
resource.labels.namespace_name="default"
labels.k8s-pod/app="cons1persec"
jsonPayload.reason="Deleted"
jsonPayload.metadata.name:*
severity>=DEFAULT
| parse jsonPayload.metadata.name as pod_name
| parse jsonPayload.deletionTimestamp as deletion_time
| select pod_name, deletion_time
| order by deletion_time desc
  • Here we use jsonPayload.reason="Deleted" to isolate deletion events
  • We parse the deletionTimestamp field instead of creation time to track when pods were terminated
3. Count Total Created Pods

If you just want a total count of all pods ever created for your Deployment:

resource.type="k8s_container"
resource.labels.project_id="autoscaling-kafka"
resource.labels.location="europe-west1-d"
resource.labels.cluster_name="autoscalekafka"
resource.labels.namespace_name="default"
labels.k8s-pod/app="cons1persec"
jsonPayload.reason="Created"
severity>=DEFAULT
| count_distinct(jsonPayload.metadata.name) as total_created_pods
  • count_distinct ensures we don't count duplicate entries for the same pod (in case multiple logs exist for a single creation event)
4. Get Currently Running Pods (Exclude Deleted Ones)

To see only pods that are still active (haven't been deleted yet), we can use a CTE (Common Table Expression) to join creation and deletion logs:

WITH created_pods AS (
  SELECT jsonPayload.metadata.name as pod_name, jsonPayload.metadata.creationTimestamp as creation_time
  FROM `autoscaling-kafka.europe-west1-d._Default`
  WHERE resource.type="k8s_container"
    AND resource.labels.project_id="autoscaling-kafka"
    AND resource.labels.location="europe-west1-d"
    AND resource.labels.cluster_name="autoscalekafka"
    AND resource.labels.namespace_name="default"
    AND labels.k8s-pod/app="cons1persec"
    AND jsonPayload.reason="Created"
    AND severity>=DEFAULT
),
deleted_pods AS (
  SELECT jsonPayload.metadata.name as pod_name, jsonPayload.deletionTimestamp as deletion_time
  FROM `autoscaling-kafka.europe-west1-d._Default`
  WHERE resource.type="k8s_container"
    AND resource.labels.project_id="autoscaling-kafka"
    AND resource.labels.location="europe-west1-d"
    AND resource.labels.cluster_name="autoscalekafka"
    AND resource.labels.namespace_name="default"
    AND labels.k8s-pod/app="cons1persec"
    AND jsonPayload.reason="Deleted"
    AND severity>=DEFAULT
)
SELECT cp.pod_name, cp.creation_time, dp.deletion_time
FROM created_pods cp
LEFT JOIN deleted_pods dp ON cp.pod_name = dp.pod_name
WHERE dp.deletion_time IS NULL
ORDER BY cp.creation_time desc
  • We first create two temporary datasets: one for all created pods, one for deleted pods
  • A left join lets us match pods across both datasets, and filtering for dp.deletion_time IS NULL keeps only pods that haven't been deleted
5. Alternative: Use k8s_pod Resource Type for Pod-Level Events

Instead of using k8s_container (which targets container logs), you can use the k8s_pod resource type to query directly against pod-level events. This might be more reliable for tracking pod lifecycle events:

resource.type="k8s_pod"
resource.labels.project_id="autoscaling-kafka"
resource.labels.location="europe-west1-d"
resource.labels.cluster_name="autoscalekafka"
resource.labels.namespace_name="default"
labels.k8s-pod/app="cons1persec"
severity>=DEFAULT
| parse resource.labels.pod_name as pod_name
| parse jsonPayload.metadata.creationTimestamp as creation_time
| parse jsonPayload.metadata.deletionTimestamp as deletion_time
| select pod_name, creation_time, deletion_time
| order by creation_time desc
  • Here we pull the pod name directly from resource.labels.pod_name instead of the JSON payload
  • This query will return all lifecycle events for your pods, so you'll see both creation and deletion entries in the results

内容的提问来源于stack exchange,提问作者Mazen Ezzeddine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:37:35