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:
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
parsecommands extract the pod name and creation time from the log's JSON payload - Sorting by
creation_time descmakes it easy to see your most recently spun-up pods
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
deletionTimestampfield instead of creation time to track when pods were terminated
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_distinctensures we don't count duplicate entries for the same pod (in case multiple logs exist for a single creation event)
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 NULLkeeps only pods that haven't been deleted
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_nameinstead 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

