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

在Kubernetes不同命名空间配置Nginx Plus Ingress遇404问题排查

问题排查:Nginx Plus Ingress配置后返回404

环境与现状

  • MicroK8s环境中Nginx Plus Ingress控制器已正常运行(部署在nginx-ingress命名空间)
  • Web应用部署在web命名空间,Deployment、Service均正常运行,但Ingress转发后访问返回404

核心问题分析与修复步骤

1. Ingress类配置冲突

你的Ingress资源同时使用了废弃的annotation和标准的ingressClassName字段,且两者取值不一致:

  • annotation: kubernetes.io/ingress.class: nginx-ingress
  • spec字段: ingressClassName: nginx

这会导致Nginx控制器无法正确识别并加载该Ingress规则。

修复方案:
保持配置一致,推荐使用K8s 1.19+标准的ingressClassName字段:

  1. 先确认Nginx Plus Ingress控制器的ingress-class取值:
    kubectl describe deployment nginx-ingress -n nginx-ingress | grep "ingress-class"
    
    若输出类似--ingress-class=nginx-ingress,则控制器的类名为nginx-ingress。
  2. 修改Ingress YAML,统一配置:
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: web-server-ingress
      namespace: web
      # 移除冲突的annotation
    spec:
      ingressClassName: nginx-ingress  # 与控制器的ingress-class取值保持一致
      rules:
      - host: web.example.com
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web-server-service
                port:
                  number: 3000
    
  3. 应用修改:
    kubectl apply -f your-ingress-file.yaml
    

2. 验证Service到Pod的连通性

确认Service能正确转发请求到Pod:

# 进入Nginx Ingress控制器Pod,测试访问Web Service
kubectl exec -n nginx-ingress nginx-ingress-8dd4c95f5-b5qzs -- curl web-server-service.web.svc.cluster.local:3000

如果返回404,说明问题出在Web应用本身或Service配置:

  • 检查Pod内应用是否在80端口正常监听:kubectl exec -n web <pod-name> -- netstat -tulpn
  • 确认Service的targetPort与Pod的containerPort(或端口名)匹配(你的配置中targetPort: http对应Pod的name: http端口,配置本身是正确的)

3. 确认访问方式正确

Ingress规则绑定了web.example.com域名,需确保:

  1. 本地/etc/hosts添加映射:
    <你的MicroK8s节点IP> web.example.com
    
  2. 使用NodePort端口访问:http://web.example.com:31656(80端口对应NodePort 31656)

4. 检查Ingress控制器日志

查看控制器是否加载了Ingress规则,有无报错:

kubectl logs -n nginx-ingress nginx-ingress-8dd4c95f5-b5qzs

如果日志中未出现web-server-ingress相关的规则加载记录,说明类配置仍不匹配,回到步骤1重新确认。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 04:46:18