You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

K3s中用Traefik路由Django应用遇404及连接问题求助

问题排查与修复方案

1. Service与Pod标签不匹配

你的Service配置中selector字段为name: web-sandbox,但Deployment的Pod模板里定义的标签是app: web-sandbox,两者完全不匹配,导致Service无法关联到对应的Pod,这是核心问题。

修复:修改Service的selector,使其与Pod标签一致:

apiVersion: v1
kind: Service
metadata:
  name: web-sandbox-service
spec:
  type: ClusterIP
  selector:
    app: web-sandbox  # 改为与Pod标签匹配的键值对
  ports:
  - protocol: TCP
    port: 8000
    targetPort: 8000  # 同步修改targetPort,见下一条
    name: HTTP

2. Service的targetPort配置错误

Service中targetPort设为80,但你的Pod容器暴露的端口是8000(gunicorn监听端口),即使Service找到Pod,流量也会发往错误端口,导致连接失败。

修复:将Service的targetPort改为8000,与容器的containerPort保持一致。

3. 访问名称的误区

你尝试用curl http://web-sandbox:8000/robots.txt访问,这里的web-sandbox是Deployment名称,并非Service或Pod的DNS标识。在Kubernetes集群内,需通过Service名称访问服务,正确命令应为:

curl http://web-sandbox-service:8000/robots.txt

4. Ingress 404的根源

因为Service未正确关联Pod,Traefik通过Ingress转发流量到Service后,没有可用的后端Pod承接请求,所以返回404。修复上述Service配置后,Ingress的问题会随之解决。

额外检查点

  • 确认Pod状态:执行kubectl get pods查看是否处于Running状态,kubectl describe pod <pod-name>检查事件是否正常。
  • 验证容器端口监听:进入Pod执行netstat -tulpn,确认gunicorn确实监听在8000端口,或查看容器日志确认启动命令是否正确。

内容的提问来源于stack exchange,提问作者Lucas03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:07:11