<p>I am working with a test environment that has around 10 kubernetes pods that I need to interact with a lot, especially port forwarding.</p>
<p>It takes me a lot of time every day to list pods, get pod name and use that in a command.</p>
<p>Since all my pods start with a fixed name, followed by some random string that changes several times a day, I am looking for a way to port forward (and potentially other commands) using only the fixed part of the pod name.</p>
<p>So instead of doing</p>
<pre><code>kubectl port-forward pods/planning-api-5fbf84cd4-mbxmn 8081:8081 -n production
</code></pre>
<p>I would want something like</p>
<pre><code>kubectl port-forward pods/planning-api* 8081:8081 -n production
</code></pre>
<p>I would ideally want to put it into some sort of script that I can just run, so multiple statements would work as well, as long as they don't need my manual intervention.</p>
<p>There will never be more than one instance of any of the pods I use, so that won't be an issue. Changing the pod names to drop the random part is not an option.</p>
<p>I am using a windows machine to achieve this. Using a kubectl alternative would be an option.</p>
<p>Can anyone tell me if and how this could be achieved?</p>

<p>You can do a port-forward on a service or controller, i.e. deployment, instead, which should be sufficient, given the assumption that you have a service or controller.</p>
<pre><code>kubectl port-forward svc/planning-api 8081:80
# or
kubectl port-forward deploy/planning-api 8081:8081
</code></pre>
<p>That removes the need to know the random pod suffix.</p>
<p>Alternatively, you can use some shell basics. i.e. grep and command substitution:</p>
<pre><code>kubectl port-forward &quot;$(kubectl get pod -o name | grep planning-api)&quot; 8081:8081
</code></pre>
