<p>I'm trying to create a simple microservice, where a JQuery app in one Docker container uses this code to get a JSON object from another (analytics) app that runs in a different container:</p>
<pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){
$('#get-info-btn').click(function(){
  $.get(&quot;http://localhost:8084/productinfo&quot;, 
  function(data, status){          
    $.each(data, function(i, obj) {
      //some code
    });   
  });
});
});
&lt;/script&gt; 
</code></pre>
<p>The other app uses this for the <code>Deployment</code> containerPort.</p>
<pre><code>  ports:
    - containerPort: 8082
</code></pre>
<p>and these for the <code>Service</code> ports.</p>
<pre><code>  type: ClusterIP
  ports:
    - targetPort: 8082
      port: 8084   
</code></pre>
<p>The 'analytics' app is a golang program that listens on 8082.</p>
<pre><code>func main() {
    http.HandleFunc(&quot;/productinfo&quot;, getInfoJSON)    
    log.Fatal(http.ListenAndServe(&quot;:8082&quot;, nil))
}
</code></pre>
<p>When running this on Minikube, I encountered issues with CORS, which was resolved by using this in the golang code when returning a JSON object as a response:</p>
<pre><code>w.Header().Set(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;)
w.Header().Set(&quot;Access-Control-Allow-Headers&quot;, &quot;Content-Type&quot;) 
</code></pre>
<p>All this worked fine on Minikube (though in Minikube I was using <code>localhost:8082</code>). The first app would send a GET request to <code>http://localhost:8084/productinfo</code> and the second app would return a JSON object.</p>
<p>But when I tried it on a cloud Kubernetes setup by accessing the first app via :, when I open the browser console, I keep getting the error <code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8084/productinfo</code>.</p>
<p><strong>Question:</strong>
Why is it working on Minikube but not on the cloud Kubernetes worker nodes? Is using <code>localhost</code> the right way to access another container? How can I get this to work? How do people who implement microservices use their GET and POST requests across containers? All the microservice examples I found are built for simple demos on Minikube, so it's difficult to get a handle on this nuance.</p>

<p>@P.... is absolutely right, I just want to provide some more details about <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">DNS for Services</a> and communication between containers in the same Pod.</p>
<h4>DNS for Services</h4>
<p>As we can find in the <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services" rel="nofollow noreferrer">documentation</a>, Kubernetes Services are assigned a DNS A (or AAAA) record, for a name of the form <code>&lt;serviceName&gt;.&lt;namespaceName&gt;.svc.&lt;cluster-domain&gt;</code>. This resolves to the cluster IP of the Service.</p>
<blockquote>
<p>&quot;Normal&quot; (not headless) Services are assigned a DNS A or AAAA record, depending on the IP family of the service, for a name of the form my-svc.my-namespace.svc.cluster-domain.example. This resolves to the cluster IP of the Service.</p>
</blockquote>
<p>Let's break down the form <code>&lt;serviceName&gt;.&lt;namespaceName&gt;.svc.&lt;cluster-domain&gt;</code> into individual parts:</p>
<ul>
<li><p><code>&lt;serviceName&gt;</code> - The name of the Service you want to connect to.</p>
</li>
<li><p><code>&lt;namespaceName&gt;</code> - The name of the Namespace in which the Service to which you want to connect resides.</p>
</li>
<li><p><code>svc</code> - This should not be changed - <code>svc</code> stands for Service.</p>
</li>
<li><p><code>&lt;cluster-domain&gt;</code> - cluster domain, by default it's <code>cluster.local</code>.</p>
</li>
</ul>
<p>We can use <code>&lt;serviceName&gt;</code> to access a Service in the same Namespace, however we can also use <code>&lt;serviceName&gt;.&lt;namespaceName&gt;</code> or <code>&lt;serviceName&gt;.&lt;namespaceName&gt;.svc</code> or FQDN <code>&lt;serviceName&gt;.&lt;namespaceName&gt;.svc.&lt;cluster-domain&gt;</code>.</p>
<p>If the Service is in a different Namespace, a single <code>&lt;serviceName&gt;</code> is not enough and we need to use  <code>&lt;serviceName&gt;.&lt;namespaceName&gt;</code> (we can also use: <code>&lt;serviceName&gt;.&lt;namespaceName&gt;.svc</code> or <code>&lt;serviceName&gt;.&lt;namespaceName&gt;.svc.&lt;cluster-domain&gt;</code>).</p>
<p>In the following example, <code>app-1</code> and <code>app-2</code> are in the same Namespace and <code>app-2</code> is exposed with ClusterIP on port <code>8084</code> (as in your case):</p>
<pre><code>$ kubectl run app-1 --image=nginx
pod/app-1 created

$ kubectl run app-2 --image=nginx
pod/app-2 created

$ kubectl expose pod app-2 --target-port=80 --port=8084
service/app-2 exposed

$ kubectl get pod,svc
NAME        READY   STATUS    RESTARTS   AGE
pod/app-1   1/1     Running   0          45s
pod/app-2   1/1     Running   0          41s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
service/app-2        ClusterIP   10.8.12.83   &lt;none&gt;        8084/TCP   36s
</code></pre>
<p><strong>NOTE:</strong> The <code>app-2</code> is in the same Namespace as <code>app-1</code>, so we can use  <code>&lt;serviceName&gt;</code> to access it from <code>app-1</code>, you can also notice that we got the FQDN for <code>app-2</code> (<code>app-2.default.svc.cluster.local</code>):</p>
<pre><code>$ kubectl exec -it app-1 -- bash
root@app-1:/# nslookup app-2
Server:         10.8.0.10
Address:        10.8.0.10#53

Name:   app-2.default.svc.cluster.local
Address: 10.8.12.83
</code></pre>
<p><strong>NOTE:</strong> We need to provide the port number because <code>app-2</code> is listening on <code>8084</code>:</p>
<pre><code>root@app-1:/# curl app-2.default.svc.cluster.local:8084
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
...
</code></pre>
<p>Let's create <code>app-3</code> in a different Namespace and see how to connect to it from <code>app-1</code>:</p>
<pre><code>$ kubectl create ns test-namespace
namespace/test-namespace created

$ kubectl run app-3 --image=nginx -n test-namespace
pod/app-3 created

$ kubectl expose pod app-3 --target-port=80 --port=8084 -n test-namespace
service/app-3 exposed
</code></pre>
<p><strong>NOTE:</strong> Using <code>app-3</code> (<code>&lt;serviceName&gt;</code>) is not enough, we also need to provide the name of the Namespace in which <code>app-3</code> resides (<code>&lt;serviceName&gt;.&lt;namespaceName&gt;</code>):</p>
<pre><code># nslookup app-3
Server:         10.8.0.10
Address:        10.8.0.10#53

** server can't find app-3: NXDOMAIN

# nslookup app-3.test-namespace
Server:         10.8.0.10
Address:        10.8.0.10#53

Name:   app-3.test-namespace.svc.cluster.local
Address: 10.8.12.250

# curl app-3.test-namespace.svc.cluster.local:8084
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
...
</code></pre>
<h4>Communication Between Containers in the Same Pod</h4>
<p>We can use <code>localhost</code> to communicate with other containers, but <strong>only</strong> within the same Pod (Multi-container pods).</p>
<p>I've created a simple multi-container Pod with two containers: <code>nginx-container</code> and <code>alpine-container</code>:</p>
<pre><code>$ cat multi-container-app.yml
apiVersion: v1
kind: Pod
metadata:
  name: multi-container-app
spec:
  containers:
  - image: nginx
    name: nginx-container
  - image: alpine
    name: alpine-container
    command: [&quot;sleep&quot;, &quot;3600&quot;]

$ kubectl apply -f multi-container-app.yml
pod/multi-container-app created
</code></pre>
<p>We can connect to the <code>alpine-container</code> container and check if we can access the nginx web server located in the <code>nginx-container</code> with <code>localhost</code>:</p>
<pre><code>$ kubectl exec -it multi-container-app -c alpine-container -- sh

/ # 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 :::80                   :::*                    LISTEN      -

/ # curl localhost
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
...
</code></pre>
<p>More information on communication between containers in the same Pod can be found <a href="https://stackoverflow.com/questions/67061603/how-to-communicate-between-containers-in-same-pod-in-kubernetes">here</a>.</p>
