【FastAPI 】踩坑总结
阅读目录
一、部署之殇
二、日志之殇
三、中间件之殇
四、配置文件之殇
五、其它
一、部署之殇
1 linux后台启动
nohup uvicorn main:app --host 0.0.0.0 --port 8080
2 Docker部署
FROM python:3.7
RUN pip install fastapi uvicorn
EXPOSE 80
COPY ./app /app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
2.1 Docker + gunicorn
gunicorn配置文件
#!usr/bin/env python
# encoding: utf-8
import multiprocessing
# 监听端口
bind = '0.0.0.0:8899'
# 工作模式
worker_class = 'uvicorn.workers.UvicornWorker'
# 并行工作进程数
workers = multiprocessing.cpu_count() * 2 + 1
# 设置守护进程
#daemon = True
# 配置文件方式配置日志
logconfig = "./logger.ini"
Dockerfile
FROM python:3.7
ENV TZ Asia/Shanghai
#将项目代码放入镜像
COPY . /app
WORKDIR /app
#安装第三方模块,更新数据库
RUN pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com \
&& rm -rf configure
ENTRYPOINT ["gunicorn", "-c", "gunicorn.conf.py", "main:app"]
3 k8s部署
3.1 service.yaml
apiVersion: v1
kind: Service
metadata:
name: project_name # 项目名称
spec:
ports:
- name: http
port: 80
targetPort: 8899
type: ClusterIP
3.2 deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: project_name # 项目名称
spec:
template:
spec:
imagePullSecrets:
- name: registry-pull-secret
contain