<p>I often run tasks like:</p>

<ul>
<li>Read the log of the service  <code>X</code> 
or</li>
<li>Attach a shell inside the service <code>Y</code></li>
</ul>

<p>I always use something in my history like:</p>

<pre><code>kubectl logs `kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep &lt;partial_name&gt;`
</code></pre>

<p>or</p>

<pre><code>kubectl exec -it `kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep &lt;partial_name&gt;` bash
</code></pre>

<p>Do you know if <code>kubectl</code> has already something in place for this? Or should I create my own set of aliases?</p>

<p>Kubernetes instances are loosely coupled by the means of <strong>labels</strong> (key-value pairs). Because of that Kubernetes provides various functionalities that can help you to operate on sets of objects based on labels.</p>

<p>In case you have several pods of the same service good chances that they are managed by some ReplicaSet with the use of some specific label. You should see it if you run:</p>

<pre><code>kubectl get pods --show-labels
</code></pre>

<p>Now for aggregating logs for instance you could use label selector like:</p>

<pre><code>kubectl logs -l key=value
</code></pre>

<p>For more info please see: <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/</a> .</p>
