Get all the pods dynamically created belonging to an autoscaled deployment

6/19/2021

I am running an application/deployment called cons1persec on google kubernetes engine GKE. My application/deployment is being monitored through a controller application and autoscaled approparitely based on a metric. I can view the logs of my deployment in google cloud logs explorer through the following query :

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" severity>=DEFAULT

My question is about the appropriate query to get the number of pods created belonging to my application/deployment cons1persec, the name of pods and their creation/deletion time etc..

Thank you.

-- Mazen Ezzeddine
google-cloud-platform
google-kubernetes-engine
kubernetes

1 Answer

6/24/2021

When using Deployment, it makes use of deployment’s name as a prefix for a pod’s name it creates and we cannot have two deployments with the same name, so we can make use of these to query for pods belonging to a specific deployment.
Refer to the below sample query which uses regular expressions/substring comparison operator to match the deployment name which is prefix of a pod’s name and reason for log creation to query for the pod’s name, creation/deletion and corresponding timestamps.

Sample Log query:

Severity = INFO
Resource.type = "k8s_cluster"
log_name = "projects/<PROJECT-ID>/logs/events"
jsonPayload.reason = ("SuccessfulCreate" OR "SuccessfulDelete")
# Using regular expressions[1]
jsonPayload.metadata.name =~ "<WORKLOAD-NAME>\S*"  
# Using substring comparison operator[2]
jsonPayload.metadata.name : "WORKLOAD-NAME"

1- https://cloud.google.com/logging/docs/view/logging-query-language#regular-expressions
2- https://cloud.google.com/logging/docs/view/logging-query-language#comparisons

-- Ashish
Source: StackOverflow