Flask 在gke上找不到google云端点api_方法

Flask 在gke上找不到google云端点api_方法,flask,kubernetes,google-cloud-platform,google-kubernetes-engine,google-cloud-endpoints,Flask,Kubernetes,Google Cloud Platform,Google Kubernetes Engine,Google Cloud Endpoints,404响应方法:1.api\u端点\u gcp\u项目\u云\u goog.Postoperation失败:在google云端点esp上未找到 我正试图通过我的后端在GKE上使用google云端点部署我的API。我在生成的API日志中发现此错误,其中显示: 方法:1.api\u endpoints\u gcp\u project\u cloud\u goog.Postoperation失败:未找到 我从终点得到了404的回应 后端容器回答正确,但是当我尝试发布时,我得到404错误。我猜它与api

404响应
方法:1.api\u端点\u gcp\u项目\u云\u goog.Postoperation失败:在google云端点esp上未找到

我正试图通过我的后端在GKE上使用google云端点部署我的API。我在生成的API日志中发现此错误,其中显示:

方法:1.api\u endpoints\u gcp\u project\u cloud\u goog.Postoperation失败:未找到

我从终点得到了404的回应

后端容器回答正确,但是当我尝试发布时,我得到404错误。我猜它与api_方法名称有关,但我已经更改了,因此openapi.yaml、gke部署和app.py中的名称相同

我使用这个openapi.yaml成功地部署了API服务:

swagger: "2.0"
info:
  description: "API rest"
  title: "API example"
  version: "1.0.0"
host: "api.endpoints.gcp-project.cloud.goog"
basePath: "/v1"
# [END swagger]
consumes:
- "application/json"
produces:
- "application/json"
schemes:
# Uncomment the next line if you configure SSL for this API.
#- "https"
- "http"
paths:
  "/postoperation":
    post:
      description: "Post operation 1"
      operationId: "postoperation"
      produces:
      - "application/json"
      responses:
        200:
          description: "success"
          schema:
            $ref: "#/definitions/Model"
        400:
          description: "Error"
      parameters:
      - description: "Description"
        in: body
        name: payload
        required: true
        schema:
          $ref: "#/definitions/Resource"

definitions:
  Resource:
    type: "object"
    required:
    - "text"
    properties:
      tipodni:
        type: "string"
      dni:
        type: "string"
      text:
        type: "string"

  Model:
    type: "object"
    properties:
      tipodni:
        type: "string"
      dni:
        type: "string"
      text:
        type: "string"
      mundo:
        type: "string"
      cluster:
        type: "string"
      equipo:
        type: "string"
      complejidad:
        type: "string"
然后我尝试使用deploy.yaml和lb-deploy.yaml配置后端和esp

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: api-deployment
  namespace: development
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: api1
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: api1
    spec:
      volumes:
        - name: google-cloud-key
          secret:
            secretName: secret-key

      containers:
      - name: api-container
        image: gcr.io/gcp-project/docker-pqr:IMAGE_TAG_PLACEHOLDER
        volumeMounts:
        - name: google-cloud-key
          mountPath: /var/secrets/google
        ports:
        - containerPort: 5000

      - name: esp
        image: gcr.io/endpoints-release/endpoints-runtime:1
        args: [
          "--http_port=8081",
          "--backend=127.0.0.1:5000",
          "--service=api.endpoints.gcp-project.cloud.goog",
          "--rollout_strategy=managed"
        ]
        ports:
        - containerPort: 8081

kind: Service
metadata:
  name: "api1-lb"
  namespace: development
  annotations:
    cloud.google.com/load-balancer-type: "Internal"
spec:
  type: LoadBalancer
  #  loadBalancerIP: "172.30.33.221"
  selector:
    app: api1
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8081
我提供api的flask应用程序就是这个app.py

app = Flask(__name__)

categorizador = Categorizador(model_properties.paths)

@app.route('/postoperation', methods=['POST'])
def postoperation():

    text = request.get_json().get('text', '')
    dni = request.get_json().get('dni', '')
    tipo_dni = request.get_json().get('tipo_dni', '')

    categoria,subcategoria = categorizador.categorizar(text)

    content = {
        'tipodni': tipo_dni,
        'dni': dni,
        'text': text,
        'mundo': str(categoria),
        'cluster': str(subcategoria),
        'equipo': '',
        'complejidad': ''
    }

    return jsonify(content)

来自
kubectl expose-h的某些位

  • --port=''
    -服务应在其上提供服务的端口。从要公开的资源复制(如果未指定)
  • --target port=''
    -服务应将流量定向到的容器上的端口的名称或编号。 可选

代理将您的流量定向到
--backend=127.0.0.1:5000
,使用容器名称isntead
--backend=api container:5000

看起来您需要在flask应用程序中配置路由。 试试这个:

@app.route('/v1/postoperation', methods=['POST'])

这就是为什么我在openapi.yaml上使用basePath:“/v1”。正如我所说,后端运行正常,我认为问题可能出在esp上。我不认为这个“basePath”url会自动传播到flask路由。这只是一个模式,应该在实际应用程序中重复。至少它对我是这样工作的。我删除了basePath,它工作正常。这似乎是我尝试对其进行版本设置时遇到的问题。是的,将其删除或添加到应用程序yaml文件中的处理程序中,这也是它对我的工作方式。我正在定义服务yaml上的端口和目标端口。实际上,我在容器之间没有通信问题,当我从esp转到127.0.0.1时,我得到了正确的答案