如何在没有互联网访问的情况下从集群外部通过 SSH 连接到 Pod? (K8S)

如何在没有互联网访问的情况下从集群外部通过 SSH 连接到 Pod? (K8S)

我创建了一个 K8S 集群(2 个节点和 1 个主节点),该集群无法访问互联网,只有试验机可以访问互联网。

我想从集群外部通过 SSH 连接到 Ubuntu-OpenSSH Pod 并进行端口转发:

kubectl port-forward pod/ubuntu-ssh 30180:22

openssh-server 在 pod 内监听的30180容器端口在哪里。22

但当我尝试连接集群 IP 时出现错误:

$  ssh [email protected] -p 30180
ssh: connect to host 192.X.X.X port 30180: Connection refused

但如果我使用 localhost 执行此操作,它就可以正常工作:

$ ssh root@localhost -p 30180
root@localhost's password:
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.15.0-25-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
Last login: Fri Jul  1 12:39:19 2022 from 127.0.0.1
root@ubuntu-ssh:~#

有人可以帮助我使用 ip 地址而不是 localhost 连接到 pod 吗?提前致谢 !

有关更多背景信息,我遵循了本教程: https://sanda-dev.medium.com/ssh-into-kubernetes-pod-without-public-ip-access-fbb9da7f7b26

答案1

看一下 的输出kubectl port-forward --help,您会看到:

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.

默认情况下,打开的端口kubectl port-forward仅绑定到localhost,因此它仅适用于源自本地计算机的连接。如果您想将其公开在运行命令的计算机之外port-forward,请添加:

kubectl port-forward --address=0.0.0.0 ...

0.0.0.0意味着“所有地址”,因此它将可用于任何可用接口中的连接。

相关内容