<p>I have configured Kubernetes service (of type <code>LoadBalancer</code>) hosted on AWS EKS. The service's pods run in interactive mode (which is needed to run the container in the pod). The container in there is running a simple Java .jar application that receives input and produces output based on that.</p>

<p><strong>The question</strong>: how can I pass input to Java application inside the Kubernetes container?</p>

<p>Here is the part of <em>yaml</em> configuration of the pod that ensures it runs in an interactive mode:</p>

<pre><code>spec:
  containers:
  - image: ardulat/mckinsey
    imagePullPolicy: Always
    name: anuar-mckinsey
    ports:
    - containerPort: 8080
      name: http
      protocol: TCP
    stdin: true
    tty: true
</code></pre>

<p>The pod is up and running. As you see from the configuration, it receives TCP connections.</p>

<p>Here is the service of the pod:</p>

<pre><code>NAME             TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)          AGE
anuar-mckinsey   LoadBalancer   10.100.239.207   a8154210d09da11ea9c3806983848f2f-1085657314.us-east-2.elb.amazonaws.com   8080:32516/TCP
</code></pre>

<p>I can ensure that the pod and service are configured correctly by looking at the logs of the pod that output a print message from Java application in the container.</p>

<p><strong>Already tried</strong>:</p>

<ul>
<li>curl application in a verbose mode that produces:</li>
</ul>

<pre><code>*   Trying 18.190.142.22...
* TCP_NODELAY set
* Connected to a8154210d09da11ea9c3806983848f2f-1085657314.us-east-2.elb.amazonaws.com (18.190.142.22) port 8080 (#0)
&gt; GET / HTTP/1.1
&gt; Host: a8154210d09da11ea9c3806983848f2f-1085657314.us-east-2.elb.amazonaws.com:8080
&gt; User-Agent: curl/7.63.0
&gt; Accept: */*
&gt;
* Empty reply from server
* Connection #0 to host a8154210d09da11ea9c3806983848f2f-1085657314.us-east-2.elb.amazonaws.com left intact
curl: (52) Empty reply from server
</code></pre>

<ul>
<li>connecting via netcat in a verbose mode that produces the output:</li>
</ul>

<pre><code>found 1 connections:
     1: flags=82&lt;CONNECTED,PREFERRED&gt;
    outif en0
    src 172.20.22.204 port 51144
    dst 18.190.142.22 port 8080
    rank info not available
    TCP aux info available

Connection to a8154210d09da11ea9c3806983848f2f-1085657314.us-east-2.elb.amazonaws.com port 8080 [tcp/http-alt] succeeded!
</code></pre>

<p><strong>Final thoughts</strong>: so it seems like the configurations are right and the service runs on the cluster hosted on AWS EKS. The logs produce the right output. However, I can't send the message (through TCP connection?) to the Java application inside the container. Any thoughts on how I can pass the input to it?</p>

Anuar Maratkhan
<p>Based on the discussion in comments, the information was that the Java application uses <code>System.in</code> to receive the input. That is usually equivalent to <code>stdin</code>.</p>

<p>A way to send input to the application in the container and get output is by <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#attach" rel="nofollow noreferrer">attaching</a> to the process in the container in raw terminal mode, which connects the local <code>stdin</code> and <code>stdout</code> with the container's:</p>

<pre><code>kubectl -n &lt;namespace&gt; attach &lt;pod-name&gt; -c &lt;container-name&gt; -i -t
</code></pre>

<p>On the additional question about how to access the application from the outside without accessing pods directly and by using a Service, I'll suggest:</p>

<ul>
<li>Change the application so that it creates a <code>ServerSocket</code> to listen and accept connections at a particular port number.</li>
<li>Read from the socket's input stream individual lines and have them processed as if the input were from <code>System.in</code>.</li>
<li>Write the result to socket's output stream.</li>
<li>Expose the port via a LoadBalancer Kubernetes Service like you already had.</li>
</ul>

<p><a href="https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="nofollow noreferrer">This here</a> could be a good starting point for the new code. You can use <code>telnet 127.0.0.1 &lt;port&gt;</code> or <code>curl telnet://127.0.0.1:&lt;port&gt;/</code> to test the application locally and when not running it in a container.</p>

<p>Exposing an HTTP-based service instead, in my opinion, seems like an overkill for this application.</p>
