首页 > 解决方案 > Kubernetes 命名空间中的 netcat 监听 pod 无法连接

问题描述

我正在使用 weave-net 运行 kubernetes v 19.4(图片:weaveworks/weave-npc:2.7.0)默认命名空间中没有活动的网络策略

我想在 pod1 的 8080 端口上运行一个 netcat 监听器,并且想通过 pod2 连接到 pod1 的 8080 端口

[root@node01 ~]# kubectl run pod1 -i -t --image=ubuntu -- /bin/bash
If you don't see a command prompt, try pressing enter.
root@pod1:/# apt update ; apt install netcat-openbsd -y            
........
root@pod1:/# nc -l -p 8080

我通过以下方式验证端口正在侦听 pod1:

root@node01 ~]# kubectl exec -i -t pod1 -- /bin/bash 
root@pod1:/# apt install net-tools -y 
...........
root@pod1:/# netstat -tulpen  
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      0          213960     263/nc

root@pod1:/# ifconfig             
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1376
        inet 10.32.0.3  netmask 255.240.0.0  broadcast 10.47.255.255
        ether a2:b9:3e:bc:6e:25  txqueuelen 0  (Ethernet)
        RX packets 8429  bytes 17438639 (17.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4217  bytes 284639 (284.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

              

我在上面安装了 pod2 和 netcat:

[root@node01 ~]# kubectl run pod2 -i -t --image=ubuntu -- /bin/bash 
If you don't see a command prompt, try pressing enter.
root@pod2:/# apt update ; apt install netcat-openbsd -y 

我在 pod2 的 pod1 上测试我的 netcat 监听器:

root@pod2:/# nc 10.32.0.3 8080
....times out

所以我决定在 pod1 上创建一个 8080 端口的服务:

kubectl expose pod pod1 --port=8080 ; kubectl get svc ; kubectl get netpol 

[root@node01 ~]# kubectl expose pod pod1 --port=8080 ; kubectl get svc 
service/pod1 exposed
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
apache       ClusterIP   10.104.218.123   <none>        80/TCP     20d
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    21d
nginx        ClusterIP   10.98.221.196    <none>        80/TCP     13d
pod1         ClusterIP   10.105.194.196   <none>        8080/TCP   2s
No resources found in default namespace.

现在通过服务从 pod2 重试:

ping pod1   
PING pod1.default.svc.cluster.local (10.105.194.196) 56(84) bytes of data.

root@pod2:/# nc pod1 8080
....times out

我也用常规的 netcat 包试过这个。

为了更好地衡量,我尝试将 pod 上的端口 8080 公开为节点端口:

root@node01 ~]# kubectl delete svc pod1 ; kubectl expose pod pod1 --port=8080 --type=NodePort ; kubectl get svc 

So when i try to access that port from outside kubernetes i am unable to connect, for good measure i also test the ssh port to verify my base connectivity is ok
user@DESKTOP-7TIH9:~$ nc -zv 10.10.70.112 30743
nc: connect to 10.10.70.112 port 30743 (tcp) failed: Connection refused
user@DESKTOP-7TIH9:~$ nc -zv 10.10.70.112 22
Connection to 10.10.70.112 22 port [tcp/ssh] succeeded!

任何人都可以告诉我我是否在做某事,有错误的期望或建议我如何继续。先感谢您。

标签: kubernetesnetcat

解决方案


为了解决这个问题,我以某种方式决定在 k8s 主机上启用防火墙。这导致我遇到了一个损坏的集群。我决定重新启动集群,确保所有 fw 端口都已打开。包括这个:https ://www.weave.works/docs/net/latest/faq#ports

现在一切正常1


推荐阅读