<p>I have to set up an FTP server in a GCP Kubernetes cluster, and do not know how to route clients so that multiple requests from the same IP to different ports get routed to the same Kubernetes pod.</p>

<p>In the (passive) FTP protocol, the server opens up a new port, and sends the port number to the client. The client then creates a new connection to that port. Hence, I need to ensure that the second request gets routed to the same pod, because only on that pod a server is waiting for a new connection.</p>

<p>I tried a minimal sample with a workload of two pods that do nothing, and have the ports 21, and 30000-30098 exposed. Then, I set up service of type LoadBalancer as follows (reduced to the relevant portions):</p>

<pre><code>kind: Service
spec:
    type: LoadBalancer
    sessionAffinity: ClientIP
    loadBalancerIP: IP_OF_LB
    ports:
    - name: ftp-control
      port: 21
      protocol: TCP
      targetPort: 21
    - name: pasv-30000
      port: 30000
      protocol: TCP
      targetPort: 30000
      # and so on for the remaining ports up to 30098
</code></pre>

<p>Now I log onto each of the pods with a shell, and I start to listen manually on one port as follows:</p>

<pre><code>netcat -l -p 30001
</code></pre>

<p>Then, I use telnet from my workstation to connect to the IP address of the load balancer.</p>

<pre><code>telnet IP_OF_LB 30001
</code></pre>

<p>This way, I can see which pod gets the incoming connection request.</p>

<p>For a single port, the load balancer always forwards my request to the same pod.</p>

<p>However, when I try several ports, I can see that subsequent requests get routed to different pods, even though the session affinity is set to <code>ClientIP</code>.</p>

<p>Is there any setting that I have missed? I assume, that a session affinity by client IP would only use the IP of the client to determine the target pod. However, it looks as if it is using the IP and the port.</p>

<p>Does anyone know if there are more settings that I can try to get the desired behavior?</p>

Haplo
<p>I can see you configure your service as LoadBalancer type, however it would be troublesome to configure <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/configure-backend-service#setting_client_ip_affinity" rel="nofollow noreferrer">session affinity</a> on it. Therefore, I actually recommend to do the following:</p>

<p><a href="https://cloud.google.com/kubernetes-engine/docs/how-to/configure-backend-service#creating_a_backendconfig" rel="nofollow noreferrer">Backend config</a> -> <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/configure-backend-service#creating_a_service" rel="nofollow noreferrer">Nodeport service</a> -> <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/configure-backend-service#creating_an_ingress" rel="nofollow noreferrer">Ingress</a></p>

<p>Please check using <a href="https://cloud.google.com/load-balancing/docs/backend-service#client_ip_affinity" rel="nofollow noreferrer">client IP affinity</a> and <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/backendconfig#backendconfig_v1beta1_cloudgooglecom" rel="nofollow noreferrer">Backend Configs</a> for more information.</p>
