<p>I have two pods running for a specific service. I want to get logs related to both the pods and check for specific text.</p>
<p>For that I used ,as described <a href="https://stackoverflow.com/questions/33069736/how-do-i-get-logs-from-all-pods-of-a-kubernetes-replication-controller">here</a>:</p>
<p><code>kubectl logs -l app=my-app -c my-app-container --since=25m | grep -i &quot;search-text&quot; |wc -l</code></p>
<p>This does not output anything, even though there are matching text for <code>search-text</code></p>
<p>Then tried with <code>deployments</code></p>
<p><code>kubectl logs deployment/my-app-deployment -c my-app-container --since=90m  | grep -i &quot;search-text&quot; |wc -l</code></p>
<p>How can I search for this specific string in all related pods?</p>
<p><code>kubectl logs my-pod-1 -c my-app-container --since=90m  | grep -i &quot;search-text&quot; |wc -l</code>, this gives the proper count.</p>
<p>References :</p>
<p><a href="https://stackoverflow.com/questions/55851250/get-all-logs-from-a-specific-container-in-a-replica-set">Get all Logs from a specific container in a replica set</a>
<br>
<a href="https://stackoverflow.com/questions/60518658/how-to-get-logs-of-deployment-from-kubernetes">how to get logs of deployment from kubernetes</a></p>

<p><code>kubectl logs</code> is limited to viewing a single pod’s logs at a time. However, you can use the <code>-l</code> flag to use a selector (label query) to filter on. For example:</p>
<pre><code>kubectl logs -l app=nginx -l app=php 
</code></pre>
<p>Use <code>-c</code> flag if you need to see container logs. More supported flags and examples can be found <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs" rel="nofollow noreferrer">here</a>.</p>
<p>When you are able to see the logs from desired Pods/Containers it is time to use <code>grep</code> to filter out the output. For example, I got some logs from a Pod:</p>
<pre><code>~$ kubectl logs nginx-app-b8b875889-4nn52
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
</code></pre>
<p>and I would like to only see lines with the word &quot;configuration&quot;, so I execute:</p>
<pre><code>$ kubectl logs nginx-app-b8b875889-4nn52 | grep configuration
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
</code></pre>
<p>or if I would like to count lines with the word &quot;info&quot; than:</p>
<pre><code>$ kubectl logs nginx-app-b8b875889-4nn52 | grep info |wc -l
2
</code></pre>
<p>More details can be found in the <a href="https://man7.org/linux/man-pages/man1/grep.1.html" rel="nofollow noreferrer">grep manual</a>. Bear in mind that if you don't specify arguments like <code>--since=</code> or <code>--tail=</code> and the Pod your are trying to view logs from is running for some longer period of time the results may be misleading.</p>
<p>Normally, I would also suggest to use 3rd party tools like <a href="https://github.com/stern/stern" rel="nofollow noreferrer">Stern</a> or <a href="https://github.com/johanhaleby/kubetail" rel="nofollow noreferrer">Kubetail</a> which are more powerful than simple <code>kubectl logs</code> but in your use case combining both:</p>
<ul>
<li><code>kubectl logs -l</code></li>
</ul>
<p>and:</p>
<ul>
<li><code>| grep</code></li>
</ul>
<p>is the way to go.</p>
<p><strong>EDIT:</strong></p>
<p>Also make sure you are greping from the proper resources. From your question it seems that you run <code>kubectl logs deployment/my-app-deployment</code> and than <code>kubectl logs my-pod-1 -c my-app-container</code> which does not correspond to the <code>my-app-deployment</code> deployment. List all deployments, pods and labels to be confident that you check the right resource. Use:</p>
<pre><code>kubectl get deploy,pods --show-labels 
</code></pre>
