Erkundung des kubectl port-forward
-Befehls
Der kubectl port-forward
-Befehl ermöglicht es Ihnen, einen oder mehrere lokale Ports an einen Pod, eine Bereitstellung (Deployment) oder einen Service in Ihrem Kubernetes-Cluster weiterzuleiten. Er wird üblicherweise zum Testen und Debuggen von Services verwendet, ohne sie nach außen hin sichtbar zu machen.
Führen Sie den folgenden Befehl aus, um die verfügbaren Optionen für kubectl port-forward
anzuzeigen:
kubectl port-forward -h
Sie werden die folgende Ausgabe sehen:
Forward one or more local ports to a pod.
Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.
If there are multiple pods matching the criteria, a pod will be selected automatically. The forwarding session ends
when the selected pod terminates, and a rerun of the command is needed to resume forwarding.
Examples:
## Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
kubectl port-forward pod/mypod 5000 6000
## Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
kubectl port-forward deployment/mydeployment 5000 6000
## Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by the service
kubectl port-forward service/myservice 8443:https
## Listen on port 8888 locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod 8888:5000
## Listen on port 8888 on all addresses, forwarding to 5000 in the pod
kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000
## Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000
## Listen on a random port locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod :5000