<h1><strong>Problem</strong></h1>
<p>In our project, we want two pods working as server-client that communicate via the Python <code>socket</code> library. Both containers are built locally with <code>docker build</code>, pulled locally via <code>imagePullPolicy: IfNotPresent</code> on the yaml files and run on the same node of the k8s cluster (I'm running kubernetes vanilla, if that's important).</p>
<p>The communication works well when we</p>
<ul>
<li>run both python scripts in the command line</li>
<li>run both scripts as containers using <code>docker build</code> and <code>docker run</code></li>
<li>the server app container is deployed in the K8s cluster and the client app is run either on the command line or as a docker container.</li>
</ul>
<p>The communication fails when both server and client are deployed in K8s. <code>kubectl logs client -f</code> returns :</p>
<pre><code>Traceback (most recent call last):
  File &quot;client.py&quot;, line 7, in &lt;module&gt;
    client_socket.connect((IP_Server,PORT_Server))
TimeoutError: [Errno 110] Connection timed out
</code></pre>
<p>I suspect there's a problem with the outgoing request from the client script when it's deployed on the cluster, but I can't find where the problem lies.</p>
<h1><strong>Codes</strong></h1>
<p><strong>server.py</strong></p>
<pre><code>import socket

IP = &quot;0.0.0.0&quot;
PORT = 1234

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen()

...
</code></pre>
<p><strong>server.yaml</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: server
  labels:
    app: server
spec:
  ports:
    - port: 1234
      targetPort: 1234
      protocol: TCP
  selector:
    app: server
---
apiVersion: v1
kind: Pod
metadata:
  name: server
  labels:
    app: server
spec:
  containers:
  - name: server
    image: server:latest
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 1234
</code></pre>
<p><strong>client.py</strong></p>
<pre><code>import socket

IP_Server = # the IP of the server service, obtained from &quot;kubectl get svc&quot; 
PORT_Server = 1234

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP_Server,PORT_Server)) # fails here

...
</code></pre>
<p><strong>client.yaml</strong></p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: client
  labels:
    app: client
spec:
  containers:
  - name: client
    image: client:latest
    imagePullPolicy: IfNotPresent
</code></pre>

waldowe
<p>In case anyone gets here in the future, I found a solution that worked for me by executing</p>
<pre><code>iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
</code></pre>
<p>and then deleting all coredns pods.</p>
<p>Reference : <a href="https://github.com/kubernetes/kubernetes/issues/86762#issuecomment-836338017" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/86762#issuecomment-836338017</a></p>
