kubectl 使用
原创大约 3 分钟...
端口转发
查看帮助 "kubectl port-forward --help"
kubectl port-forward --help
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
Options:
--address=[localhost]: Addresses to listen on (comma separated). Only accepts IP addresses or localhost as a
value. When localhost is supplied, kubectl will try to bind on both 127.0.0.1 and ::1 and will fail if neither of these
addresses are available to bind.
--pod-running-timeout=1m0s: The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one
pod is running
Usage:
kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]
Use "kubectl options" for a list of global command-line options (applies to all commands).
# 使用
# TYPE 可选: pod/deployment/service/svc, 默认: pod
kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]
# 举例
# 查看 myns 下的 deployment
kubectl get deployments -n=myns
NAME READY UP-TO-DATE AVAILABLE AGE
redis 3/3 3 3 39m
# 查看 myns 下的 service
kubectl get svc -n=myns
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-svc ClusterIP 10.98.249.0 <none> 6379/TCP 39m
# 查看 myns 下的 pod
kubectl get po -n=myns
NAME READY STATUS RESTARTS AGE
redis-6bdd7f9b9c-fr8dv 1/1 Running 0 33m
redis-6bdd7f9b9c-jfmx7 1/1 Running 0 33m
redis-6bdd7f9b9c-v8nkz 1/1 Running 0 33m
# 将 [命名空间: myns, 名称: redis 的 deployment] 的 6379 端口映射到宿主机的 26379
kubectl port-forward -n myns deployment/redis 26379:6379
# 将 [命名空间: myns, 名称: redis-svc 的 service] 的 6379 端口映射到宿主机的 26379
kubectl port-forward -n myns service/redis-svc 26379:6379
# 或
kubectl port-forward -n myns svc/redis-svc 26379:6379
# 将 [命名空间: myns, 名称: redis-6bdd7f9b9c-fr8dv 的 pod] 的 6379 端口映射到宿主机的 26379
kubectl port-forward -n myns pod/redis-6bdd7f9b9c-fr8dv 26379:6379
# 或
kubectl port-forward -n myns redis-6bdd7f9b9c-fr8dv 26379:6379