0
votes

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.

I'm able to make this communication with docker-compose locally but when deploying it to gke I'm getting: failed (111: Connection refused)

on the backend I have:

controller:

@RestController
@RequestMapping("/msgs")
@CrossOrigin
public class MsgController {

    @GetMapping("/getMsg")
    public String getMsg() {
        return "hello from backend";
    }
}

Dockerfile

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 ["java","-jar","app.jar"]

deployment yml:

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: "gcr.io/gcp-kub-course/server-demo:latest"
          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

on the frontend side, I have

const [msg, setMsg] = useState('');

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


return <div>{msg}</div>

Dockerfile:

FROM node:10-alpine as a builder

COPY package.json package-lock.json ./

RUN npm install && mkdir /react-ui && 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 ["nginx", "-g", "daemon off;"]

nginx.conf:

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;
        }
    }
}

and deployment yml:

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: "Always"
        ports:
        - name: http
          containerPort: 80

when looking at the services:

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      <none>          443/TCP        25h
server-demo        ClusterIP      10.24.4.49     <none>          4420/TCP       57m

when looking at the pods:

frontend-service-bf9b4ccfd-jcjvm   1/1     Running   0          58m
server-demo-84df7f57c6-blgxq       1/1     Running   0          58m

and finally when looking at the frontend-service logs I see:

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: "GET /msgs/getMsg HTTP/1.1", upstream: "http://10.24.4.49:4420/msgs/getMsg", host: "34.121.100.70", referrer: "http://34.121.100.70/"
10.128.0.7 - - [23/Aug/2020:16:05:11 +0000] "GET /msgs/getMsg HTTP/1.1" 502 559 "http://34.121.100.70/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"

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

1

1 Answers

4
votes

the second I posted it I saw my mistake: at the end of the server-demo deployment yaml

I missed configured:

selector:
  app: hello

which had to be:

selector:
  app: server-demo