Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active August 22, 2022 22:45
Show Gist options
  • Save sheldonhull/646c307877d57c0737b06cf2312f8e82 to your computer and use it in GitHub Desktop.
Save sheldonhull/646c307877d57c0737b06cf2312f8e82 to your computer and use it in GitHub Desktop.
Kubectl hacks

Port Forwarding To A Pod

When you can't easily port forward to a service, here's how to grab a single pod from the matching selector and port forward to it.

NAMESPACE=
INSTANCE=myapp
CONTEXT=
PORT_LOCAL=
PORT_REMOTE=

pod=$(kubectl get pod --namespace $NAMESPACE --selector "app.kubernetes.io/instance=$INSTANCE" --template '{{ (index .items 0).metadata.name}}')
kubectl port-forward --context $CONTEXT --namespace $NAMESPACE --address 0.0.0.0 "pod/$pod" $PORT_LOCAL:$PORT_REMOTE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment