<p>I'm trying to create a very simple Kubernetes project that includes communication between frontend client written in Reactjs +nginx and backend server written in Java + Spring boot.</p>
<p>I'm able to make this communication with docker-compose locally but when deploying it to gke I'm getting: failed (111: Connection refused)</p>
<p>on the backend I have:</p>
<p>controller:</p>
<pre><code>@RestController
@RequestMapping(&quot;/msgs&quot;)
@CrossOrigin
public class MsgController {

    @GetMapping(&quot;/getMsg&quot;)
    public String getMsg() {
        return &quot;hello from backend&quot;;
    }
}
</code></pre>
<p>Dockerfile</p>
<pre><code>FROM adoptopenjdk/openjdk11:alpine-jre

WORKDIR /opt/app
ARG JAR_FILE=target/*.jar

COPY ${JAR_FILE} app.jar

# java -jar /opt/app/app.jar
ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;app.jar&quot;]
</code></pre>
<p>deployment yml:</p>
<pre><code>Version: apps/v1
kind: Deployment
metadata:
  name: server-demo
spec:
  selector:
    matchLabels:
      app: server-demo
      tier: backend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: server-demo
        tier: backend
        track: stable
    spec:
      containers:
        - name: hello
          image: &quot;gcr.io/gcp-kub-course/server-demo:latest&quot;
          ports:
            - name: http
              containerPort: 4420
---
apiVersion: v1
kind: Service
metadata:
  name: server-demo
spec:
  selector:
    app: hello
    tier: backend
  ports:
  - protocol: TCP
    port: 4420
    targetPort: 4420
</code></pre>
<p>on the frontend side, I have</p>
<pre><code>const [msg, setMsg] = useState('');

useEffect(() =&gt; {
    fetch('/msgs/getMsg')
        .then(response =&gt; response.text())
        .then(m =&gt; {
            // console.log(JSON.stringify(m))
            setMsg(m)
        });
});


return &lt;div&gt;{msg}&lt;/div&gt;
</code></pre>
<p>Dockerfile:</p>
<pre><code>FROM node:10-alpine as a builder

COPY package.json package-lock.json ./

RUN npm install &amp;&amp; mkdir /react-ui &amp;&amp; mv ./node_modules ./react-ui

WORKDIR /react-ui

COPY . .

# Build the project and copy the files
RUN npm run build


FROM nginx:alpine

#!/bin/sh

COPY ./nginx/nginx.conf /etc/nginx/nginx.conf

RUN rm -rf /usr/share/nginx/html/*

COPY --from=builder /react-ui/build /usr/share/nginx/html

EXPOSE 3000 80

ENTRYPOINT [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p>nginx.conf:</p>
<pre><code>worker_processes  5;  ## Default: 1
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  ## Default: 1024
}

http {
    upstream client {

    }
    server {
        listen 80;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /msgs {
            proxy_pass http://server-demo:4420;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
        }
    }
}
</code></pre>
<p>and deployment yml:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: frontend-service
  labels:
    app: frontend-service
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    app: frontend-service
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-service
  namespace: default
  labels:
    app: frontend-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend-service
  template:
    metadata:
      labels:
        app: frontend-service
    spec:
      containers:
      - name: frontend-service
        image: gcr.io/gcp-kub-course/frontend-service:latest
        imagePullPolicy: &quot;Always&quot;
        ports:
        - name: http
          containerPort: 80
</code></pre>
<p>when looking at the services:</p>
<pre><code>kubectl get svc
NAME               TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)        AGE
frontend-service   LoadBalancer   10.24.15.122   34.121.100.70   80:32506/TCP   57m
kubernetes         ClusterIP      10.24.0.1      &lt;none&gt;          443/TCP        25h
server-demo        ClusterIP      10.24.4.49     &lt;none&gt;          4420/TCP       57m
</code></pre>
<p>when looking at the pods:</p>
<pre><code>frontend-service-bf9b4ccfd-jcjvm   1/1     Running   0          58m
server-demo-84df7f57c6-blgxq       1/1     Running   0          58m
</code></pre>
<p>and finally when looking at the frontend-service logs I see:</p>
<pre><code>2020/08/23 16:05:11 [error] 6#6: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 10.128.0.7, server: , request: &quot;GET /msgs/getMsg HTTP/1.1&quot;, upstream: &quot;http://10.24.4.49:4420/msgs/getMsg&quot;, host: &quot;34.121.100.70&quot;, referrer: &quot;http://34.121.100.70/&quot;
10.128.0.7 - - [23/Aug/2020:16:05:11 +0000] &quot;GET /msgs/getMsg HTTP/1.1&quot; 502 559 &quot;http://34.121.100.70/&quot; &quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36&quot;
</code></pre>
<p>I can see that the nginx did its job since its proxying the GET /msgs/getMsg to 10.24.4.49 which is demo-server IP, they are both on default namespace and I'm lost</p>

Lior Derei
<p>the second I posted it I saw my mistake:
at the end of the server-demo deployment yaml</p>
<p>I missed configured:</p>
<pre><code>selector:
  app: hello
</code></pre>
<p>which had to be:</p>
<pre><code>selector:
  app: server-demo
</code></pre>
