我们环境istio升级到最新版了,在未升级前1.8.2时就发现socket连接任何IP,任何端口时都是成功,这很不正常。
应用层面要发起socket连接时,会由操作系统在底层来完成TCP三次握手,当我们注入了istio时,此时pod的所有进出流量均由istio-proxy中的组件代理,此时给应用层造成的假像是连接成功,通过抓包我们发现只有出去的数据包,没有回包。
istio官方回应:不要过度依赖socket的connect状态,但我们没有找到更好的方法来判断是否连接成功,通过java和python测试均是如此。
Software version
kubernetes: 1.21.4
istio: 1.11.0
docker: 19.3.9
linux: CentOS Linux 7 (Core) 3.10.0-1160.11.1.el7.x86_64
有兴趣的同学可以按我的环境来测试一下
kubectl create ns test
kubectl label namespace test istio-injection=enabled
kubectl create deployment web --image=gozap/oraclejdk8
kubectl exec -it web-xxxxxxxx -n test -- sh
示例代码
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class HelloJava{
public static void main(String[] args){
System.out.println("Hello Java");
String host = "7.0.5.1";
int port = 6789;
System.out.println("check.connectable.host:"+ host +", port:" + port);
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(host, port));
} catch (Exception e) {
System.out.println("check.connectable.error:" + e);
return;
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("check.connectable.host: "+host+", port: "+port+", success");
}
}
Drive by: the tcp connection will be accepted by istio-proxy.
Don't rely on the socket connect() behavior.
问题原因定位后,是如何解决,从官方得知我们将某CIDR做一个排除。最后测试果然返回了ConnectionRefusedError: [Errno 111] Connection refused
如果你有更好的方法改进socket类,可以回贴
高啊,我也遇到了