kubectl port-forward

概要

将一个或多个本地端口转发到 Pod。

使用资源类型/名称,例如 deployment/mydeployment 来选择一个 pod。如果省略,资源类型默认为“pod”。

如果存在多个匹配标准的 pod,将自动选择一个 pod。当选定的 pod 终止时,转发会话结束,需要重新运行命令才能恢复转发。

kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]

示例

  # 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

选项

--address strings     默认值: "localhost"

要侦听的地址(逗号分隔)。仅接受 IP 地址或 localhost 作为值。当提供 localhost 时,kubectl 将尝试绑定到 127.0.0.1 和 ::1,如果这两个地址都无法绑定,则会失败。

-h, --help

port-forward 的帮助信息

--pod-running-timeout duration     默认值:1m0s

等待至少一个 Pod 运行的时间长度(例如,5s、2m 或 3h,大于零)

继承的父选项

--as string

用于执行操作的用户名。用户可以是命名空间中的普通用户或服务帐户。

--as-group strings

用于执行操作的组,可以重复此标志以指定多个组。

--as-uid string

用于执行操作的 UID。

--as-user-extra strings

用于操作的额外用户,可以重复此标志以指定同一键的多个值。

--cache-dir string     默认值: "$HOME/.kube/cache"

默认缓存目录

--certificate-authority string

用于证书颁发机构的证书文件的路径

--client-certificate string

用于 TLS 的客户端证书文件的路径

--client-key string

用于 TLS 的客户端密钥文件的路径

--cluster string

要使用的 kubeconfig 集群的名称

--context string

要使用的 kubeconfig 上下文的名称

--disable-compression

如果为 true,则选择退出对服务器的所有请求的响应压缩

--insecure-skip-tls-verify

如果为 true,则不会检查服务器证书的有效性。这将使您的 HTTPS 连接不安全

--kubeconfig string

用于 CLI 请求的 kubeconfig 文件的路径。

--kuberc string

要使用的 kuberc 文件的路径。可以通过导出 KUBECTL_KUBERC=false 功能门或关闭 KUBERC=off 功能来禁用此功能。

--match-server-version

要求服务器版本与客户端版本匹配

-n, --namespace string

如果存在,则此 CLI 请求的命名空间范围

--password string

用于与 API 服务器进行基本身份验证的密码

--profile string     默认值: "none"

要捕获的名称之一 (none|cpu|heap|goroutine|threadcreate|block|mutex|trace)

--profile-output string     默认值: "profile.pprof"

要写入配置文件的文件名

--request-timeout string     默认值: "0"

放弃单个服务器请求之前等待的时间长度。非零值应包含相应的时间单位(例如 1s、2m、3h)。值为零表示不超时请求。

-s, --server string

Kubernetes API 服务器的地址和端口

--storage-driver-buffer-duration duration     默认值: 1m0s

存储驱动程序中的写入将缓冲此持续时间,并作为单个事务提交到非内存后端

--storage-driver-db string     默认值: "cadvisor"

数据库名称

--storage-driver-host string     默认值: "localhost:8086"

数据库主机:端口

--storage-driver-password string     默认值: "root"

数据库密码

--storage-driver-secure

使用与数据库的安全连接

--storage-driver-table string     默认值: "stats"

表名

--storage-driver-user string     默认值: "root"

数据库用户名

--tls-server-name string

用于服务器证书验证的服务器名称。如果未提供,则用于联系服务器的主机名将被使用

--token string

用于与 API 服务器进行身份验证的 Bearer 令牌

--user string

要使用的 kubeconfig 用户名

--username string

用于与 API 服务器进行基本身份验证的用户名

--version version[=true]

--version, --version=raw 打印版本信息并退出;--version=vX.Y.Z... 设置报告的版本

--warnings-as-errors

将从服务器接收到的警告视为错误并以非零退出代码退出

参见

  • kubectl - kubectl 控制 Kubernetes 集群管理器

本页面是自动生成的。

如果您打算报告此页面的问题,请在您的问题描述中提及该页面是自动生成的。修复可能需要在 Kubernetes 项目的其他地方进行。

上次修改时间:2025年12月21日 下午5:18 PST:更新 v1.35 kubectl 参考 (fd1e78a94a)