Search code examples
kubernetesfastapiambassador

Fastapi 404 on kubernates amabassador, adds a slash infront of the path


I am trying to deploy a fast API app on Kubernetes cluster AWS EKS, but after I deploy the API, when i hit the path of the endpoint I get a 494 error, since it adds a slash in front of the path

Here is my example fastapi code,

@app.get("/ping")
async def ping():
    """Health check"""
    response = {
        "message": HTTPStatus.OK.phrase,
        "status-code": HTTPStatus.OK
    }
    return response

Here is my yaml file for deploying on k8s cluster


apiVersion: v1
kind: Service
metadata:
  name: fastapi-example
  namespace: gpu
  labels:
    run: fastapi-example
    app: fastapi-example
spec:
  selector:
    app: fastapi-example
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
    name: http

---
apiVersion: getambassador.io/v2
kind: Mapping
name: fastapi-example-mapping
metadata:
  name: fastapi-example
  namespace: gpu
spec:
  prefix: fastapi-example
  service: fastapi-example.gpu:8080

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fastapi-example
  namespace: gpu
  labels:
    app: fastapi-example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fastapi-example
  template:
    metadata:
      labels:
        app: fastapi-example
    spec:
      nodeSelector:
        node-type: gpu
      containers:
      - name: fastapi-example
      
        image: <docker-image-url>
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          requests:
            cpu: "0.5"
          limits:
            cpu: "1"
        env:
     

After deploying when i access the url with the endpoint and the path, i am getting 404, when I checked the logs, it adds a slash infront of the path. For example, when i checked the logs, I get //ping instead of /ping.

Also to note, i have deployed a flask api on the same cluster with the same above configuration, but I was able to access the endpoint and the path- did not get any issues, Also I tested the api inside the cluster and was able to recieve the response, so i am not sure where the problem lies


Solution

  • You can use the Merge slashes option

    https://www.getambassador.io/docs/emissary/1.14/topics/running/ambassador#merge-slashes

    merge_slashes: true
    

    it will merge the slashes if extra available so //ping to /ping