我正在尝试将客户端流量定向到正在侦听的 kubernetes 集群 NodePort 192.168.1.100.30000
。
客户需要发出请求,192.168.1.100.8000
所以我在 iptables 中添加了以下 REDIRECT 规则:
iptables -t nat -I PREROUTING -p tcp --dst 192.168.1.100 --dport 8000 -j REDIRECT --to-port 30000
然后我发出一个 curl,192.168.1.100:8000
但是,在 tcpdump 中我看到了一个不同的端口:
# tcpdump -i lo -nnvvv host 192.168.1.100 and port 8000
tcpdump: listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
[Interface: lo] 20:39:22.685968 IP (tos 0x0, ttl 64, id 20590, offset 0, flags [DF], proto TCP (6), length 40)
[Interface: lo] 192.168.1.100.8000 > 192.168.1.100.49816: Flags [R.], cksum 0xacda (correct), seq 0, ack 3840205844, win 0, length 0
[Interface: lo] 20:39:37.519256 IP (tos 0x0, ttl 64, id 34221, offset 0, flags [DF], proto TCP (6), length 40)
我希望 tcpdump 显示类似
192.168.1.100.8000 > 192.168.1.100.30000
但是,它显示并导致连接被拒绝错误,因为没有进程列在192.168.1.100.49816
.
192.168.1.100.8000 > 192.168.1.100.49816
我正在使用测试环境,所以我无法访问远程设备,这就是我curl
用来测试 iptables REDIRECT 路径的原因。
添加 REDIRECT 规则是否会导致 tcpdump 将流量重定向到指定端口以外的其他端口?
编辑:
在@AB 建议后添加了以下 OUTPUT 规则:
iptables -t nat -I OUTPUT -d 192.168.1.100 -p tcp --dport 8000 -j REDIRECT --to-port 30000
并且 curl 确实继续进行,OUTPUT 链的数据包计数确实增加了(虽然 PREROUTING REDIRECT 链数据包没有增加):
2 10 600 REDIRECT tcp -- * * 0.0.0.0/0 192.168.1.100 tcp dpt:8000 redir ports 30000
但是,收到以下错误:
# curl -vk https://192.168.1.100:8000/v1/api
* About to connect() to 192.168.1.100 port 8000 (#0)
* Trying 192.168.1.100...
* Connected to 192.168.1.100 (192.168.1.100) port 8000 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* NSS error -12263 (SSL_ERROR_RX_RECORD_TOO_LONG)
* SSL received a record that exceeded the maximum permissible length.
* Closing connection 0
curl: (35) SSL received a record that exceeded the maximum permissible length.
另外,尝试添加一个远程系统网络,这次 PREROUTING REDIRECT CHAIN 数据包计数在执行后增加remotesystem curl ...
(但 OUTPUT CHAIN 不增加):
2 34 2040 REDIRECT tcp -- * * 0.0.0.0/0 172.16.128.1 tcp dpt:8000 redir ports 30000
错误:
# ip netns exec remotesystem curl -vk https://192.168.1.100:8000/v1/api
* About to connect() to 192.168.1.100 port 8000 (#0)
* Trying 192.168.1.100...
* Connection timed out
* Failed connect to 192.168.1.100:8000; Connection timed out
* Closing connection 0
curl: (7) Failed connect to 192.168.1.100:8000; Connection timed out