I am working with a test environment that has around 10 kubernetes pods that I need to interact with a lot, especially port forwarding.
It takes me a lot of time every day to list pods, get pod name and use that in a command.
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.
So instead of doing
kubectl port-forward pods/planning-api-5fbf84cd4-mbxmn 8081:8081 -n production
I would want something like
kubectl port-forward pods/planning-api* 8081:8081 -n production
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.
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.
I am using a windows machine to achieve this. Using a kubectl alternative would be an option.
Can anyone tell me if and how this could be achieved?
CodePudding user response:
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.
kubectl port-forward svc/planning-api 8081:80
# or
kubectl port-forward deploy/planning-api 8081:8081
That removes the need to know the random pod suffix.
Alternatively, you can use some shell basics. i.e. grep and command substitution:
kubectl port-forward "$(kubectl get pod -o name | grep planning-api)" 8081:8081