本文档介绍如何从零开始,将本地的 Python 推理服务(FastAPI + PyTorch)打包成 Docker 镜像,并部署到 Sealos 集群中,通过 Service 供前端调用。
app/
│── app.py # FastAPI 推理服务入口
│── requirements.txt # 依赖文件
│── nets/ # 模型代码
│── util/ # 工具代码
│── checkpoint/ # 模型权重文件
│── Dockerfile # 镜像构建文件
docker build -t py-infer:latest .docker run --rm -p 8000:8000 py-infer:latestcurl -X POST "http://127.0.0.1:8000/infer" \
-F "file=@test.jpg" #根据具体需要post的内容修改返回结果符合预期,即说明服务正常。
或者:
- 打开浏览器:http://127.0.0.1:8000/docs → 发送 POST 请求 → 正常得到返回即成功
-
登录 Docker Hub 网站
-
右上角头像 → Account Settings → Security → New Access Token。
-
取个名字,点击 Generate。
-
复制生成的一长串 token。
-
回到 PowerShell,执行:
docker login-
Username: 你的 Docker Hub 用户名
-
Password: 刚生成的 Access Token
docker tag py-infer:latest your_dockerhub_username/py-infer:latestdocker push your_dockerhub_username/py-infer:latestkubectl version --client$env:KUBECONFIG = "C:\kube\kubeconfig.yaml"(改为你的路径)kubectl config view --minify -o "jsonpath={..namespace}"kubectl get pods -n user-system(user-system为之前上一步查询到的名字)- 能显示结果(哪怕是空列表而不是 Forbidden),说明你在 user-system 有权限
- 在本地新建
py-infer.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: py-infer #你的镜像名
namespace: ns-5vx9sy9v #你的user-system
spec:
replicas: 1
selector:
matchLabels:
app: py-infer
template:
metadata:
labels:
app: py-infer
spec:
# ★★★ Pod 级安全上下文(满足 restricted 策略)
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: py-infer
image: docker.io/elysialover/py-infer:latest #你的docker hub用户名/镜像名:Tag
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8000
# ★★★ 容器级安全上下文(满足 restricted 策略)
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
# readOnlyRootFilesystem 可选;部分库会写缓存,先不启用,若策略强制再说
# readOnlyRootFilesystem: true
readinessProbe:
httpGet: { path: /healthz, port: 8000 }
initialDelaySeconds: 20
periodSeconds: 5
failureThreshold: 6 # 可选:最多连续失败 6 次
timeoutSeconds: 2
livenessProbe:
httpGet: { path: /healthz, port: 8000 }
initialDelaySeconds: 40
periodSeconds: 10
failureThreshold: 3
timeoutSeconds: 2
resources:
requests: { cpu: "100m", memory: "256Mi" }
limits: { cpu: "1", memory: "1Gi" }
---
apiVersion: v1
kind: Service
metadata:
name: py-infer #你的镜像名
namespace: ns-5vx9sy9v #你的user-system
spec:
selector: { app: py-infer }
ports:
- name: http
port: 8000
targetPort: 8000
type: ClusterIPkubectl apply -f py-infer.yaml检查状态:
kubectl -n ns-xxxxxxx get pods -o wide
kubectl -n ns-xxxxxxx get svc/py-infer 18000:8000- Pod 状态变为 Running
- 打开 http://127.0.0.1:18000/healthz → 应返回 {"ok": true}
- 打开 http://127.0.0.1:18000/docs → 进行 POST 尝试
- 若前后端容器与Python服务在同一命名空间:
http://py-infer:8000/infer- 若不在同一命名空间
http://py-infer.ns-5vx9sy9v.svc.cluster.local:8000/infer- 写好
requirements.txt与Dockerfile。 - 本地构建并测试镜像。
- 推送镜像到 Docker Hub。
- 在 Sealos 写
py-infer.yaml,Deployment + Service。 - 用
kubectl apply部署。
这样就完成了 从本地代码 → Docker Hub → Sealos 集群 → 前后端访问 的全流程。