<p>Is there anyway to get all logs from pods in a specific namespace running a dynamic command like a combination of awk and xargs?</p>
<pre><code> kubectl get pods | grep Running | awk '{print $1}' | xargs kubectl logs | grep value
</code></pre>
<p>I have tried the command above but it's failing like <code>kubectl logs</code> is missing pod name:</p>
<blockquote>
<p>error: expected 'logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]'.
POD or TYPE/NAME is a required argument for the logs command
See 'kubectl logs -h' for help and examples</p>
</blockquote>
<p>Do you have any suggestion about how get all logs from Running pods?</p>

placplacboom
<p>Think about what your pipeline is doing:</p>
<p>The <code>kubectl logs</code> command takes as an argument a <em>single</em> pod name, but through your use of <code>xargs</code> you're passing it <em>multiple</em> pod names. Make liberal use of the <code>echo</code> command to debug your pipelines; if I have these pods in my current namespace:</p>
<pre><code>$ kubectl get pods -o custom-columns=name:.metadata.name
name
c069609c6193930cd1182e1936d8f0aebf72bc22265099c6a4af791cd2zkt8r
catalog-operator-6b8c45596c-262w9
olm-operator-56cf65dbf9-qwkjh
operatorhubio-catalog-48kgv
packageserver-54878d5cbb-flv2z
packageserver-54878d5cbb-t9tgr
</code></pre>
<p>Then running this command:</p>
<pre><code>kubectl get pods | grep Running | awk '{print $1}' | xargs echo kubectl logs
</code></pre>
<p>Produces:</p>
<pre><code>kubectl logs catalog-operator-6b8c45596c-262w9 olm-operator-56cf65dbf9-qwkjh operatorhubio-catalog-48kgv packageserver-54878d5cbb-flv2z packageserver-54878d5cbb-t9tgr
</code></pre>
<hr />
<p>To do what you want, you need to arrange to call <code>kubectl logs</code> multiple times with a single argument. You can do that by adding <code>-n1</code> to your <code>xargs</code> command line. Keeping the <code>echo</code> command, running this:</p>
<pre><code>kubectl get pods | grep Running | awk '{print $1}' | xargs -n1 echo kubectl logs
</code></pre>
<p>Gets us:</p>
<pre><code>kubectl logs catalog-operator-6b8c45596c-262w9
kubectl logs olm-operator-56cf65dbf9-qwkjh
kubectl logs operatorhubio-catalog-48kgv
kubectl logs packageserver-54878d5cbb-flv2z
kubectl logs packageserver-54878d5cbb-t9tgr
</code></pre>
<p>That looks more reasonable. If we drop the echo and run:</p>
<pre><code>kubectl get pods | grep Running | awk '{print $1}' | xargs -n1 kubectl logs | grep value
</code></pre>
<p>Then you will get the result you want. You may want to add the <code>--prefix</code> argument to <code>kubectl logs</code> so that you know which pod generated the match:</p>
<pre><code>kubectl get pods | grep Running | awk '{print $1}' | xargs -n1 kubectl logs --prefix | grep value
</code></pre>
<hr />
<p>Not directly related to your question, but you can lose that <code>grep</code>:</p>
<pre><code>kubectl get pods | awk '/Running/ {print $1}' | xargs -n1 kubectl logs --prefix | grep value
</code></pre>
<p>And even lose the <code>awk</code>:</p>
<pre><code>kubectl get pods --field-selector=status.phase==Running -o name | xargs -n1 kubectl logs --prefix | grep value
</code></pre>
