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

本地Kubernetes中Ingress无访问IP问题排查求助

解决Ingress无访问IP及服务暴露问题的思路

1. 确认Ingress Controller已安装并运行

本地Kubernetes环境(如Docker Desktop、Minikube)默认不会自动部署Ingress Controller,这是Ingress无ADDRESS的最常见原因:

  • 执行命令检查Ingress Controller Pod状态:
    kubectl get pods -n ingress-nginx
    
  • 如果无相关Pod,需安装对应Ingress Controller:
    • Docker Desktop:在设置中启用Kubernetes后,进入「Kubernetes」选项卡勾选「Enable ingress」
    • Minikube:执行minikube addons enable ingress

2. 为Ingress指定正确的Ingress Class

当前Ingress的CLASS为<none>,Kubernetes 1.19+版本要求明确指定ingressClassName,否则Ingress Controller不会接管该Ingress。修改Ingress配置:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: calculadora-ingress
  namespace: calculadora
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx  # 添加此行,指定使用Nginx Ingress Controller
  rules:
  - http:
      paths:
      - path: /Calculator/
        pathType: Prefix
        backend:
          service :
            name: calculadorarestapi-service
            port:
              number: 15001

更新配置后执行kubectl apply -f <ingress文件路径> -n calculadora,等待几分钟后再次查看Ingress状态,ADDRESS应会被分配。

3. 验证服务与Pod的基础连通性

先排除Pod和Service的问题,确认服务本身可正常访问:

  • 执行端口转发命令:
    kubectl port-forward service/calculadorarestapi-service 15001:15001 -n calculadora
    
  • 在浏览器或终端访问http://localhost:15001/Calculator/sum/1/1,若能正常返回结果,说明Pod和Service无问题,问题集中在Ingress配置。

4. 调整Ingress路径与转发规则

当前的rewrite-target配置可能导致路径转发错误,需根据服务实际路由调整:

  • 如果服务的接口路由是/Calculator/sum/1/1,则不需要rewrite,直接移除nginx.ingress.kubernetes.io/rewrite-target: /注解
  • 如果服务的接口路由是/sum/1/1(即去掉了/Calculator前缀),则修改路径规则与rewrite配置:
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$2
    spec:
      rules:
      - http:
          paths:
          - path: /Calculator(/|$)(.*)
            pathType: Prefix
            backend:
              service :
                name: calculadorarestapi-service
                port:
                  number: 15001
    

5. 调整端口映射以匹配预期访问地址

你期望通过http://localhost:15001/Calculator/sum/1/1访问,但Ingress默认使用80端口。若要映射到15001端口:

  • 找到Ingress Controller的Service(默认在ingress-nginx命名空间下,名称通常为ingress-nginx-controller)
  • 修改该Service的类型为NodePort,并指定nodePort为15001:
    kubectl patch service ingress-nginx-controller -n ingress-nginx -p '{"spec":{"type":"NodePort","ports":[{"port":80,"nodePort":15001,"name":"http"}]}}'
    
    之后即可通过http://localhost:15001/Calculator/sum/1/1访问服务。

6. 检查资源状态与关联

最后确认所有资源状态正常:

  • 执行kubectl get all -n calculadora,查看Deployment的Pod是否处于Running状态,Service的ENDPOINTS是否有Pod IP(说明Service已正确关联Pod)
  • 检查Ingress的事件日志:kubectl describe ingress calculadora-ingress -n calculadora,查看是否有错误信息提示

内容的提问来源于stack exchange,提问作者Marcos Guerrero Wilson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 06:10:30