Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Refused with nginx and kubernetes

I trying to deploy my angular application with kubernates inside a container with nginx.

I create my docker file:

FROM node:10-alpine as builder

COPY package.json package-lock.json ./

RUN npm ci && mkdir /ng-app && mv ./node_modules ./ng-app

WORKDIR /ng-app

COPY . .

RUN npm run ng build -- --prod --output-path=dist

FROM nginx:1.14.1-alpine

COPY nginx/default.conf /etc/nginx/conf.d/

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

COPY --from=builder /ng-app/dist /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

My nginx config:

server {

  listen 80;

  sendfile on;

  default_type application/octet-stream;


  gzip on;
  gzip_http_version 1.1;
  gzip_disable      "MSIE [1-6]\.";
  gzip_min_length   1100;
  gzip_vary         on;
  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;
  gzip_comp_level   9;


  root /usr/share/nginx/html;

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

  location /api {
    proxy_pass https://my-api;
  }

}

If I launch this image locally It works perfectly but when I deploy this container inside a kubernate cluster the site load fine but all api request shows the error ERR_CONNECTION_REFUSED.

I'm trying to deploy in GCP I build the image and then publish my image by GCP dashboard.

Some idea for this ERR_CONNECTION_REFUSED?

like image 985
Brunno Avatar asked Mar 26 '19 13:03

Brunno


2 Answers

I found the solution. The problem was with my requests, I was using localhost at the URL, with that I took the wrong pod IP. I've just changed the request to use straight the service IP and that sort out my problem.

like image 118
Brunno Avatar answered Nov 11 '22 01:11

Brunno


Kubernetes Engine nodes are provisioned as instances in Compute Engine. As such, they adhere to the same stateful firewall mechanism as other instances. Have you configured the firewall rules?

https://cloud.google.com/solutions/prep-kubernetes-engine-for-prod#firewalling

like image 38
afed Avatar answered Nov 11 '22 02:11

afed