Cannot connect to backend with POST in Kubernetes

12/28/2019

I would like to set up my project in k8s environment. My app consists of frontend(react), api(node.js) and db(mongoDB). What I can do now is setup frontend so I can see mainpage, also I can request backend with GET method (both from postman and browser). But for reason I cannot establish proper connection with API through POST, ie. I can see that the endpoint is reached but the payload is empty. In non-k8s environment it works well so it needs to be connected to some k8s setup.

Client deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: panel-admin-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: panel-admin
  template:
    metadata:
      labels:
        component: panel-admin
    spec:
      containers:
        - name: panel-admin
          image: XXX
          ports:
            - containerPort: 3000
          env:
            - name: API_BASE_URL
              value: panel-admin-server-service
      imagePullSecrets:
        - name: gcr-json-key

Client service:

apiVersion: v1
kind: Service
metadata:
  name: panel-admin-service
spec:
  type: ClusterIP
  selector:
    component: panel-admin
  ports:
    - port: 3000
      targetPort: 3000

Ingress for both api and frontend routes:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - http:
        paths:
          - path: /.*
            backend:
              serviceName: panel-admin-service
              servicePort: 3000
          - path: /api/.* // here goes my GET request and it works well
            backend:
              serviceName: panel-admin-server-service
              servicePort: 3001
          - path: /auth/.* // here goes my POST request, ie. it starts with auth
            backend:
              serviceName: panel-admin-server-service 
              servicePort: 3001

Server deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: panel-admin-api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: panel-admin-api
  template:
    metadata:
      labels:
        component: panel-admin-api
    spec:
      containers:
        - name: panel-admin-api
          image: XXX
          ports:
            - containerPort: 3001
          env:
            - name: MONGO_URL
              value: mongodb://panel-admin-mongo-cluster-ip-service/mongodb
      imagePullSecrets:
        - name: gcr-json-key

Server service:

apiVersion: v1
kind: Service
metadata:
  name: panel-admin-server-service
spec:
  type: ClusterIP
  selector:
    component: panel-admin-api
  ports:
    - port: 3001
      targetPort: 3001

So from the browser I'm getting 502 Bad Gateway error, from Postman it reaches the endpoint but the POST payload is empty. What am I missing here?

-- Murakami
docker
google-cloud-platform
kubernetes

1 Answer

1/1/2020

Try changing the URL from http://example.com/api to https://example.com/api if you are using HTTPS

POST requests doesn't redirect to https automatically by default refer Redirecting HTTP POST requests to HTTPS POST requests

-- Tummala Dhanvi
Source: StackOverflow