<p>When using Google Stackdriver I can use the log query to find the exact log statements I am looking for.</p>
<p>This might look like this:</p>
<pre><code>resource.type=&quot;k8s_container&quot;
resource.labels.project_id=&quot;my-project&quot;
resource.labels.location=&quot;europe-west3-a&quot;
resource.labels.cluster_name=&quot;my-cluster&quot;
resource.labels.namespace_name=&quot;dev&quot;
resource.labels.pod_name=&quot;my-app-pod-7f6cf95b6c-nkkbm&quot;
resource.labels.container_name=&quot;container&quot;
</code></pre>
<p>However as you can see in this query argument <code>resource.labels.pod_name=&quot;my-app-pod-7f6cf95b6c-nkkbm&quot;</code> that I am looking for a pod with the id <code>7f6cf95b6c-nkkbm</code>. Because of this I can not use this Stackdriver view with this exact query <em>if</em> I deployed a new revision of <code>my-app</code> therefore having a new ID and the one in the curreny query becomes <em>invalid</em> or not locatable.</p>
<p>Now I don't always want to look for the new ID every time I want to have the current view of my <code>my-app</code> logs. So I tried to add a special label <code>stackdriver: my-app</code> to my Kubernetes YAML file.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    metadata:
      labels:
        stackdriver: my-app &lt;&lt;&lt;
</code></pre>
<p>Revisiting my newly deployed Pod I can assure that the label <code>stackdriver: my-app</code> is indeed existing.</p>
<p>Now I want to add this new label to use as a query argument:</p>
<pre><code>resource.type=&quot;k8s_container&quot;
resource.labels.project_id=&quot;my-project&quot;
resource.labels.location=&quot;europe-west3-a&quot;
resource.labels.cluster_name=&quot;my-cluster&quot;
resource.labels.namespace_name=&quot;dev&quot;
resource.labels.pod_name=&quot;my-app-pod-7f6cf95b6c-nkkbm&quot;
resource.labels.container_name=&quot;container&quot;
resource.labels.stackdriver=my-app &lt;&lt;&lt; the kubernetes label
</code></pre>
<p>As you can guess this did not work otherwise I'd have no reason to write this question ;)
Any idea how the thing I am about to do can be achieved?</p>

xetra11
<blockquote>
<p>Any idea how the thing I am about to do can be achieved?</p>
</blockquote>
<p>Yes! In fact, I've prepared an example to show you the whole process :)</p>
<p>Let's assume:</p>
<ul>
<li>You have a <code>GKE</code> cluster named: <code>gke-label</code></li>
<li>You have a Cloud Operations for GKE enabled (logging)</li>
<li>You have a <code>Deployment</code> named <code>nginx</code> with a following <code>label</code>:
<ul>
<li><code>stackdriver: look_here_for_me</code></li>
</ul>
</li>
</ul>
<p><code>deployment.yaml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
      stackdriver: look_here_for_me
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
        stackdriver: look_here_for_me
    spec:
      containers:
      - name: nginx
        image: nginx
</code></pre>
<p>You can apply this definition and send some traffic from the other pod so that the logs could be generated. I've done it with:</p>
<ul>
<li><code>$ kubectl run -it --rm --image=ubuntu ubuntu -- /bin/bash</code></li>
<li><code>$ apt update &amp;&amp; apt install -y curl</code></li>
<li><code>$ curl NGINX_POD_IP_ADDRESS/NONEXISTING</code> # &lt;-- this path is only for better visibility</li>
</ul>
<p>After that you can go to:</p>
<ul>
<li><code>GCP Cloud Console (Web UI)</code> -&gt; <code>Logging</code> (I used new version)</li>
</ul>
<p>With the following query:</p>
<pre><code>resource.type=&quot;k8s_container&quot;
resource.labels.cluster_name=&quot;gke-label&quot;
--&gt;labels.&quot;k8s-pod/stackdriver&quot;=&quot;look_here_for_me&quot;
</code></pre>
<p>You should be able to see the container logs as well it's <code>label</code>:</p>
<p><a href="https://i.stack.imgur.com/RTMYD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RTMYD.png" alt="NGINX_POD" /></a></p>
