<h3>TL;DR</h3>
<p>java.net.SocketException: Network is unreachable (connect failed) in simple Kubernetes pod that can curl to the internet.</p>
<h3>Short Description</h3>
<p>I've setup a simple Job object in Kubernetes, which spawns a simple pod to use the Slack-Api to poll for a conversation history via slack.</p>
<p>Running this application locally or dockerized works like a charm. But when trying to execute it in Kubernetes I get a</p>
<pre><code>java.net.SocketException: Network is unreachable (connect failed)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:120)
</code></pre>
<h3>Kubernetes config</h3>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-app-job
spec:
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: name
              image: myimage
              imagePullPolicy: Always
              ports:
                - containerPort: 443
                  protocol: TCP
                - containerPort: 80
                  protocol: TCP
              env:
                - name: http_proxy
                  value: myproxy
                - name: https_proxy
                  value: myproxy
                - name: no_proxy
                  value: myproxy
          restartPolicy: OnFailure
</code></pre>
<p>Trying to debug what is happening, I noticed that I could curl something from my pod (so there's access to the public internet) but when I try to ping, i get Socket: Operation not permitted.</p>
<p>Eg:</p>
<pre><code>bash-4.2$ ping www.google.com
ping: socket: Operation not permitted
bash-4.2$ curl -I www.google.com
HTTP/1.1 200 OK
content-type: text/html; charset=ISO-8859-1
p3p: CP=&quot;This is not a P3P policy! See g.co/p3phelp for more info.&quot;
date: Wed, 29 Sep 2021 09:18:36 GMT
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
expires: Wed, 29 Sep 2021 09:18:36 GMT
cache-control: private
set-cookie: XXXXXXXXXXX
expires=Thu, 31-Mar-2022 09:18:36 GMT; path=/; domain=.google.com; HttpOnly
x-cache: MISS from XXXXXXXXXXX
x-cache-lookup: MISS fXXXXXXXXX

bash-4.2$ command terminated with exit code 137
</code></pre>
<p>I believe that I'm missing some configuration. I tried opening a port with a <code>NodePort</code> service but I had no success. Any ideas how to debug this?</p>

<p>Java does not inherit the proxy settings from the environment.</p>
<p>You need to specify the proxy using Java system properties</p>
<pre><code>-Dhttp.proxyHost=locahost -Dhttp.proxyPort=9900
</code></pre>
