在Kubernetes中无法通过POST连接到后端

时间:2019-12-28 21:25:40

标签: docker kubernetes google-cloud-platform

我想在k8s环境中设置我的项目。我的应用程序由frontend(react),api(node.js)和db(mongoDB)组成。我现在可以做的是设置前端,这样我就可以看到主页,也可以使用GET方法请求后端(来自邮递员和浏览器)。但是由于某种原因,我无法通过POST与API建立正确的连接,即。我可以看到已到达端点,但有效负载为空。在非k8s环境中,它运行良好,因此需要将其连接到某些k8s设置。

客户端部署:

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

客户服务:

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

api和前端路由的入口:

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

服务器部署文件:

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

服务器服务:

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

因此,从浏览器中我收到502 Bad Gateway错误,从Postman到达端点,但是POST有效负载为空。我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

如果使用的是HTTPS,请尝试将URL从http://example.com/api更改为https://example.com/api

默认情况下,POST请求不会自动重定向到https,请参考Redirecting HTTP POST requests to HTTPS POST requests