connection refused from frontend to backend in k8s cluster

12/4/2020

I have an application (front: Angular, back: Spring) deployed in Kubernetes. I have 3 pods: front, back, database (Mongo). My pods are running correctly and the back pod is connected to Mongo pod successfully. I dockerized the Angular app using NGINX.

Now when I access logs of three pods I don't have any problem, even the spring app is started successfully. O even tested API services from frontend pod with curl x Post, get..., everything works perfectly.

But from my browser, nothing works. it shows 'connection to http//:localhost:8080/api refused' !!
i eliminated any 'localhost' word in my angular project and replaced them all with the back service name but still telling me 'cannot connect to localhost'!!!

Here is services.yaml:

apiVersion: v1
kind: Service
metadata:
  name: front-service
  labels:
    app: front
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: front
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: back-service
  labels:
    app: back
spec:
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    app: back
  type: ClusterIP

Dockerfile of Angular:

   # Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM node:10.8.0 as build-stage
WORKDIR /app
RUN rm -rf node_modules
RUN npm cache clean --force 
RUN npm install
COPY package*.json /app/
COPY ./ /app/
ARG configuration=production
RUN npm run build -- --output-path=./dist/parkingangular 

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
#Copy ci-dashboard-dist
COPY --from=build-stage /app/dist/parkingangular/ /usr/share/nginx/html
#Copy default nginx configuration
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/conf.d
EXPOSE 80

nginx.conf :

upstream back-service {
    server back-service:8080;
}

server {
  listen 80;
  server_name localhost;
  location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
    }
  location /api {
     proxy_pass http://back-service;
    }
}

get pods:

NAME                        READY   STATUS    RESTARTS   AGE
backend-755c4bd78d-r4gk4    1/1     Running   0          125m
backend-755c4bd78d-z2pjt    1/1     Running   0          125m
frontend-546f8db776-62srl   1/1     Running   0          116m
frontend-546f8db776-jngt8   1/1     Running   0          116m
mongodb-66689f754d-hn5tm    1/1     Running   1          2d7h

kubectl get svc:

NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
back-service    ClusterIP   10.108.235.196   <none>        8080/TCP       5d3h
front-service   NodePort    10.103.234.24    <none>        80:32594/TCP   5d4h
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP        5d7h
mongoservice    ClusterIP   10.109.235.5     <none>        27017/TCP      2d8h

i even made npm clear cache and npm clean but in vain!!

-- manel ben amara
angular
docker
kubernetes
nginx

2 Answers

12/4/2020

Do you have port forwared to your local machine when using "localhost"?

kubectl port-forward -n <NAMESPACE> <POD> 8080:8080

Not sure what application exacly answers on your localhost:8080. you may need to forward to another local port.

Alternative you can use the ip adress of any kubernetes host and join the service port.

-- s8k-37
Source: StackOverflow

12/5/2020

problem solved with an ingress using nginx ingress :) i used nginx controller : first dowloaded two nginx controller yaml files and then created the ingress.yaml and set paths to font and back:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/cloud-generic.yaml

ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: frontend-ingress
spec:
    rules:
    - http:
        paths:
        - path: /api
          backend:
            serviceName: back-service
            servicePort: 8080
        - path: /
          backend:
            serviceName: front-service
            servicePort: 80
-- manel ben amara
Source: StackOverflow