kubectl port-forward

From wikieduonline
Revision as of 12:06, 21 July 2022 by Ant (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

kubectl port-forward forward one or more local ports to a pod.

Examples

export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}")
kubectl --namespace default port-forward $POD_NAME 3000
Forwarding from 127.0.0.1:3000 -> 3000
Forwarding from [::1]:3000 -> 3000
kubectl -n default port-forward $POD_NAME 8443:8443
Forwarding from 127.0.0.1:8443 -> 8443
Forwarding from [::1]:8443 -> 8443


List of ports, do not included port-forward ports:

kubectl get svc --all-namespaces -o json | jq '.items[] | {name:.metadata.name, p:.spec.ports[] } | select( .p.nodePort != null ) | "\(.name): localhost:\(.p.nodePort) -> \(.p.port) -> \(.p.targetPort)"'

Examples with errors

kubectl --namespace default port-forward $POD_NAME 3000
error: unable to forward port because pod is not running. Current status=Failed
kubectl --namespace default port-forward $POD_NAME 3000
error: unable to forward port because pod is not running. Current status=Pending
kubectl --namespace default port-forward $POD_NAME 3000
error: You must be logged in to the server (Unauthorized)
kubectl --namespace default port-forward $POD_NAME 3000
error: TYPE/NAME and list of ports are required for port-forward
See 'kubectl port-forward -h' for help and examples


bind: address alredy in use unable to create listener

Solution: lsof -i

Related

See also

Advertising: