kubectl port-forward配置

本文详细介绍了如何使用Kubernetes的port-forward命令,解决容器22端口通过代理方式暴露到宿主的问题。虽然该命令默认仅能将端口绑定到localhost,但通过一系列的软件安装与配置,如socat和nsenter的使用,仍能实现对外部网络的访问,包括通过VPN或代理服务器。文中还提供了具体的操作步骤和命令实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在看到port-forward的功能时非常的兴奋,因为可以解决一个容器22端口通过代理的方式暴露到宿主,经过一列软件的安装测试后发现,容器端口只能暴露到localhost网卡,好坑,但是还是记录一下整个操作过程,还可以通过VPN或者代理服务器访问

+

port-forward命令参数说明

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

Examples:
  # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
  kubectl port-forward mypod 5000 6000

  # Listen on port 8888 locally, forwarding to 5000 in the pod
  kubectl port-forward mypod 8888:5000

  # Listen on a random port locally, forwarding to 5000 in the pod
  kubectl port-forward mypod :5000

Options:
  -p, --pod='': Pod name

Usage:
  kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] [options]
  • 看完参数说明后你应该觉得他无法bind指定网卡啦,但是还是希望你们看一下官方文档,因为这些命令会继一些命令的

port-forward辅助工具安装

  • 安装socat

yum -y install socat

  • 安装nsenter
$  wget https://www.kernel.org/pub/linux/utils/util-linux/v2.27/util-linux-2.27.tar.gz
$ tar -zxvf util-linux-2.27.tar.gz
$ cd util-linux-2.27
$ ./configure --without-ncurses && make nsenter
cp nsenter /usr/local/bin
  • 测试port-forward命令
kubectl port-forward $POD_NAME hostPort:containerPort
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值