Difference between revisions of "Kubectl port-forward"

From wikieduonline
Jump to navigation Jump to search
 
(40 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Draft}}
 
 
{{lowercase}}
 
{{lowercase}}
 +
<code>[[kubectl]] [[port-forward]]</code> [[forward]] one or more local ports to a [[pod]].
 +
* Man: https://www.mankier.com/1/kubectl-port-forward
 +
* <code>[[kubectl port-forward --help]]</code>
 +
* <code>[[kubectl]] [[port-forward]] [[deployment/]]mydeployment 8080:443</code>
 +
* <code>[[kubectl]] [[port-forward]] [[deployment/]]mydeployment 8080:443 &</code>
  
* <code>[[kubectl]] [[port-forward]]</code>
+
== Examples ==
* <code>kubectl --namespace default port-forward $POD_NAME 3000</code>
+
* [[Grafana]]: <code>kubectl --namespace default port-forward [[$POD_NAME]] [[3000]]</code>
* <code>kubectl port-forward [[svc]]/[[argocd]]-server -n argocd 8080:443</code>
+
* [[Prometheus]]:
 +
** <code>kubectl --namespace default port-forward $POD_NAME [[9090]]</code>
 +
** <code>[[kubectl port-forward]] --namespace default [[svc/]][[prometheus-kube-prometheus-prometheus]] [[9090]]:9090</code>
 +
** [[Alertmanager]]: <code>kubectl port-forward --namespace default svc/prometheus-kube-prometheus-alertmanager 9093:9093</code>
  
  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}")
+
  <code>[[kubectl port-forward svc/argocd-server -n argocd 8080:443]]</code>
 +
[[Forwarding from]] 127.0.0.1:8080 -> 8080
  
 +
[[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
 +
 +
kubectl -n your-namespace port-forward [[service]]/[[pritunl-ui]] 7111:443
 +
 +
 +
 +
List of ports, do not included <code>[[port-forward]]</code> 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
 
  kubectl --namespace default port-forward $[[POD_NAME]] 3000
 
  error: unable to forward port because [[pod is not running]]. Current status=Failed
 
  error: unable to forward port because [[pod is not running]]. Current status=Failed
  
 
  kubectl --namespace default port-forward $POD_NAME 3000
 
  kubectl --namespace default port-forward $POD_NAME 3000
  error: unable to forward port because pod is not running. Current status=Pending
+
  [[error: unable to forward port because pod is not running. Current status=Pending]]
 
 
kubectl --namespace default port-forward $POD_NAME 3000
 
Forwarding from 127.0.0.1:3000 -> 3000
 
Forwarding from [::1]:3000 -> 3000
 
  
 
  kubectl --namespace default port-forward $POD_NAME 3000
 
  kubectl --namespace default port-forward $POD_NAME 3000
Line 26: Line 47:
 
  See '[[kubectl port-forward -h]]' for help and examples
 
  See '[[kubectl port-forward -h]]' for help and examples
  
 +
kubectl port-forward service/my-argo-argocd-server -n default 8080:443
 +
[[error: timed out waiting for the condition]]
 +
 +
 +
[[bind: address alredy in use]] [[unable to create listener]]
 +
 +
Solution: <code>[[lsof -i]]</code>
  
 
== Related ==
 
== Related ==
 
* <code>[[kubectl get pods]]</code>
 
* <code>[[kubectl get pods]]</code>
 
* [[Port forwarding]]
 
* [[Port forwarding]]
* [[Kubernetes services]]
 
* <code>[[helm install]]</code>
 
 
* <code>[[kubectl proxy]]</code>
 
* <code>[[kubectl proxy]]</code>
 +
* [[Kubernetes networking]]
 +
* <code>[[kubectl get services]]</code>
 +
* <code>[[kubectl create service clusterip]]</code>
 +
* <code>[[minikube tunnel]]</code>
  
 
== See also ==
 
== See also ==
* {{kubectl}}
+
* {{kubectl port-forward}}
 +
* {{Kubernetes networking}}
 
* {{Kubernetes services}}
 
* {{Kubernetes services}}
  
 
[[Category:Kubernetes]]
 
[[Category:Kubernetes]]

Latest revision as of 07:39, 9 July 2024

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

Examples[edit]

kubectl port-forward svc/argocd-server -n argocd 8080:443
Forwarding from 127.0.0.1:8080 -> 8080
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

kubectl -n your-namespace port-forward service/pritunl-ui 7111:443


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[edit]

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
kubectl port-forward service/my-argo-argocd-server -n default 8080:443
error: timed out waiting for the condition


bind: address alredy in use unable to create listener

Solution: lsof -i

Related[edit]

See also[edit]

Advertising: