<p>I have the following pod setup:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: proxy-test
  namespace: test
spec:
  containers:
    - name: container-a
      image: &lt;Image&gt;
      imagePullPolicy: Always
      ports:
        - name: http-port
          containerPort: 8083
    - name: container-proxy
      image: &lt;Image&gt;
      ports:
        - name: server
          containerPort: 7487
          protocol: TCP
    - name: container-b
      image: &lt;Image&gt;
</code></pre>
<p>I <code>exec</code> into <code>container-b</code> and execute following curl request:</p>
<pre><code>curl --proxy localhost:7487 -X POST http://localhost:8083/
</code></pre>
<p>Due to some reason, <code>http://localhost:8083/</code> is directly getting called and proxy is ignored. Can someone explain why this can happen ?</p>

Ahmad.Masood
<h2>Environment</h2>
<p>I replicated the scenario on <code>kubeadm</code> and <code>GCP GKE</code> kubernetes clusters to see if there is any difference - no, they behave the same, so I assume AWS EKS should behave the same too.</p>
<p>I created a pod with 3 containers within:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: proxy-pod
spec:
  containers:
  - image: ubuntu # client where connection will go from
    name: ubuntu
    command: ['bash', '-c', 'while true ; do sleep 60; done']
  - name: proxy-container # proxy - that's obvious
    image: ubuntu
    command: ['bash', '-c', 'while true ; do sleep 60; done']
  - name: server # regular nginx server which listens to port 80
    image: nginx
</code></pre>
<p>For this test stand I installed <code>squid</code> proxy on <code>proxy-container</code> (<a href="https://ubuntu.com/server/docs/proxy-servers-squid" rel="nofollow noreferrer">what is squid and how to install it</a>). By default it listens to port <code>3128</code>.</p>
<p>As well as <code>curl</code> was installed on <code>ubuntu</code> - client container. (<code>net-tools</code> package as a bonus, it has <code>netstat</code>).</p>
<h2>Tests</h2>
<p><strong>Note!</strong></p>
<ul>
<li>I used <code>127.0.0.1</code> instead of <code>localhost</code> because <code>squid</code> has some resolving questions, didn't find an easy/fast solution.</li>
<li><code>curl</code> is used with <code>-v</code> flag for verbosity.</li>
</ul>
<p>We have <code>proxy</code> on <code>3128</code> and <code>nginx</code> on <code>80</code> within the pod:</p>
<pre><code># netstat -tulpn

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:3128            0.0.0.0:*               LISTEN      -
tcp6       0      0 :::80                   :::*                    LISTEN      -
</code></pre>
<p><code>curl</code> directly:</p>
<pre><code># curl 127.0.0.1 -vI

*   Trying 127.0.0.1:80... # connection goes directly to port 80 which is expected
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
&gt; HEAD / HTTP/1.1
&gt; Host: 127.0.0.1
&gt; User-Agent: curl/7.68.0
&gt; Accept: */*
</code></pre>
<p><code>curl</code> via proxy:</p>
<pre><code># curl --proxy 127.0.0.1:3128 127.0.0.1:80 -vI

*   Trying 127.0.0.1:3128... # connecting to proxy!
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3128 (#0) # connected to proxy
&gt; HEAD http://127.0.0.1:80/ HTTP/1.1 # going further to nginx on `80`
&gt; Host: 127.0.0.1
&gt; User-Agent: curl/7.68.0
&gt; Accept: */*
</code></pre>
<p><code>squid</code> logs:</p>
<pre><code># cat /var/log/squid/access.log

1635161756.048      1 127.0.0.1 TCP_MISS/200 958 GET http://127.0.0.1/ - HIER_DIRECT/127.0.0.1 text/html
1635163617.361      0 127.0.0.1 TCP_MEM_HIT/200 352 HEAD http://127.0.0.1/ - HIER_NONE/- text/html
</code></pre>
<h2>NO_PROXY</h2>
<p><code>NO_PROXY</code> environment variable might be set up, however by default it's empty.</p>
<p>I added it manually:</p>
<pre><code># export NO_PROXY=127.0.0.1

# printenv | grep -i proxy
NO_PROXY=127.0.0.1
</code></pre>
<p>Now <code>curl</code> request via proxy will look like:</p>
<pre><code># curl --proxy 127.0.0.1:3128 127.0.0.1 -vI

* Uses proxy env variable NO_PROXY == '127.0.0.1' # curl detects NO_PROXY envvar
*   Trying 127.0.0.1:80... # and ignores the proxy, connection goes directly
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
&gt; HEAD / HTTP/1.1
&gt; Host: 127.0.0.1
&gt; User-Agent: curl/7.68.0
&gt; Accept: */*
</code></pre>
<p>It's possible to override <code>NO_PROXY</code> envvar while executing <code>curl</code> command with <code>--noproxy</code> flag.</p>
<blockquote>
<p>--noproxy no-proxy-list</p>
<p>Comma-separated list of hosts which do not use a proxy, if one is specified. The only wildcard is a single *
character, which matches all hosts, and effectively disables the
proxy. Each name in this list is matched as either a domain which
contains the hostname, or the hostname itself. For example, local.com
would match local.com, local.com:80, and <a href="http://www.local.com" rel="nofollow noreferrer">www.local.com</a>, but not
<a href="http://www.notlocal.com" rel="nofollow noreferrer">www.notlocal.com</a>. (Added in 7.19.4).</p>
</blockquote>
<p>Example:</p>
<pre><code># curl --proxy 127.0.0.1:3128 --noproxy &quot;&quot; 127.0.0.1 -vI

*   Trying 127.0.0.1:3128... # connecting to proxy as it was supposed to
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3128 (#0) # connection to proxy is established
&gt; HEAD http://127.0.0.1/ HTTP/1.1 # connection to nginx on port 80
&gt; Host: 127.0.0.1
&gt; User-Agent: curl/7.68.0
&gt; Accept: */*
</code></pre>
<p>This proves that proxy works! with localhost.</p>
<p><strong>Another option</strong> is something incorrectly configured in <code>proxy</code> which is used in the question. You can get this pod and install <code>squid</code> and <code>curl</code> into both containers and try yourself.</p>
