Kaj*_*jsa 8 nginx kubernetes kubernetes-ingress nginx-ingress fastapi
I have written an application that runs a FastAPI server inside a Kubernetes pod. The external communication with the pod goes through an nginx ingress controller in a separate pod. I am running nginx:1.17.0.
When it is all up and running I can use curl calls to interact with the app server through the ingress address, and access all the simple GET paths as well as address/openapi.json in my browser. I can also access the interactive documentation page if I use the internal ip of the app service in Kubernetes.
However trying to reach the interactive documentation page (address/docs#/default/) gives me an error regarding /openapi.json.
Since the curl calls work as expected I do not think the problem is necessarily in the ingress definition but as using the internal ip of the app also works fine the issue should not be inside the app.
I have included the ingress definition file below.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.17.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: my-host.info
http:
paths:
- path: /server(/|$)(.*)
backend:
serviceName: my-app-service # This is the service that runs my fastAPI server pod
servicePort: 80
Run Code Online (Sandbox Code Playgroud)
EDIT
This is the service.yaml file
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: ClusterIP
selector:
app: server
ports:
- protocol: "TCP"
port: 80
targetPort: 80
Run Code Online (Sandbox Code Playgroud)
As the service is a ClusterIP inside my local cluster I have might be able to curl straight to it, I have not tried though. When I curl I use commands like
curl -X GET "http://my-host.info/server/subpath/" -H "accept: application/json"
curl -X POST "http://my-host.info/server/subpath/update/" -H "accept: application/json"
Run Code Online (Sandbox Code Playgroud)
from outside the local cluster.
These are all the services that are running:
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
default my-app-service ClusterIP 10.96.68.29 <none> 80/TCP 18h
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 28d
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.96.114.1 <none> 8000/TCP 28d
kubernetes-dashboard kubernetes-dashboard ClusterIP 10.96.249.255 <none> 80/TCP 28d
Run Code Online (Sandbox Code Playgroud)
and inside my /etc/hosts file I have connected 10.0.0.1 (cluster "external" IP) to my-host.info.
Any ideas of why this is happening?
我认为你绝对应该查看 FastApi 的官方文档:https ://fastapi.tiangolo.com/advanced/behind-a-proxy/
正如您所提到的,当在内部访问您的应用程序时,Swagger 自动文档工作正常,但是当从集群外部访问时,您会收到有关 /openapi.json 的错误。
在你的service.yaml你有:
- path: /server(/|$)(.*)
backend:
serviceName: my-app-service # This is the service that runs my fastAPI server pod
servicePort: 80
Run Code Online (Sandbox Code Playgroud)
当使用 uvicorn 开始你的应用程序时,你应该通过root_path
uvicorn main:app --root-path /server
注意:在这里您将能够访问路由器端点,但不能访问 Swagger 文档。为了获取 Swagger 文档,您必须编辑主main.py文件:
- path: /server(/|$)(.*)
backend:
serviceName: my-app-service # This is the service that runs my fastAPI server pod
servicePort: 80
Run Code Online (Sandbox Code Playgroud)
我搜索了为什么我们需要显式传递 OpenApi 前缀,但我发现只有类似的解决方法:https ://github.com/iwpnd/fastapi-aws-lambda-example/issues/2
因此,我建议存储在系统上的root_path环境变量中并将其传递给以及:$ROOT_PATH=/serveruvicorn main:app --root-path $ROOT_PATHmain.py
from fastapi import FastAPI, Request
app = FastAPI(openapi_prefix="/server")
@app.get("/")
def read_root(request: Request):
return {"message": "Hello World", "root_path": request.scope.get("root_path")}
Run Code Online (Sandbox Code Playgroud)
更新 07.07.2020
目前,tiangolo 准备使用 docker 镜像https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker如果继续使用 fastapi 版本(目前为:0.55.1),则“已过时” - 此处报告:链接
从 0.56.0 开始支持“root_path”
使用 Kubernetes重写功能进行入口,我可以像这样解决我的问题:
入口.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: my-app
http:
paths:
- path: /server(/|$)(.*)
pathType: Prefix
backend:
service:
name: my-fastapi
port:
number: 80
Run Code Online (Sandbox Code Playgroud)
然后我只需要将root_path添加到我的 FastAPI 应用程序中:
app = FastAPI(root_path="/server")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9402 次 |
| 最近记录: |